Zaid Afaneh
Zaid Afaneh

Reputation: 154

Use latest launch template version in AWS Cloudformation

I am writing a cloudformation (cf) template to provision an auto-scaling group, I prepared the launch template ahead of time and want to use the latest version.

I am getting the LaunchTemplateId as a parameter but I am not sure how to use the latest lunch template version.

my cf template looks like this:

--- 
AWSTemplateFormatVersion: 2010-09-09
Description: Create Auto Scaling Group with 2 min, 2 desired, 4 max
Parameters:
...
  TemplateID: 
    Description: Lunch Template for ASG EC2s 
    Type: String
    Default: lt-xxxxxxxxxxxxxxxx
Resources:  
  ASG:
    Type: AWS::AutoScaling::AutoScalingGroup
    Properties:
...
      LaunchTemplate:
        LaunchTemplateId: !Ref TemplateID
        Version: !GetAtt 
          - !Ref TemplateID
          - LatestVersionNumber
...

but I always get this linting error when I run taskcat for testing:

[ERROR ] : Linting detected issues in: /Users/zaidafaneh/Desktop/RealBlocks/repos/cloudformation-temaplates/templates/saas/asg.yml

[ERROR ] : line 23 [1010] [GetAtt validation of parameters] Invalid GetAtt TemplateID.LatestVersionNumber for resource ASG

I am thinking about using Lambda Custom Resource as a workaround but I feel it's too much. is there any way to do this through cloudformation?

Upvotes: 3

Views: 2073

Answers (2)

RenuzitV
RenuzitV

Reputation: 11

I'm a bit too late to the question, but after fiddling with CloudFormation I've found out that you can just specify a parameter for the Version attribute like so:

Parameters:
  LaunchTemplateVersion:
    Type: String
    Description: Version number of the launch template

Resources:
  EC2:
    Type: AWS::EC2::Instance
    Properties:
      LaunchTemplate:
        LaunchTemplateId: # Replace with your launch template ID
        Version: !Ref LaunchTemplateVersion

and the nice part is that you don't even need to specify a value for LaunchTemplateVersion! AWS seems to just use the latest (or default) version of your EC2 template. I cannot find the resource to back this answer up, so this is a just 'trust me bro' solution.

EDIT: to use this within the AWS cli, do:

aws cloudformation create-stack --stack-name #YOUR_STACK_NAME# --template-url #YOUR_TEMPLATE# --parameters ParameterKey="LaunchTemplateVersion",ParameterValue=

EDIT2: aws uses the default version, not the latest version. So remember to change that in your aws configuration.

Upvotes: 1

Paolo
Paolo

Reputation: 26265

!GetAtt only works for resources created in the template, you can't use it to get a property for a parameter.

To achieve the desired workflow, you'll have to either:

  1. Create the launch template in the same template (then you can use !GetAtt as you are doing now)
  2. Pass in the version number as an additional parameter.

If the launch template was created with another CloudFormation template, then there is a third option:

  1. Export the launch template resource from the other template and import it in your template; then you'll be able to use !GetAtt

Upvotes: 3

Related Questions