Mappings in Cloudformation Template

The optional Mappings section of cloudformation template can be used to declare variables. It is like python dictionary. You use the “FindInMap” intrinsic function to retrieve values. For e.g.

Mappings:
  Function:
    SocialMediaMLFunction:
      S3Bucket: solutions
      S3Key: ai-driven-social-media-dashboard/v1.0.0/socialmediafunction.zip
    AddTriggerForFunction:
      S3Bucket: solutions
      S3Key: ai-driven-social-media-dashboard/v1.0.0/addtriggerfunction.zip
  Code:
    EC2Twitter:
      S3Bucket: solutions
      S3Key: ai-driven-social-media-dashboard/v1.0.0/ec2_twitter_reader.tar

If you want to refer to socialmediafunction.zip file along with it’s path, then use…

S3Key: !FindInMap [ Function, SocialMediaMLFunction, S3Key]

And this statement will generate the URL that can be used to download the file…

EC2TwitterCode: !Join [”, [‘https://s3.’, !Ref ‘AWS::Region’, ‘.amazonaws.com/’, !Join [‘-‘, [!FindInMap [ Code, EC2Twitter, S3Bucket], !Ref ‘AWS::Region’]], ‘/’, !FindInMap [ Code, EC2Twitter, S3Key]]]
 
The output will look something like this…

https://s3.us-east-1.amazonaws.com/solutions-us-east-1/ai-driven-social-media-dashboard/v1.0.0/ec2_twitter_reader.tar
_____

Here is another example:

Value: !FindInMap [RegionAndInstanceTypeToAMIID, !Ref “AWS::Region”, !Ref EnvironmentType]

If your current region is us-east-1 and if the user has selected “test” environment as a parameter while creating the template, then the value returned will be “ami-8ff710e2” from this mapping:

 Mappings:
    RegionAndInstanceTypeToAMIID:
      us-east-1:
        test: “ami-8ff710e2”
        prod: “ami-f5f41398”
      us-west-2:
        test: “ami-eff1028f”
        prod: “ami-d0f506b0”

Powered by WPeMatico