Skip to content

Instantly share code, notes, and snippets.

@pyr-revs
Created October 6, 2015 09:15
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save pyr-revs/31dba1c9aeff575f58b9 to your computer and use it in GitHub Desktop.
Save pyr-revs/31dba1c9aeff575f58b9 to your computer and use it in GitHub Desktop.
Launch Chainer via AWS Lambda
Launch Chainer via AWS Lambda
console.log('Launch-Chainer: Start');
var ec2Region = 'us-east-1';
var s3Region = 'ap-northeast-1';
var snsRegion = 'ap-northeast-1';
var s3Bucket = 'mybucket';
var shellScriptS3Key = 'sh/launch_chainer.sh';
var shellScriptS3Path = 's3://' + s3Bucket + '/' + shellScriptS3Key;
var cuDnnAS3Path = 's3://' + s3Bucket + '/cuda/cudnn-6.5-linux-x64-v2.tgz'; // optional
var availabilityZone = ec2Region + 'd';
var spotPrice = '0.2';
var imageId = 'ami-65116700'; // us-east-1, Amazon Linux 2015.09, HVM Instance Store 64 bit
//var imageId = 'ami-e3106686'; // us-east-1, Amazon Linux 2015.09, HVM(SSD)EBS-Backed 64 bit
//var imageId = 'ami-a22fb8a2'; // ap-northeast-1, Amazon Linux 2015.09, HVM Instance Store 64 bit
//var imageId = 'ami-9a2fb89a'; // ap-northeast-1, Amazon Linux 2015.09, HVM(SSD)EBS-Backed 64 bit
var instanceType = 'g2.2xlarge';
var iamInstanceProfile = 'my_ec2_role';
var securityGroup = 'launch-wizard-1';
var keyName = 'my_ssh_keypair';
var userData = (function () {/*#!/bin/bash
cd /root
# Update sudoers
tmp_sudoers=/root/sudoers_tmp
cat /etc/sudoers > $tmp_sudoers
cat >> $tmp_sudoers <<EOF
Defaults:ec2-user !requiretty
EOF
cat $tmp_sudoers > /etc/sudoers
# Install yum deps
yum update -y
yum groupinstall -y "Development tools"
yum -y install gcc-c++ python27-devel atlas-sse3-devel lapack-devel
yum install -y kernel-devel-`uname -r`
# Install NVIDIA Driver
wget -q http://us.download.nvidia.com/XFree86/Linux-x86_64/346.96/NVIDIA-Linux-x86_64-346.96.run
chmod +x NVIDIA-Linux-x86_64-346.96.run
./NVIDIA-Linux-x86_64-346.96.run -s > driver.log 2>&1
# Install CUDA (without driver installation... for Amazon Linux 2015.09)
wget -q http://developer.download.nvidia.com/compute/cuda/7_0/Prod/local_installers/cuda_7.0.28_linux.run
chmod +x cuda_7.0.28_linux.run
./cuda_7.0.28_linux.run -extract=/root
./cuda-linux64-rel-7.0.28-19326674.run -noprompt > cuda.log 2>&1
# Install cuDNN (Optional)
#aws s3 cp %s ./
#tar zxvf cudnn-6.5-linux-x64-v2.tgz
#cd cudnn-6.5-linux-x64-v2
#cp lib* /usr/local/cuda/lib64/
#cp cudnn.h /usr/local/cuda/include/
# Install python deps
pip install numpy
pip install six
# Update .bashrc for ec2-user
tmp_bashrc=/home/ec2-user/.bashrc_backup
cat /home/ec2-user/.bashrc > $tmp_bashrc
cat >> $tmp_bashrc <<EOF
export PATH=/usr/local/cuda/bin:$PATH
export LD_LIBRARY_PATH=/usr/local/cuda/lib64
EOF
cat $tmp_bashrc > /home/ec2-user/.bashrc
# Launch post-installation script with ec2-user
aws s3 cp %s /home/ec2-user/launch_chainer.sh
chown ec2-user /home/ec2-user/launch_chainer.sh
chmod +x /home/ec2-user/launch_chainer.sh
su - ec2-user /home/ec2-user/launch_chainer.sh
*/}).toString().match(/[^]*\/\*([^]*)\*\/\}$/)[1];
var shellScriptContents = (function () {/*#!/bin/bash
cd /home/ec2-user
# Install Chainer
git clone https://github.com/pfnet/chainer
cd /home/ec2-user/chainer
sudo -s python setup.py install > setup.log 2>&1
# Run Chainer Sample with GPU
cd /home/ec2-user/chainer/examples/mnist
python train_mnist.py --gpu=0 > run.log 2>&1
# Send SNS Message
export AWS_DEFAULT_REGION=%s
aws sns publish --topic-arn arn:aws:sns:ap-northeast-1:xxxxxxxxxxxx:My-SNS-Topic --subject "Launch Chainer Done" --message "Launch Chainer Done!!"
*/}).toString().match(/[^]*\/\*([^]*)\*\/\}$/)[1];
exports.handler = function(event, context) {
var util = require('util');
var AWS = require('aws-sdk');
// Write sh file for chainer launch to S3
AWS.config.region = s3Region;
var shellScriptContentsFormatted = util.format(shellScriptContents, snsRegion);
var s3 = new AWS.S3();
var s3Params = {Bucket: s3Bucket, Key: shellScriptS3Key, Body: shellScriptContentsFormatted};
var s3Options = {partSize: 10 * 1024 * 1024, queueSize: 1};
s3.upload(s3Params, s3Options, function(err, data) {
if (err) {
console.log(err, err.stack);
context.fail('[Fail]');
}
else {
console.log(data);
// Lauch EC2 Spot Instance with UserData
var userDataFormatted = util.format(userData, cuDnnAS3Path, shellScriptS3Path);
var userDataBase64 = new Buffer(userDataFormatted).toString('base64');
var ec2LaunchParams = {
SpotPrice: spotPrice,
LaunchSpecification : {
IamInstanceProfile: {
Name: iamInstanceProfile
},
// EBS Setting (for ami-e3106686)
/*
BlockDeviceMappings : [
{
DeviceName : '/dev/xvda',
Ebs : { VolumeSize : 16 }
},
],
*/
// Instance Storage Setting (for ami-65116700)
BlockDeviceMappings : [
{
DeviceName : '/dev/sdb',
VirtualName : 'ephemeral0'
}
],
ImageId: imageId,
InstanceType: instanceType,
KeyName: keyName,
Placement: {
AvailabilityZone: availabilityZone
},
SecurityGroups: [
securityGroup
],
UserData: userDataBase64
}
};
AWS.config.region = ec2Region;
var ec2 = new AWS.EC2();
ec2.requestSpotInstances(ec2LaunchParams, function(err, data) {
if (err) {
console.log(err, err.stack);
context.fail('[Fail]');
}
else {
console.log(data);
context.succeed('[Succeed]');
}
});
}
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment