2

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?

Paolo
  • 21,270
  • 6
  • 38
  • 69
Zaid Afaneh
  • 114
  • 1
  • 10

1 Answers1

2

!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
Paolo
  • 21,270
  • 6
  • 38
  • 69
  • According to the documentation [link](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-autoscaling-autoscalinggroup-launchtemplatespecification.html#cfn-autoscaling-autoscalinggroup-launchtemplatespecification-version) this is not possible: Specifying $Latest or $Default for the template version number is not supported. However, you can specify LatestVersionNumber or DefaultVersionNumber using the Fn::GetAtt intrinsic function. For more information, see Fn::GetAtt. – Zaid Afaneh Sep 12 '22 at 08:58
  • 1
    @ZaidAfaneh yes, that's right – Paolo Sep 12 '22 at 08:59
  • I tried it also and didn't work with the same message mentioned in the documentation – Zaid Afaneh Sep 12 '22 at 08:59
  • @ZaidAfaneh please read my updated answer – Paolo Sep 12 '22 at 08:59
  • I don't want to change my lunch template, is it even a best practice to create a different template each time the cf template is updated? I though about Option 2 but want to make sure if there is a way to always use the latest. – Zaid Afaneh Sep 12 '22 at 09:00
  • *I don't want to change anything on my template*.. then you won't be able to get this working. – Paolo Sep 12 '22 at 09:01
  • @ZaidAfaneh option 2 is the best one I think; how are you invoking the cloudformation template? it should be straightforward to run a CLI command to fetch the latest version of the launch template and feed that in when launching the stack – Paolo Sep 12 '22 at 09:05
  • @ZaidAfaneh no, that's not supported. is the launch template created by another CloudFormation stack by any chance? – Paolo Sep 12 '22 at 09:14
  • Yes through CLI. I believe fetching the version number first and feeding it to the create-stack command will do the job for me. Thank you – Zaid Afaneh Sep 12 '22 at 09:15
  • @ZaidAfaneh in which case I've now added a third option – Paolo Sep 12 '22 at 09:16
  • No, it was manually created. if it was created by another stack I would export the value of the latest version from there. – Zaid Afaneh Sep 12 '22 at 09:17