Last active
June 28, 2017 16:48
-
-
Save efueyo/e630a366c1f5dad40c544005df592873 to your computer and use it in GitHub Desktop.
Lambda_Run_Instance
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const fs = require('fs'); | |
| const aws = require('aws-sdk'); | |
| const ec2 = new aws.EC2({ region: 'eu-west-1' }); | |
| function getUserData() { | |
| return fs.readFileSync(`${__dirname}/user_data.sh`, { encoding: 'utf-8' }); | |
| } | |
| function deployInstance(cb) { | |
| const data = { | |
| ImageId: 'YOUR-AMI-id', // EDIT ME | |
| MaxCount: 1, | |
| MinCount: 1, | |
| InstanceType: 'the instance type, like m4.medium', // EDIT ME | |
| UserData: new Buffer(getUserData()).toString('base64'), | |
| InstanceInitiatedShutdownBehavior: 'terminate', | |
| SecurityGroupIds: ['ID for Task Sec.Group'], // EDIT ME | |
| TagSpecifications: [{ | |
| ResourceType: 'instance', | |
| Tags: [{ | |
| Key: 'Name', | |
| Value: 'Task Instance', | |
| }], | |
| }], | |
| }; | |
| ec2.runInstances(data, cb); | |
| } | |
| exports.handler = function handler(event, context) { | |
| console.log(`Received: ${JSON.stringify(event)}`); | |
| deployInstance((e) => { | |
| if (e) { | |
| console.log(`Error: ${e}`); | |
| context.done('Something Failed'); | |
| return; | |
| } | |
| context.done(); | |
| }); | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment