amazon-web-serviceslambdaparametersaws-cloudformationssm

How to fetch latest version of SSM parameter on Cloudformation Yaml file


I have a Cloudformation Yaml file where it creates a lot of resources, one of them is an EC2 with Windows. My problem is I need to have this automated selection of the latest AMI, I made a Lambda function where it retrieves newest AMI ID and stores in an SSM parameter, then using the Cloudformation with YAML template I can access to ssm using the following command

{{resolve:ssm:parameter_name:version_number_int}}

but my problem is that it's not always the same version number, it will be changed everytime when there will be a new AMI, is there any method where I can write to get always the latest version? or to stop versioning or anything?

Thanks.


Solution

  • SSM Parameter Store provides public parameters to retrieve latest AMIs.

    # Use public Systems Manager Parameter
    Parameters:
      LatestAmiId:
        Type: 'AWS::SSM::Parameter::Value<AWS::EC2::Image::Id>'
        Default: '/aws/service/ami-windows-latest/Windows_Server-2019-English-Full-Base'
    
    Resources:
     Instance:
        Type: 'AWS::EC2::Instance'
        Properties:
          ImageId: !Ref LatestAmiId