amazon-web-servicesamazon-ec2aws-cloudformationinfrastructure-as-codeaws-auto-scaling

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?


Solution

  • !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