CloudFormation Mappings

  CloudFormation

https://www.udemy.com/aws-cloudformation-master-class/learn/v4/t/lecture/8161736?start=0

Course Main Menu

Section 5 Main Menu

Mappings Overview

What are Mappings?

  • Mappings are fixed variables within your CF Template
  • They’re very handy to differentiate between different environments (dev vs. prod), Regions, AMI types, etc.
  • All the values are hardcoded within the template
  • Example:
    • Mappings:
      • Mapping01:
        • Key01:
          • Name: Value01
        • Key02
          • Name: Value02
    • RegionMap:
      • us-east-1:
        • “32”: “ami-6411e20d”
        • “64”: “ami-7a11e213”
      • us-west-1:
        • “32”: “ami-c9c7978c”
        • “64”: “ami-cfc7978a”

When to use Mappings vs. Parameters?

  • Mappings are great when you know in advance all the values that can be taken and that they can be deduced from variables such as:
    • Region
    • Availability Zone
    • AWS Account #
    • Environment (dev vs. prod)
    • Etc
  • They allow safer control over the template (avoid user errors?)
  • Use Parameters when the values are really user specific

Fn::FindInMap (Accessing Mapping Values)

https://www.udemy.com/aws-cloudformation-master-class/learn/v4/t/lecture/8161738?start=0

  • JSON: “Fn::FindInMap [MapName, TopLevelKey, SecondLevelKey]
  • YAML: “!FindInMap [MapName, TopLevelKey, SecondLevelKey]
    • ImageId: !FindInMap [RegionMap, !Ref “AWS::Region”, 32]

Mappings Hands On

Parameters:
  EnvironmentName:
    Description: Environment Name
    Type: String
    AllowedValues: [development, production]
    ConstraintDescription: must be development or production

Mappings:
  AWSRegionArch2AMI:
    us-east-1:
      HVM64: ami-6869aa05
    us-west-2:
      HVM64: ami-7172b611
    us-west-1:
      HVM64: ami-31490d51
    eu-west-1:
      HVM64: ami-f9dd458a
    eu-central-1:
      HVM64: ami-ea26ce85
    ap-northeast-1:
      HVM64: ami-374db956
    ap-northeast-2:
      HVM64: ami-2b408b45
    ap-southeast-1:
      HVM64: ami-a59b49c6
    ap-southeast-2:
      HVM64: ami-dc361ebf
    ap-south-1:
      HVM64: ami-ffbdd790
    us-east-2:
      HVM64: ami-f6035893
    sa-east-1:
      HVM64: ami-6dd04501
    cn-north-1:
      HVM64: ami-8e6aa0e3
  EnvironmentToInstanceType:
    development:
      instanceType: t2.micro
    # we want a bigger instance type in production
    production:
      instanceType: t2.small

Resources:
  EC2Instance:
    Type: AWS::EC2::Instance
    Properties:
      InstanceType: !FindInMap [EnvironmentToInstanceType, !Ref 'EnvironmentName', instanceType]
      # Note we use the pseudo parameter AWS::Region
      ImageId: !FindInMap [AWSRegionArch2AMI, !Ref 'AWS::Region', HVM64]

 

Pseudo Parameters in CloudFormation

  • AWS allows pseudo parameters in any CF template
  • These can be use at any time and are enabled by default
Reference Value Example Return Value
AWS::AccountId 1234567890
AWS::NotificationARNs [arn:aws:sns:us-east-1:123456789012:MyTopic]
AWS::NoValue Does not return a value
AWS::Region us-east-2
AWS::StackId arn:aws:sns:us-east-1:123456789012:stack/MyStack/1C2Fa620-982a-aae3-aff7-50e2416294e0
AWS::StackName MyStack

Quiz

When is a mapping a good choice against a parameter?

  • When you know all the possible values of a variable in advance
  • To give flexibility to the user

Mappings have to be completely exhaustive and thus they reduce the template flexibility.  Parameters are perfect for highly changing values, while mappings are great for stable values that won’t change in time, but can be determined ahead of time.

What are Pseudo Parameters

  • They’re pre-populated AWS parameters that you can reference in your template, to figure out the region, stack name, etc.
  • They’re user defined parameters that can only be named the way AWS wants it.

LEAVE A COMMENT