Skip to content

Instantly share code, notes, and snippets.

@dbgeek
Created November 16, 2019 12:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dbgeek/e46da7f75720584e1fc09c9ff594a63a to your computer and use it in GitHub Desktop.
Save dbgeek/e46da7f75720584e1fc09c9ff594a63a to your computer and use it in GitHub Desktop.
aws cdk - asg eventbus lambda
import cdk = require('@aws-cdk/core');
import autoscaling = require('@aws-cdk/aws-autoscaling');
import ec2 = require('@aws-cdk/aws-ec2');
import eventBus = require('@aws-cdk/aws-events')
import lambda = require('@aws-cdk/aws-lambda')
import lambdTarget = require('@aws-cdk/aws-events-targets')
export class AsgEventbusStack extends cdk.Stack {
constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props);
const vpc = ec2.Vpc.fromLookup(this, 'vpc-9d9872f4', { isDefault: true })
const eventLambda = new lambda.Function(this, 'asgEventLambda', {
runtime: lambda.Runtime.NODEJS_8_10,
code: lambda.Code.asset('lambda'),
handler: 'asgevent.handler'
})
const target = new lambdTarget.LambdaFunction(eventLambda)
const busRule = new eventBus.Rule(this, 'eventbus-rule-asg', {
eventPattern: {
source: ['aws.autoscaling'],
detailType: [
"EC2 Instance Launch Successful",
"EC2 Instance Terminate Successful",
"EC2 Instance Launch Unsuccessful",
"EC2 Instance Terminate Unsuccessful",
"EC2 Instance-launch Lifecycle Action",
"EC2 Instance-terminate Lifecycle Action"
]
},
targets: [
target
]
})
// We are using the latest AMAZON LINUX AMI
const awsAMI = new ec2.AmazonLinuxImage({generation: ec2.AmazonLinuxGeneration.AMAZON_LINUX_2});
const asg = new autoscaling.AutoScalingGroup(this, 'asgEventTest', {
instanceType: ec2.InstanceType.of(ec2.InstanceClass.T3, ec2.InstanceSize.NANO),
machineImage: awsAMI,
updateType: autoscaling.UpdateType.REPLACING_UPDATE,
desiredCapacity: 1,
maxCapacity: 1,
minCapacity: 1,
spotPrice: "0.0017",
vpc,
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment