Cloudformation YAML file pass parameters

We have a simple cloud formation file in a YAML format. There are couple of parameters that need to be passed to this template. We want to make sure the parameters that are being passed are based on whether it is a PROD/Test environment.

Here is the sample simple S3 upload file template

AWSTemplateFormatVersion: 2010-09-09
Metadata:
  ...
Parameters:
  S3BucketName:
    ...
    Type: String
  CorsIDRuleName:
    Default: DEVCorsRuleID
    Description: Cors ID Rule information for the s3 bucket to post
    Type: String      
Resources:
  S3B3SR4G:
    Type: 'AWS::S3::Bucket'
    Properties:
      BucketName: !Ref S3BucketName
      AccessControl: PublicRead
      CorsConfiguration:
        CorsRules:
        ...
         	Id: !Ref CorsIDRuleName
          MaxAge: '3600'


Now based on the environment, we have two parameter files that have to be used. For test we will use the test-config.json file and production we will use the prod-config.json file.

Sample test-config.json

[
{
"ParameterKey": "S3BucketName",
"ParameterValue": "mynewawesomebucket20191231"
},
{
"ParameterKey": "CorsIDRuleName",
"ParameterValue": "myawesomecorsruleid"
}
]

To run the stack from cloud formation we can run the command

aws cloudformation create-stack --stack-name startmyinstance  --template-body file://template1.yaml --parameters file:///S3Uploader/test-stack-s3-configuration.json

Few take aways:

  • I was not able to pass parameters from another yaml file
  • Template can be yaml, but when parameters are to be externalized then it is had to be from a JSON file only
  • Parameter file though in JSON had to follow some specific conventions for yaml file. In our example “ParameterKey”, “ParameterValue” had to be in the format for CF/YAML config. To pick up.
  • Sample code for this is at github

Startup template for cloud formation

If you are looking for some good startup templates for cloud formation, I found this repository from acloud.guru website a good starting place. 

The templates provided gives a startup template on how to spin an EC2 instance. Of course there have to be more building blocks to it. But to kickstart for a demo or a presentation on how to use cloud formation, start with the first template, but to see a snake definition. Here is a greater addition.