Aws-cdk: ec2: cfn-init support in ASGs

Created on 20 Dec 2018  ·  10Comments  ·  Source: aws/aws-cdk

I want to use the logical ID of the resource created in the construct library.
For example, cfn-signal require AutoScalingGroup's logical id.

I wrote this code.

    const asg = cluster.addDefaultAutoScalingGroupCapacity({
      instanceType: new ec2.InstanceType("t2.xlarge"),
      instanceCount: 1,
    });

    const appLaunchTemplate = new ec2.cloudformation.LaunchTemplateResource(this, 'AppLaunchTemplate', {
      launchTemplateName: "launchtemplatetest",
      launchTemplateData: {
        userData: new cdk.FnBase64([
            `#!/bin/sh`,
            `set -e`,
            `echo ECS_CLUSTER=${cluster.clusterName} >> /etc/ecs/ecs.config`,
            `/opt/aws/bin/cfn-signal -e $? --stack ${new cdk.AwsStackName()} --resource ${asg.autoScalingGroupName} --region ${new cdk.AwsRegion}`,
          ].join('\n')
        ),
      }
    })
    appLaunchTemplate.addDependency(instanceProfile)

    const asgResource = asg.findChild('ASG') as autoscaling.cloudformation.AutoScalingGroupResource

    asgResource.addPropertyDeletionOverride('LaunchConfigurationName')
    asgResource.addPropertyOverride('LaunchTemplate', {
      LaunchTemplateId: appLaunchTemplate.ref,
      Version: '1'
    })

However Circular dependency caused.

    SampleStack failed: ValidationError: Circular dependency between resources: [AppLaunchTemplate, ClusterDefaultAutoScalingGroupLifecycleHookDrainHook4A9A4325, ClusterDefaultAutoScalingGroupASG0F98E147]
Circular dependency between resources: [AppLaunchTemplate, ClusterDefaultAutoScalingGroupLifecycleHookDrainHook4A9A4325, ClusterDefaultAutoScalingGroupASG0F98E147]

This will be solved by specifying a name or not have a hash in construct library.

@aws-cdaws-ec2 efforlarge feature-request managementracking p1

Most helpful comment

For future reference, this is how you can get the logical ID for the autoscaling group:

const autoScalingGroupCfn = <CfnAutoScalingGroup> autoscalingGroup.node.tryFindChild('ASG');
console.log(autoScalingGroupCfn.logicalId);

All 10 comments

To to make sure I understand the problem: asg.autoScalingGroupName is not the logical ID but rather a the actual name of the ASG, resolved only at deploy-time, and that's the reason we are getting the cyclic dependency, correct?

What you could do is simply use asgResource.logicalId instead of asg.autoScalingGroupName:

`/opt/aws/bin/cfn-signal -e $? --stack ${new cdk.AwsStackName()} --resource ${asgResource.logicalId} --region ${new cdk.AwsRegion}`,

As for a longer term solution, I am not sure this is actually a very common need in the general sense, but perhaps we can provide a nice abstract to 'cfn-signal' so you could do something like:

asg.cfnSignal({ options });

And it will return the "correct" cfn-signal command bound to this specific ASG.

thanks a lot, did the expected behavior with asgResource.logicalId🙇‍♂️

I will keep this open (and change title) so we can use this as an example for the cfn-init support, okay?

okay, thanks.

Related: #777

It seems that logicalId is no longer a property for resources. @eladb what is the current method to fetch the logical ID? It need it for the same purpose as the OP.

For future reference, this is how you can get the logical ID for the autoscaling group:

const autoScalingGroupCfn = <CfnAutoScalingGroup> autoscalingGroup.node.tryFindChild('ASG');
console.log(autoScalingGroupCfn.logicalId);

Hello, any workaround to make cfn-init (AWS::CloudFormation::Init:) work with CDK?

Is there a way to add the CloudFormation directly when CDK does not support a feature like this?

Was this page helpful?
0 / 5 - 0 ratings