Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS CDK How to override default launch configuration in auto scaling group?

Tags:

Hi I am working on AWS CDK. I am creating ECS. I have created auto scaling group as below.

autoScallingGroup=asg.AutoScalingGroup(self, id = "auto scalling", vpc= vpc, machine_image=ecs.EcsOptimizedImage.amazon_linux(), desired_capacity=1, key_name="mws-location", max_capacity=1, min_capacity=1, instance_type=ec2.InstanceType("t2.xlarge"))

This will generate default launch configuration also. I want to write my own launch configuration for this auto scaling group.

Can someone help me to fix this? Any help would be appreciated. Thanks

like image 949
Niranjan Avatar asked Dec 06 '19 10:12

Niranjan


1 Answers

There is no specific construct to create a launch configuration in CDK. However, you can construct one by passing in arguments to aws_autoscaling.AutoScalingGroup constructor.

You need to specifiy the following attributes of AutoScalingGroup class:

  • role
  • instance_type
  • key_name
  • machine_image
  • user_data
  • associate_public_ip_address
  • block_devices

You can also add security groups using the add_security_group() function.

For example, if you want to add user data to the LaunchConfig:

userdata = ec2.UserData.for_linux(shebang="#!/bin/bash -xe")
userdata.add_commands(
         "echo '======================================================='",
         "echo \"ECS_CLUSTER=${MWSServiceCluster}\" >> /etc/ecs/ecs.config"
)

asg = autoscaling.AutoScalingGroup(
        self,
        "asg-identifier",
        ...
        user_data=userdata,
)
like image 61
Vikyol Avatar answered Sep 30 '22 21:09

Vikyol