AWS CloudFromation Mapping - krdheeraj51/aws-labs GitHub Wiki

Overview:

Mappings in AWS CloudFormation allow you to create static key-value pairs that can be used to specify values based on certain conditions or dependencies. This is particularly useful for setting values based on the AWS Region where the stack is deployed or other environment-specific parameters.

Key Points:

  1. Structure:
  • Mappings are defined in the Mappings section of a CloudFormation template.
  • Each mapping consists of a logical name, followed by key-value pairs.
  • The keys and values must be literal strings.
  1. Usage:
  • Mappings are often used to manage region-specific configurations, such as AMI IDs, instance types, or other environment-specific settings.
  • You can retrieve values from a mapping using the Fn::FindInMap intrinsic function.
  1. Syntax:
Mappings:
  RegionMap:
    us-east-1:
      InstanceType: t2.micro
    us-west-1:
      InstanceType: t2.micro
    eu-west-1:
      InstanceType: t2.micro
    eu-north-1:
      InstanceType: t3.micro
    me-south-1:
      InstanceType: t3.micro
  1. Example Use Case:
  • Suppose you want to use different AMI IDs for different regions. You can define a mapping for AMI IDs and use Fn::FindInMap to retrieve the correct AMI ID based on the region.
Mappings:
  AMIMap:
    us-east-1:
      AMI: ami-0ff8a91507f77f867
    us-west-1:
      AMI: ami-0bdb828fd58c52235
    eu-west-1:
      AMI: ami-047bb4163c506cd98

Resources:
  MyEC2Instance:
    Type: "AWS::EC2::Instance"
    Properties:
      ImageId: !FindInMap [AMIMap, !Ref "AWS::Region", AMI]
      InstanceType: t2.micro

In this example, the AMIMap mapping defines AMI IDs for different regions. The MyEC2Instance resource uses the Fn::FindInMap function to retrieve the appropriate AMI ID based on the region where the stack is deployed.