'How to disable life-cycle hooks when creating auto scaling group?

Hi I am working on aws cdk to create resources in aws. When i created aws auto scaling group, I see there are many other resources got created.

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 is also creating below resources such as

autoscallingDrainECSHookFunctionServiceRole219A7F8B,
autoscallingDrainECSHookFunctionServiceRoleDefaultPolicyE2FB5F79,
autoscallingDrainECSHookFunctionBE2A2160,
autoscallingDrainECSHookFunctionAllowInvokeLocationCdkStackcdkstackautoscallingLifecycleHookDrainHookTopicA75797CC21F927C0,
autoscallingDrainECSHookFunctionTopic3103D34F,
autoscallingLifecycleHookDrainHookRoleA95F8BD2,
autoscallingLifecycleHookDrainHookRoleDefaultPolicyBB70BF84,
autoscallingLifecycleHookDrainHookTopicA04CE464,
autoscallingLifecycleHookDrainHook9489AED1

Why these resources are created and If I don't want these resources how can I restrict it? Can someone help me in this? Thanks



Solution 1:[1]

The "L2" constructs in aws-cdk are meant to be "batteries included". This usually means it will generate things like roles (with minimum needed permissions) and other resources that are commonly used and are considered best practice.

When creating an autoscaling group as you have above, the resources I get are:

AWS::EC2::SecurityGroup - security group that instances get when created as part of asg

AWS::IAM::Role - iam role that instances get when created as part of asg

AWS::IAM::InstanceProfile - instance profile for instances, this is how the role is linked to instances when they launch

AWS::AutoScaling::LaunchConfiguration - launch config for instances, profile and security groups are attached here

AWS::AutoScaling::AutoScalingGroup - the ASG itself, with the above LaunchConfiguration as a property

Lifecycle hooks are added only after asg creation. If you're passing the ASG construct to another L2 construct that relies on lifecycle hooks, it probably is creating them automatically.

The "batteries included" approach is to abstract all of this, however, L1 constructs exist if you want to create all of these resources manually and tune them yourself. You can do this with the CfnAutoScalingGroup class

Solution 2:[2]

I had the same issue - the lifecycle hook and ECS hooks were just causing problems and huge delays for me. I ended up doing something pretty ugly, so, your mileage may vary, but here ya go (in TypeScript):

const asg = new autoscaling.AutoScalingGroup(this, 'myasg', { ...asgprops })
//@ts-ignore;
delete asg.node._children.LifecycleHookDrainHook;
//@ts-ignore;
delete asg.node._children.DrainECSHook;

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Mitchell Valine
Solution 2 Eugene