Aws-cdk: Add support for AWS::CloudFormation::Init

Created on 26 Sep 2018  路  15Comments  路  Source: aws/aws-cdk

TypeScript CDK 0.9.2

How to write AWS::CloudFormation::Init metadata with AWS Construct Library for cfn-init ?
I want to add AWS::CloudFormation::Init metadata to AutoScalingGroup.

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

Most helpful comment

Still relevant

All 15 comments

We don't have this modeled as an L2 right now.

You can instantiate a cdk.Resource and pass in the properties that make up an Init resource, and add the appropriate call to the ASGs userData yourself.

It should include support for environment variables, by writing them to a script in /etc/profile.d.

Need to do research to see if all ways of starting a program source the files there well enough.

I'm trying to convert this template with cdk.
atlassian-aws-deployment / templates / JiraDataCenter.template.yaml

environment variables are in l.1008

Any updates/likely merge time on https://github.com/awslabs/aws-cdk/pull/792 , or at least a worked example of how to add AWS::CloudFormation::Init to an autoscaling.AutoScalingGroup?

The following template shows that it should be added to the AWS::AutoScaling::LaunchConfiguration in the CloudFormation:

I see that https://github.com/awslabs/aws-cdk/issues/606 makes reference to being able to find/modify children since 0.10.0. Following the syntax there.. it looks like the following should work for us..:

// Ref: https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-init.html
// Ref: https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/example-templates-autoscaling.html
const asgLaunchConfig = asg.findChild('LaunchConfig') as autoscaling.cloudformation.LaunchConfigurationResource;
asgLaunchConfig.addOverride("Metadata", {
    "AWS::CloudFormation::Init": {
        "FOO": "BAR",
    }
});

Running cdk synth, I get the following, which implies it is working as expected:

AutoScalingGroupLaunchConfigDEEB160C:
  Type: 'AWS::AutoScaling::LaunchConfiguration'
  ..snip..
  Metadata:
      'AWS::CloudFormation::Init':
          FOO: BAR

Closed PR for now. Reopen as needed.

Sad that that PR didn鈥檛 land.. :(

We'll get there. It's an important feature... Just got too much on our table RN...
Happy to take contributions of course if you are interested to pick it up

Still relevant

I haven't taken a stab at an L2 construct, but I put together a template I might be able to refactor out into something easier. I needed a shell script to be put onto the machine and be executed. The construct for Init will hopefully make it easier to "just execute this script at launch time". So I have scoped my solution to just the file / command resources, and a little bit of a fairly robust userdata script (that requires the ASG and Launch config for both cfn-init and cfn-signal).

I put it up on a gist instead of here. Hopefully this is helpful if someone writes the L2, to see how I tackled this use case. I'd like to spend time on an L2 but not sure I'll have the time for a while.

https://gist.github.com/brettswift/6e48a70d808a28614438520682459f0c

I updated the gist above adding in a builder class to help us reduce code re-use. Definitely not feature complete for cfn-init, however it's one take to solve just our use case still. If this is a strategy that people like, I might have time to contribute to a PR into CDK.

any update on this? @rix0rrr

Yeah, I'd really like to see this feature ship soon. Using escape hatches to accomplish this task is very cumbersome.

Please, keep things going on this @rix0rrr

With regard to another issue about adding metadata, this seems to be a work around at the moment: Modifying the AWS CloudFormation resource behind AWS constructs

I saw this is documented on the AWS CDK API under Configuring Instances using CloudFormation Init (cfn-init)
but it doesn't seem to work ( _undefined init property on instanceProps_ ) in version @1.60.0

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mirazmamun picture mirazmamun  路  3Comments

pepastach picture pepastach  路  3Comments

NukaCody picture NukaCody  路  3Comments

eladb picture eladb  路  3Comments

artyom-melnikov picture artyom-melnikov  路  3Comments