Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EC2: Allow launch templates to use launch-time lookups of AMI functionality #24551

Closed
1 of 2 tasks
JimNero009 opened this issue Mar 9, 2023 · 14 comments · Fixed by #26273
Closed
1 of 2 tasks

EC2: Allow launch templates to use launch-time lookups of AMI functionality #24551

JimNero009 opened this issue Mar 9, 2023 · 14 comments · Fixed by #26273
Labels
@aws-cdk/aws-ec2 Related to Amazon Elastic Compute Cloud effort/small Small work item – less than a day of effort feature-request A feature should be added or improved. p1

Comments

@JimNero009
Copy link

JimNero009 commented Mar 9, 2023

Describe the feature

As discussed in the cloudformation docs on the resource, the ImageId here can be a directive to an SSM resolve statement. The effect of doing this is that the Launch Template, when used to launch new instances, looks up the AMI ID from a given SSM parameter and uses that. This avoids the need to re-deploy/renew a config every time you want to update an AMI.

Use Case

We want to script the build and deployment of an AMI without the need for a full redeploy of stacks, and this is a featured already offered by direct Cloudformation.

Note one can work around this by doing e.g.

  launch_template.node.default_child.add_override(
      "Properties.LaunchTemplateData.ImageId", f"resolve:ssm:{ami_ssm_parameter}"
  )

Proposed Solution

Not dug into the code as a whole, but the Cloudformation 'hack' is very simple, so I'd perhaps work at it from the point of view of a new parameter, exclusive with machine image, that simply updates the image id here with a resolve:ssm directive on the passed input string.

Other Information

No response

Acknowledgements

  • I may be able to implement this feature request
  • This feature might incur a breaking change

CDK version used

2.67.0

Environment details (OS name and version, etc.)

Ubuntu Kinetic, Python 3.10.7, Node 18.14.2

@JimNero009 JimNero009 added feature-request A feature should be added or improved. needs-triage This issue or PR still needs to be triaged. labels Mar 9, 2023
@github-actions github-actions bot added the @aws-cdk/aws-ec2 Related to Amazon Elastic Compute Cloud label Mar 9, 2023
@khushail
Copy link
Contributor

khushail commented Mar 9, 2023

Thanks for your feedback @JimNero009 . We actually have a MachineImage.fromSSMParameter() that allows you to pass SSM parameter for dynamic reference like that. Is this something you are looking for?

And please note we have two similar methods with slight difference. You will want to use fromSsmParameter() in this case as the other one has been deprecated. Let me know if you need further help.

@khushail khushail added response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days. and removed needs-triage This issue or PR still needs to be triaged. labels Mar 9, 2023
@JimNero009
Copy link
Author

JimNero009 commented Mar 10, 2023

Hi @khushail -- thanks for getting back to me!

Unless I'm grossly misunderstanding, I think the suggested methods you mention there aren't doing quite the same thing I'm after (in fact, I was using fromSSMParameter before).

The difference is that these methods resolve the AMI and substitute in the value at deploy time. What an EC2 Launch Template can do is evaluate that at launch time. This requires actually passing the literal string 'resolve:ssm:someparam' to the ImageID attribute of the LaunchTemplate. As far as I can see, this is currently not possible in the API -- the LaunchTemplate only accepts objects that implement the IMachineImage interface and use this to create an SSM reference in the resultant cloudformation, that is evaluated and substituted in at deploy time.

@github-actions github-actions bot removed the response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days. label Mar 10, 2023
@khushail
Copy link
Contributor

Oh, I see. I apologize for the confusion here.

Seems like you have figured out the implementation details. So it would be great if you could submit a PR and our team would be happy to review the same when available. Here is the contributing guide to get started. Thanks.

@JimNero009
Copy link
Author

No probs! I'm a bit CDK-ed out for now but will look to give it a stab in the coming weeks.

@tvb
Copy link

tvb commented Mar 29, 2023

Subscribing. In need for this feature too!

@milanschuuring
Copy link

Subscribed, could use this feature!

@gagipro
Copy link

gagipro commented Apr 8, 2023

+1

@BDeus
Copy link
Contributor

BDeus commented Apr 11, 2023

+1 it could be nice to have this new feature
https://docs.aws.amazon.com/autoscaling/ec2/userguide/using-systems-manager-parameters.html

@peterwoodworth peterwoodworth added p1 effort/small Small work item – less than a day of effort labels May 2, 2023
@peterwoodworth
Copy link
Contributor

peterwoodworth commented May 2, 2023

Thanks everyone for your feedback, but please refrain from posting +1 comments, as they clog up the discussion. We take feedback into account through thumbs up reactions on the original issue. I think the quickest way to see this implemented is through a contribution, I wouldn't guarantee this gets put on the near term roadmap

@pahud
Copy link
Contributor

pahud commented Jul 7, 2023

It does support!

Please check out the sample below:

    // generate a ssm parameter to store the AMI info
    const amiParameter = new ssm.CfnParameter(this, 'AmiParameter', {
      name: 'myLatestAmi',
      type: 'String',
      dataType: 'aws:ec2:image',
      value: 'ami-06ca3ca175f37dd66', // initial default AMI
    });

    const lt = new ec2.LaunchTemplate(this, 'LT', {
      instanceType: ec2.InstanceType.of(ec2.InstanceClass.BURSTABLE3, ec2.InstanceSize.LARGE),
      // specify a dummy image which will be overrided
      machineImage: ec2.MachineImage.latestAmazonLinux2023(),
    })

    const cfnlt = lt.node.tryFindChild('Resource') as CfnLaunchTemplate;
    // override the image with ssm parameter as described in
    // https://docs.aws.amazon.com/autoscaling/ec2/userguide/using-systems-manager-parameters.html
    cfnlt.addPropertyOverride('LaunchTemplateData.ImageId', 'resolve:ssm:myLatestAmi');

    const asg = new autoscaling.AutoScalingGroup(this, 'ASG', {
      launchTemplate: lt,
      desiredCapacity: 1,
      vpc,
    });

When you run cdk synth, make sure you see the ImageId as below:

 Type: AWS::EC2::LaunchTemplate
    Properties:
      LaunchTemplateData:
        ImageId: resolve:ssm:myLatestAmi

Let me know if it works for you.

@pahud pahud added the response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days. label Jul 7, 2023
@pahud
Copy link
Contributor

pahud commented Jul 7, 2023

And yes, we probably can create an API like

ec2.MachineImage.fromSsmResolve(parameterName);

which simply resolve to resolve:ssm:parameterName.

@vlad-guerreiro
Copy link

vlad-guerreiro commented Jul 7, 2023 via email

@github-actions github-actions bot removed the response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days. label Jul 7, 2023
@mergify mergify bot closed this as completed in #26273 Jul 10, 2023
mergify bot pushed a commit that referenced this issue Jul 10, 2023
…launch time (#26273)

Launch Template and EC2 instance support using SSM parameter to resolve the AMI ID at instance launch time(`resolve:ssm:parameter`) rather than the CFN deploy time(`CfnDynamicReference`). This PR introduces a new support for that.

- [Using SSM Parameter with Autoscaling and Launch Template](https://docs.aws.amazon.com/autoscaling/ec2/userguide/using-systems-manager-parameters.html)
- [Launch an instance using a Systems Manager parameter](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/finding-an-ami.html#using-systems-manager-parameter-to-find-AMI)

Remove `latestAmazonLinux2022()` from the integ test as it does not return any valid al2022 images anymore as described in #26274

Closes #24551

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
@github-actions
Copy link

⚠️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.

@pahud
Copy link
Contributor

pahud commented Jul 10, 2023

Hi all,

This issue should have been resolved with #26273 by introducing a new resolveSsmParameterAtLaunch() method. Let me know if it works for you.

bmoffatt pushed a commit to bmoffatt/aws-cdk that referenced this issue Jul 29, 2023
…launch time (aws#26273)

Launch Template and EC2 instance support using SSM parameter to resolve the AMI ID at instance launch time(`resolve:ssm:parameter`) rather than the CFN deploy time(`CfnDynamicReference`). This PR introduces a new support for that.

- [Using SSM Parameter with Autoscaling and Launch Template](https://docs.aws.amazon.com/autoscaling/ec2/userguide/using-systems-manager-parameters.html)
- [Launch an instance using a Systems Manager parameter](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/finding-an-ami.html#using-systems-manager-parameter-to-find-AMI)

Remove `latestAmazonLinux2022()` from the integ test as it does not return any valid al2022 images anymore as described in aws#26274

Closes aws#24551

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
@aws-cdk/aws-ec2 Related to Amazon Elastic Compute Cloud effort/small Small work item – less than a day of effort feature-request A feature should be added or improved. p1
Projects
None yet
Development

Successfully merging a pull request may close this issue.

9 participants