Search code examples
aws-cloudformation

Using variables in cloudformation template


I am using SSM parameter value like this...

Parameters:
  LatestAmiId:
    Type: 'AWS::SSM::Parameter::Value<AWS::EC2::Image::Id>'
    Default: /aws/service/canonical/ubuntu/server/jammy/stable/current/arm64/hvm/ebs-gp2/ami-id

And using it in resources like this...

ImageId: !Ref LatestAmiId

This is working as expected.

But how do I use the default image ID directly in resources without declaring it in "Parameters"? I am trying to save those 3 lines :)


Solution

  • You can use the following to pull SSM Parameter values directly in your resources section:

    '{{resolve:ssm:parameter-name:version}}'
    

    For example:

    ImageId: '{{resolve:ssm:/aws/service/canonical/ubuntu/server/jammy/stable/current/arm64/hvm/ebs-gp2/ami-id:1}}'
    

    More info can be found here: https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/dynamic-references.html#dynamic-references-ssm