aws-cdk icon indicating copy to clipboard operation
aws-cdk copied to clipboard

Add support for AWS::CloudFormation::Init

Open taichi opened this issue 8 years ago • 17 comments

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.

taichi avatar Sep 26 '18 07:09 taichi

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.

rix0rrr avatar Sep 26 '18 08:09 rix0rrr

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.

rix0rrr avatar Oct 09 '18 09:10 rix0rrr

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

environment variables are in l.1008

taichi avatar Oct 09 '18 23:10 taichi

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?

  • https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-init.html
  • https://awslabs.github.io/aws-cdk/refs/_aws-cdk_aws-autoscaling.html#auto-scaling-group

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

  • https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/example-templates-autoscaling.html

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

0xdevalias avatar Nov 06 '18 04:11 0xdevalias

Closed PR for now. Reopen as needed.

eladb avatar Dec 19 '18 13:12 eladb

Sad that that PR didn’t land.. :(

0xdevalias avatar Dec 20 '18 07:12 0xdevalias

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

eladb avatar Dec 20 '18 09:12 eladb

Still relevant

eladb avatar Oct 02 '19 08:10 eladb

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

brettswift avatar Oct 07 '19 17:10 brettswift

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.

brettswift avatar Oct 15 '19 17:10 brettswift

any update on this? @rix0rrr

ywauto83 avatar Apr 14 '20 09:04 ywauto83

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

robertd avatar May 06 '20 17:05 robertd

Please, keep things going on this @rix0rrr

Zirkonium88 avatar Jun 04 '20 08:06 Zirkonium88

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

Zirkonium88 avatar Jun 05 '20 05:06 Zirkonium88

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

oscarnevarezleal avatar Aug 23 '20 12:08 oscarnevarezleal

⚠️COMMENT VISIBILITY WARNING⚠️

Comments on closed issues are hard for our team to see. If you need more assistance, please either tag a team member or open a new issue that references this one. If you wish to keep having a conversation with other community members under this issue feel free to do so.

github-actions[bot] avatar Aug 18 '21 13:08 github-actions[bot]

i tried to use this and got When applying CloudFormationInit, you must also configure signals by supplying 'signals' at instantiation time.

felipeloha avatar Aug 31 '21 16:08 felipeloha