

# CloudFormation テンプレートの Mappings 構文
<a name="mappings-section-structure"></a>

オプションの `Mappings` セクションは、特定の条件または依存関係に基づいて値を指定するために使用できる key-value ペアを作成するのに役立ちます。

`Mappings` セクションの一般的なユースケースの 1 つは、スタックがデプロイされている AWS リージョン に基づいて値を設定することです。これは `AWS::Region` 疑似パラメータを使用することで実現できます。`AWS::Region` 擬似パラメータは、CloudFormation によってスタックが作成されるリージョンに解決される値です。擬似パラメータは、スタックの作成時に CloudFormation によって解決されます。

マップ内の値を取得するには、テンプレートの `Resources` セクション内で `Fn::FindInMap` 組み込み関数を使用できます。

## 構文
<a name="mappings-section-structure-syntax"></a>

`Mappings` セクションでは、次の構文を使用します。

### JSON
<a name="mappings-section-structure-syntax.json"></a>

```
"Mappings" : {
  "MappingLogicalName" : {
    "Key1" : {
      "Name" : "Value1"
    },
    "Key2" : {
      "Name" : "Value2"
    },
    "Key3" : {
      "Name" : "Value3"
    }
  }
}
```

### YAML
<a name="mappings-section-structure-syntax.yaml"></a>

```
Mappings: 
  MappingLogicalName: 
    Key1: 
      Name: Value1
    Key2: 
      Name: Value2
    Key3: 
      Name: Value3
```
+ `MappingLogicalName` はマッピングの論理名です。
+ マッピング内では、各マッピングは、別のマッピングが後に続くキーです。
+ キーは、名前と値のペアのマップであり、マッピング内で一意である必要があります。
+ 名前と値のペアは、ラベルとマッピングする値から構成されます。値に名前を付けることで、1 つのキーに対して値のセットを複数、対応付けることができます。
+ マッピングに含まれるキーは、リテラル文字列であることが必要です。
+ 値はタイプ `String` または `List` です。

**注記**  
`Mappings` セクションにパラメータ、擬似パラメータまたは組み込み関数を含めることはできません。  
マッピングの値が現在スタックで使用されていない場合は、マップのみを更新することはできません。リソースを追加、変更、または削除する変更を含める必要があります。

## 例
<a name="mappings-section-structure-examples"></a>

**Topics**
+ [基本的なマッピング](#mappings-section-structure-basic-example)
+ [複数の値のマッピング](#mappings-section-structure-multiple-values-example)
+ [マッピングから値を返します](#mappings-section-structure-return-value-example)
+ [入力パラメーターと `Fn::FindInMap`](#mappings-section-structure-input-parameter-example)

### 基本的なマッピング
<a name="mappings-section-structure-basic-example"></a>

以下に示したのは、`Mappings` というマップを持つ `RegionToInstanceType` セクションの例です。キーは 5 つ存在し、それぞれ単一の文字列値を含んだ名前と値のペアに対応しています。キーはリージョンの名前です。名前と値の各ペアは、キーで表されるリージョンで利用可能な T ファミリーのインスタンスタイプです。名前と値のペアには、名前 (この例では `InstanceType`) と値があります。

#### JSON
<a name="mappings-section-structure-basic-example.json"></a>

```
"Mappings" : {
  "RegionToInstanceType" : {
    "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" }
  }
}
```

#### YAML
<a name="mappings-section-structure-basic-example.yaml"></a>

```
Mappings:
  RegionToInstanceType:
    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
```

### 複数の値のマッピング
<a name="mappings-section-structure-multiple-values-example"></a>

次の例には、2 つの値のセットにマッピングされたリージョンキーがあります。1 つは `MyAMI1` という名前で、もう 1 つは `MyAMI2` という名前です。

**注記**  
これらの例に示す AMI ID は、デモンストレーション用のプレースホルダーです。可能な場合は、`Mappings` セクションの代わりに AWS Systems Manager パラメータへの動的参照を使用することを検討してください。使用する AMI が変更されるたびに、すべてのテンプレートが新しい ID で更新されることを回避するには、AWS Systems Manager パラメータを使用してスタックの作成時または更新時に最新の AMI ID を取得します。一般的に使用される AMI の最新バージョンは、Systems Manager のパブリックパラメータとしても利用できます。詳細については、「[動的参照を使用して他のサービスに格納されている値を取得する](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/dynamic-references.html)」を参照してください。

#### JSON
<a name="mappings-section-structure-multiple-values-example"></a>

```
"Mappings" : {
  "RegionToAMI" : {
    "us-east-1"        : { "MyAMI1" : "ami-12345678901234567", "MyAMI2" : "ami-23456789012345678" },
    "us-west-1"        : { "MyAMI1" : "ami-34567890123456789", "MyAMI2" : "ami-45678901234567890" },
    "eu-west-1"        : { "MyAMI1" : "ami-56789012345678901", "MyAMI2" : "ami-67890123456789012" },
    "ap-southeast-1"   : { "MyAMI1" : "ami-78901234567890123", "MyAMI2" : "ami-89012345678901234" },
    "ap-northeast-1"   : { "MyAMI1" : "ami-90123456789012345", "MyAMI2" : "ami-01234567890123456" }
  }
}
```

#### YAML
<a name="mappings-section-structure-multiple-values-example.yaml"></a>

```
Mappings:
  RegionToAMI:
    us-east-1:
      MyAMI1: ami-12345678901234567
      MyAMI2: ami-23456789012345678
    us-west-1:
      MyAMI1: ami-34567890123456789
      MyAMI2: ami-45678901234567890
    eu-west-1:
      MyAMI1: ami-56789012345678901
      MyAMI2: ami-67890123456789012
    ap-southeast-1:
      MyAMI1: ami-78901234567890123
      MyAMI2: ami-89012345678901234
    ap-northeast-1:
      MyAMI1: ami-90123456789012345
      MyAMI2: ami-01234567890123456
```

### マッピングから値を返します
<a name="mappings-section-structure-return-value-example"></a>

`Fn::FindInMap` 関数を使用すると、指定したキーによって名前付きの値を取得できます。次に示したのは、Amazon EC2 というリソースを含んだテンプレートの例です。このリソースの `InstanceType` プロパティを、`FindInMap` 関数によって割り当てています。`FindInMap` 関数で、(`AWS::Region` 擬似パラメータを使用して) スタックを作成する AWS リージョン としてキーを指定し、マッピング先の値の名前として `InstanceType` を指定しています。`ImageId` は Systems Manager パラメータを使用して、最新の Amazon Linux 2 AMI を動的に取得します。疑似 Parameters の詳細については、「[擬似パラメータを使用して AWS 値を取得する](pseudo-parameter-reference.md)」を参照してください。

#### JSON
<a name="mappings-section-structure-return-value-example.json"></a>

```
{
  "AWSTemplateFormatVersion" : "2010-09-09",
  "Mappings" : {
    "RegionToInstanceType" : {
      "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" }
    }
  },
  "Resources" : {
    "myEC2Instance" : {
      "Type" : "AWS::EC2::Instance",
      "Properties" : {
        "ImageId" : "{{resolve:ssm:/aws/service/ami-amazon-linux-latest/amzn2-ami-hvm-x86_64-gp2}}",
        "InstanceType" : { "Fn::FindInMap" : [ "RegionToInstanceType", { "Ref" : "AWS::Region" }, "InstanceType" ]}
      }
    }
  }
}
```

#### YAML
<a name="mappings-section-structure-return-value-example.yaml"></a>

```
AWSTemplateFormatVersion: 2010-09-09
Mappings: 
  RegionToInstanceType: 
    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
Resources: 
  myEC2Instance: 
    Type: AWS::EC2::Instance
    Properties: 
      ImageId: '{{resolve:ssm:/aws/service/ami-amazon-linux-latest/amzn2-ami-hvm-x86_64-gp2}}'
      InstanceType: !FindInMap [RegionToInstanceType, !Ref 'AWS::Region', InstanceType]
```

### 入力パラメーターと `Fn::FindInMap`
<a name="mappings-section-structure-input-parameter-example"></a>

次のサンプルテンプレートは、複数のマッピングを使用して EC2 インスタンスを作成する方法を示しています。テンプレートはネストされたマッピングを使用して、ターゲットの AWS リージョン と環境タイプ (`Dev` または `Prod`) に基づいて適切なインスタンスタイプとセキュリティグループを自動的に選択します。また、Systems Manager パラメータを使用して、最新の Amazon Linux 2 AMI を動的に取得します。

#### JSON
<a name="mappings-section-structure-input-parameter-example.json"></a>

```
{
  "AWSTemplateFormatVersion" : "2010-09-09",
  "Parameters" : {
    "EnvironmentType" : {
      "Description" : "The environment type (Dev or Prod)",
      "Type" : "String",
      "Default" : "Dev",
      "AllowedValues" : [ "Dev", "Prod" ]
    }
  },
  "Mappings" : {
    "RegionAndEnvironmentToInstanceType" : {
      "us-east-1"        : { "Dev" : "t3.micro", "Prod" : "c5.large" },
      "us-west-1"        : { "Dev" : "t2.micro", "Prod" : "m5.large" }
    },
    "RegionAndEnvironmentToSecurityGroup" : {
      "us-east-1"        : { "Dev" : "sg-12345678", "Prod" : "sg-abcdef01" },
      "us-west-1"        : { "Dev" : "sg-ghijkl23", "Prod" : "sg-45678abc" }
    }
  },
  "Resources" : {
    "Ec2Instance" : {
      "Type" : "AWS::EC2::Instance",
      "Properties" : {
        "ImageId" : "{{resolve:ssm:/aws/service/ami-amazon-linux-latest/amzn2-ami-hvm-x86_64-gp2}}",
        "InstanceType" : { "Fn::FindInMap": [ "RegionAndEnvironmentToInstanceType", { "Ref": "AWS::Region" }, { "Ref": "EnvironmentType" } ]},
        "SecurityGroupIds" : [{ "Fn::FindInMap" : [ "RegionAndEnvironmentToSecurityGroup", { "Ref" : "AWS::Region" }, { "Ref" : "EnvironmentType" } ]}]
      }
    }
  }
}
```

#### YAML
<a name="mappings-section-structure-input-parameter-example.yaml"></a>

```
AWSTemplateFormatVersion: 2010-09-09
Parameters:
  EnvironmentType: 
    Description: The environment type (Dev or Prod)
    Type: String
    Default: Dev
    AllowedValues: 
      - Dev
      - Prod
Mappings:
  RegionAndEnvironmentToInstanceType:
    us-east-1: 
      Dev: t3.micro
      Prod: c5.large
    us-west-1: 
      Dev: t2.micro
      Prod: m5.large
  RegionAndEnvironmentToSecurityGroup: 
    us-east-1: 
      Dev: sg-12345678
      Prod: sg-abcdef01
    us-west-1: 
      Dev: sg-ghijkl23
      Prod: sg-45678abc
Resources:
  Ec2Instance:
    Type: AWS::EC2::Instance
    Properties:
      ImageId: '{{resolve:ssm:/aws/service/ami-amazon-linux-latest/amzn2-ami-hvm-x86_64-gp2}}'
      InstanceType: !FindInMap [RegionAndEnvironmentToInstanceType, !Ref 'AWS::Region', !Ref EnvironmentType]
      SecurityGroupIds:
        - !FindInMap [RegionAndEnvironmentToSecurityGroup, !Ref 'AWS::Region', !Ref EnvironmentType]
```

## 関連リソース
<a name="mappings-section-related-resources"></a>

これらの関連トピックは、`Fn::FindInMap` 関数を使用するテンプレートを作成する際に役立ちます。
+ [https://docs.aws.amazon.com/AWSCloudFormation/latest/TemplateReference/intrinsic-function-reference-findinmap.html](https://docs.aws.amazon.com/AWSCloudFormation/latest/TemplateReference/intrinsic-function-reference-findinmap.html)
+ [Fn::FindInMap の機能強化](https://docs.aws.amazon.com/AWSCloudFormation/latest/TemplateReference/intrinsic-function-reference-findinmap-enhancements.html)
+ [https://docs.aws.amazon.com/AWSCloudFormation/latest/TemplateReference/intrinsic-function-reference-sub.html](https://docs.aws.amazon.com/AWSCloudFormation/latest/TemplateReference/intrinsic-function-reference-sub.html)