Skip to content

Instantly share code, notes, and snippets.

@darko-mesaros
Created June 8, 2020 08:50
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save darko-mesaros/96da6f5178b6ce444118d1ca996d8738 to your computer and use it in GitHub Desktop.
Save darko-mesaros/96da6f5178b6ce444118d1ca996d8738 to your computer and use it in GitHub Desktop.
AWS CDK - Launch an EC2 Autoscaling Group with userdata read of disk
// VPC
const vpc = new ec2.Vpc(this, 'VPC');
// Security group
const webSg = new ec2.SecurityGroup(this, 'WebSG',{
vpc: vpc,
allowAllOutbound: true,
description: "Web Server Security Group"
});
webSg.addIngressRule(ec2.Peer.anyIpv4(), ec2.Port.tcp(8080), 'Web from anywhere')
// ASG Configuration
const asg = new autoscaling.AutoScalingGroup(this, 'ASG', {
vpc: vpc,
instanceType: ec2.InstanceType.of(ec2.InstanceClass.BURSTABLE2, ec2.InstanceSize.MICRO),
machineImage: new ec2.AmazonLinuxImage(), // get the latest Amazon Linux image
maxCapacity: 5,
minCapacity: 1,
desiredCapacity: 3,
role: instanceRole
});
// Add user Data
var bootscript:string;
bootscript = fs.readFileSync('assets/userdata.sh','utf8');
asg.addUserData(bootscript);
asg.addSecurityGroup(webSg)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment