Niranjan
Niranjan

Reputation: 2229

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

Upvotes: 3

Views: 833

Answers (3)

Nour Al-Habash
Nour Al-Habash

Reputation: 1

I also had the same issue, in a stack that I wrote in Python using the CDK. I saw several solutions online that were outdated, since the recommended ECS practice seems to add an ASG capacity provider to the cluster. Here's what the relevant stack portion looks like:

    ...
    service = ecs.Ec2Service(
        self, 'Ec2Service',
        cluster=cluster,
        task_definition=task_definition,
    )
    asg = autoscaling.AutoScalingGroup(self, "AutoScalingGroup")
    capacity_provider = ecs.AsgCapacityProvider(
        self, "AsgCapacityProvider",
        auto_scaling_group=asg,
    )
    cluster.add_asg_capacity_provider(capacity_provider)
    ...

This code adds extra resources to the stack for the LifecycleHookDrainHook and the DrainECSHook, like an SNS topic and a Lambda function.

However I realized these don't get created when I specify the desiredCount in the Ec2Service construct.

    ...
    service = ecs.Ec2Service(
        self, 'Ec2Service',
        cluster=cluster,
        task_definition=task_definition,
        desired_count=ecs_desired_count
    )
    ...

Maybe that will help if someone else runs into this.

Upvotes: 0

Eugene
Eugene

Reputation: 2974

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;

Upvotes: 0

Mitchell Valine
Mitchell Valine

Reputation: 254

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

Upvotes: 1

Related Questions