Description
Describe the feature
Construct: AutoScalingGroup : 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:
aws-cdk/packages/aws-cdk-lib/aws-ec2/lib/cfn-init.ts
Lines 156 to 166 in 2718bc4
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, when using the signals
property (for the ASG):
- 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
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
- 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 CLI version
2.1019.2 (build c29855e)
Environment details (OS name and version, etc.)
WSL (Ubuntu), on Windows 10