

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# CloudFormation 範本 Mappings 語法
<a name="mappings-section-structure"></a>

選用 `Mappings` 區段可協助您建立索引鍵/值對，用於根據特定條件或依賴關係指定數值。

`Mappings` 區段的一個常見使用案例是根據部署堆疊的 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` 是映射的邏輯名稱。
+ 在映射內，每個映射都是後面接著另一個映射的索引鍵。
+ 該金鑰必須是名稱值對的映射和映射中唯一的映射。
+ 名稱/值對是一個標籤，也是要映射的值。藉由命名這些值，您可以將多組的值映射至一個索引鍵。
+ 映射中的索引鍵必須是常值字串。
+ 值可以是類型 `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` 區段，內含映射至包含單一字串值之名稱值對的五個索引鍵。索引鍵是區域名稱。每個名稱/數值組都是 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>

下列範例的區域索引鍵映射至兩組值：一個名為 `MyAMI1`，另一個名為 `MyAMI2`。

**注意**  
這些範例中顯示的 AMI ID 是用作示範用途的預留位置。盡可能考慮使用 AWS Systems Manager 參數的動態參考作為 `Mappings` 區段的替代方案。若要避免在每次要使用變更的 AMI 時，使用新 ID 更新所有範本，請在建立或更新堆疊時，使用 AWS Systems Manager 參數擷取最新的 AMI ID。Systems Manager 中也提供最新版本的常用 AMI 作為公有參數。如需詳細資訊，請參閱[使用動態參考取得存放在其他服務中的值](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 區域 的 （使用`AWS::Region`虛擬參數），並將索引鍵`InstanceType`指定為要映射的值名稱。`ImageId` 會使用 Systems Manager 參數動態擷取最新的 Amazon Linux 2 AMI。如需這些虛擬參數的詳細資訊，請參閱[使用虛擬參數取得 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 執行個體。範本使用巢狀映射，根據目標和環境類型 (`Dev` 或 ) 自動選取適當的執行個體類型 AWS 區域 和安全群組`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)