aws / aws/aws-cdk

[AutoScalingGroup]: Add flag to remove automatic `cfn-signal` addition to userdata when using `init` property

Open
#34,847 5 comments 0 reactions 0 assignees View on GitHub
@aws-cdk/aws-autoscaling effort/medium feature-request p2
Dominant language
TypeScript
Stars
12.9k
Forks
4.6k
Avg merge
2d 3h
Merged PRs (30d)
83

Description

### Describe the feature

Construct: [AutoScalingGroup](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_autoscaling.AutoScalingGroup.html) : [`init`](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_autoscaling.AutoScalingGroup.html#init) property

Currently, creating an ASG with the `init` property causes the attachment of both `cfn-signal` and `cfn-init` to the UserData property of the ASG: https://github.com/aws/aws-cdk/blob/2718bc4d6c6ead65fb43956239ddb62b9849215d/packages/aws-cdk-lib/aws-ec2/lib/cfn-init.ts#L156-L166

It would be nice to have some flexibility to pass a flag / config setting to enable just the addition of `cfn-init` without `cfn-signal`. I don't think you can achieve this through escape hatches currently, as the userdata is tokenized at synth time when using this construct.

Also, as per the CDK docs on [ASG signals](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_autoscaling-readme.html#signals), when using the `signals` property [(for the ASG)](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_autoscaling.AutoScalingGroup.html#signals):

- If you use CloudFormation Init support (described in the previous section), the appropriate call to cfn-signal is automatically added to the AutoScalingGroup's UserData.

However, removing the `signals` property will throw you an error if you use `init`, stating "Error: When applying CloudFormationInit, you must also configure signals by supplying 'signals' at instantiation time."

---

### Use Case

Us / other CDK users have a desire to manage calls to `cfn-signal` outside of user data directly (i.e. in custom application logic), and the current method forces us to call `cfn-signal` before our application is fully stood up. It would be better if we had more flexibility here can self-manage the call to `cfn-signal`.

Currently, not having the control over where `cfn-signal` is added causes our application to report to CloudFormation that it is finished before our application actually starts responding, causing the ASG to cycle out instances before our replacement instances are ready.

### Proposed Solution

We could extend the ASG's [`initOptions`](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_autoscaling.AutoScalingGroup.html#initoptions) property to include an optional setting that would disable the addition of `cfn-signal` command, i.e. `addSignalCommand` : `true | false`

### Other Information

I know there is a workaround here that you can use with escape hatches to manually apply the `AWS::CloudFormation::Init` metadata to the ASG without using the `init` property, by using `addMetadata` and adding `cfn-signal` directly. But this can get pretty verbose quickly because you don't have access to the CDK formatting for `init` (i.e. `InitPackage.yum('wget')`), instead needing to pass as a cfn object.

Workaround syntax below:

```
const asg = new autoscaling.AutoScalingGroup(this, 'ASG', {
// your ASG properties here
});

// Add CloudFormation Init metadata manually
const cfnAsg = asg.node.defaultChild as autoscaling.CfnAutoScalingGroup;
cfnAsg.addMetadata('AWS::CloudFormation::Init', {
configSets: {
default: ['config']
},
config: {
packages: {
yum: {
wget: []
}
},
commands: {
'000': {
command: 'echo "Init test complete" > /tmp/init-test.log'
}
}
}
});

userData = ... // define your userdata as you usually would

// Add cfn-init to launch template user data
userData.addCommands(
`/opt/aws/bin/cfn-init -v --region \${AWS::Region} --stack \${AWS::StackName} --resource ${cfnAsg.logicalId} -c default`
);
```

Having a direct config option would go a long way to simplify this.

### Acknowledgements

- [x] I may be able to implement this feature request
- [ ] This feature might incur a breaking change

### AWS CDK Library version (aws-cdk-lib)

aws-cdk-lib@2.202.0

### AWS CDK CLI version

2.1019.2 (build c29855e)

### Environment details (OS name and version, etc.)

WSL (Ubuntu), on Windows 10

Contributor guide

Open the contributing guide

Research direction

Start with packages/aws-cdk-lib/aws-ec2/lib/cfn-init.ts around the referenced cfn-init and cfn-signal user-data handling, then trace AutoScalingGroup's init and initOptions properties. Review how the signals property currently validates and configures the construct. Done means an init configuration can retain cfn-init while disabling automatic cfn-signal insertion, with the existing behavior preserved by default.

Written by the indexing model from the issue text.

Assessment

Tech stack
aws, typescript
Domain
cloud, infrastructure
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.