Last active
July 24, 2019 09:29
-
-
Save Console32/e721d66e394feef1fd6e1289519994fe to your computer and use it in GitHub Desktop.
medium.bastion-host.createLambdaCode.ts
This file contains 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
private createLambdaCode(publicIpAddress: string): lambda.Code { | |
return lambda.Code.inline(` | |
var AWS = require('aws-sdk'); | |
AWS.config.update({region: 'eu-central-1'}); | |
exports.handler = (event,context,callback) => { | |
console.log(event.Records[0].Sns.Message) | |
const message = JSON.parse(event.Records[0].Sns.Message); | |
console.log(message.Event) | |
if(message.Event === "autoscaling:EC2_INSTANCE_LAUNCH") | |
{ | |
const instanceId = message.EC2InstanceId | |
var ec2 = new AWS.EC2({apiVersion: '2016-11-15'}); | |
console.log(\`associate Address \${instanceId} with ${publicIpAddress}\`); | |
ec2.associateAddress({ | |
InstanceId: instanceId, | |
PublicIp: "${publicIpAddress}" | |
}, function(err,data) { | |
if(err) | |
{ | |
callback(err) | |
} | |
else | |
{ | |
callback(null,data) | |
} | |
}) | |
} | |
}; | |
`) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment