매핑 - AWS CloudFormation

매핑

Mappings 섹션(선택 사항)은 키를 해당하는 명명된 값 세트와 맞춥니다. 예를 들어 리전에 따라 값을 설정하려면 리전 이름을 키로 사용하고 각각의 특정 리전에 대해 지정할 값을 포함하는 매핑을 생성할 수 있습니다. Fn::FindInMap 내장 함수를 사용하여 맵에서 값을 불러올 수 있습니다.

Mappings 섹션에는 파라미터, 가상 파라미터 또는 내장 함수를 포함할 수 없습니다.

구문

Mappings 섹션은 키 이름 Mappings로 이루어집니다. 매핑의 키는 문자열이어야 합니다. 값은 String이나 List 유형이 될 수 있습니다. 다음 예제는 Mapping01(논리명)이라는 매핑 하나가 있는 Mappings 섹션을 보여줍니다.

매핑 내에서 각 맵은 키이며 그 뒤에는 다른 매핑이 이어집니다. 키는 이름-값 페어의 맵이어야 하며 매핑 내에서 고유해야 합니다.

JSON

"Mappings" : { "Mapping01" : { "Key01" : { "Name" : "Value01" }, "Key02" : { "Name" : "Value02" }, "Key03" : { "Name" : "Value03" } } }

YAML

Mappings: Mapping01: Key01: Name: Value01 Key02: Name: Value02 Key03: Name: Value03

예제

기본 매핑

다음 예제에서는 Mappings 맵을 사용하는 RegionMap 매핑을 보여줍니다. 이 매핑에는 단일 문자열 값을 포함하는 이름-값 페어로 매핑된 다섯 개 키가 들어 있습니다. 키는 리전 이름입니다. 각 이름-값 페어는 키로 표현되는 리전의 HVM64 AMI용 AMI ID입니다.

이름-값 페어에는 이름(이 예제에서는 HVM64)과 값이 지정됩니다. 값에 이름을 지정하여 두 개 이상의 값 세트를 키로 매핑할 수 있습니다.

JSON

"Mappings" : { "RegionMap" : { "us-east-1" : { "HVM64" : "ami-0ff8a91507f77f867"}, "us-west-1" : { "HVM64" : "ami-0bdb828fd58c52235"}, "eu-west-1" : { "HVM64" : "ami-047bb4163c506cd98"}, "ap-southeast-1" : { "HVM64" : "ami-08569b978cc4dfa10"}, "ap-northeast-1" : { "HVM64" : "ami-06cd52961ce9f0d85"} } }

YAML

Mappings: RegionMap: us-east-1: "HVM64": "ami-0ff8a91507f77f867" us-west-1: "HVM64": "ami-0bdb828fd58c52235" eu-west-1: "HVM64": "ami-047bb4163c506cd98" ap-southeast-1: "HVM64": "ami-08569b978cc4dfa10" ap-northeast-1: "HVM64": "ami-06cd52961ce9f0d85"

여러 값을 사용하여 매핑

다음 예제에는 명명된 HVM64와 HVMG2, 이렇게 두 가지 값 세트로 매핑되는 리전 키가 사용됩니다.

JSON

"RegionMap" : { "us-east-1" : {"HVM64" : "ami-0ff8a91507f77f867", "HVMG2" : "ami-0a584ac55a7631c0c"}, "us-west-1" : {"HVM64" : "ami-0bdb828fd58c52235", "HVMG2" : "ami-066ee5fd4a9ef77f1"}, "eu-west-1" : {"HVM64" : "ami-047bb4163c506cd98", "HVMG2" : "ami-0a7c483d527806435"}, "ap-northeast-1" : {"HVM64" : "ami-06cd52961ce9f0d85", "HVMG2" : "ami-053cdd503598e4a9d"}, "ap-southeast-1" : {"HVM64" : "ami-08569b978cc4dfa10", "HVMG2" : "ami-0be9df32ae9f92309"} }

YAML

RegionMap: us-east-1: HVM64: ami-0ff8a91507f77f867 HVMG2: ami-0a584ac55a7631c0c us-west-1: HVM64: ami-0bdb828fd58c52235 HVMG2: ami-066ee5fd4a9ef77f1 eu-west-1: HVM64: ami-047bb4163c506cd98 HVMG2: ami-0a7c483d527806435 ap-northeast-1: HVM64: ami-06cd52961ce9f0d85 HVMG2: ami-053cdd503598e4a9d ap-southeast-1: HVM64: ami-08569b978cc4dfa10 HVMG2: ami-0be9df32ae9f92309

매핑에서 값 반환

Fn::FindInMap 함수를 사용하여 지정한 키에 따라 명명된 값을 반환할 수 있습니다. 다음 예제 템플릿에는 FindInMap 함수를 통해 ImageId 속성이 할당된 Amazon EC2 리소스가 포함되어 있습니다. FindInMap 함수는 키를 스택이 생성되는 리전(AWS::Region 가상 파라미터 사용)으로 지정하고 HVM64를 매핑할 값으로 지정합니다.

JSON

{ "AWSTemplateFormatVersion" : "2010-09-09", "Mappings" : { "RegionMap" : { "us-east-1" : {"HVM64" : "ami-0ff8a91507f77f867", "HVMG2" : "ami-0a584ac55a7631c0c"}, "us-west-1" : {"HVM64" : "ami-0bdb828fd58c52235", "HVMG2" : "ami-066ee5fd4a9ef77f1"}, "eu-west-1" : {"HVM64" : "ami-047bb4163c506cd98", "HVMG2" : "ami-0a7c483d527806435"}, "ap-northeast-1" : {"HVM64" : "ami-06cd52961ce9f0d85", "HVMG2" : "ami-053cdd503598e4a9d"}, "ap-southeast-1" : {"HVM64" : "ami-08569b978cc4dfa10", "HVMG2" : "ami-0be9df32ae9f92309"} } }, "Resources" : { "myEC2Instance" : { "Type" : "AWS::EC2::Instance", "Properties" : { "ImageId" : { "Fn::FindInMap" : [ "RegionMap", { "Ref" : "AWS::Region" }, "HVM64"]}, "InstanceType" : "m1.small" } } } }

YAML

AWSTemplateFormatVersion: "2010-09-09" Mappings: RegionMap: us-east-1: HVM64: ami-0ff8a91507f77f867 HVMG2: ami-0a584ac55a7631c0c us-west-1: HVM64: ami-0bdb828fd58c52235 HVMG2: ami-066ee5fd4a9ef77f1 eu-west-1: HVM64: ami-047bb4163c506cd98 HVMG2: ami-0a7c483d527806435 ap-northeast-1: HVM64: ami-06cd52961ce9f0d85 HVMG2: ami-053cdd503598e4a9d ap-southeast-1: HVM64: ami-08569b978cc4dfa10 HVMG2: ami-0be9df32ae9f92309 Resources: myEC2Instance: Type: "AWS::EC2::Instance" Properties: ImageId: !FindInMap [RegionMap, !Ref "AWS::Region", HVM64] InstanceType: m1.small

입력 파라미터 및 Fn::FindInMap

Fn::FindInMap 함수와 함께 입력 파라미터를 사용하여 맵에서 특정 값을 참조할 수 있습니다. 예를 들면 특정 AMI ID로 매핑하는 리전 및 환경 유형의 목록이 있다고 가정해 보겠습니다. 입력 파라미터(EnvironmentType)를 사용하여 스택에 사용되는 AMI ID를 선택할 수 있습니다. 리전을 확인하려면 AWS::Region 가상 파라미터를 사용합니다. 이 가상 파라미터는 스택이 생성되는 AWS 리전을 가져옵니다.

JSON

{ "Parameters" : { "EnvironmentType": { "Description": "The environment type", "Type": "String", "Default": "test", "AllowedValues": ["prod", "test"], "ConstraintDescription": "must be a prod or test" } }, "Mappings" : { "RegionAndInstanceTypeToAMIID" : { "us-east-1": { "test": "ami-8ff710e2", "prod": "ami-f5f41398" }, "us-west-2" : { "test" : "ami-eff1028f", "prod" : "ami-d0f506b0" }, ...other regions and AMI IDs... } }, "Resources" : { ...other resources... }, "Outputs" : { "TestOutput" : { "Description" : "Return the name of the AMI ID that matches the region and environment type keys", "Value" : { "Fn::FindInMap" : [ "RegionAndInstanceTypeToAMIID", { "Ref" : "AWS::Region" }, { "Ref" : "EnvironmentType" } ]} } } }

YAML

Parameters: EnvironmentType: Description: The environment type Type: String Default: test AllowedValues: - prod - test ConstraintDescription: must be a prod or test Mappings: RegionAndInstanceTypeToAMIID: us-east-1: test: "ami-8ff710e2" prod: "ami-f5f41398" us-west-2: test: "ami-eff1028f" prod: "ami-d0f506b0" ...other regions and AMI IDs... Resources: ...other resources... Outputs: TestOutput: Description: Return the name of the AMI ID that matches the region and environment type keys Value: !FindInMap [RegionAndInstanceTypeToAMIID, !Ref "AWS::Region", !Ref EnvironmentType]