Mappature - AWS CloudFormation

Le traduzioni sono generate tramite traduzione automatica. In caso di conflitto tra il contenuto di una traduzione e la versione originale in Inglese, quest'ultima prevarrà.

Mappature

La sezione Mappings opzionale abbina una chiave a un corrispondente set di valori denominati. Ad esempio, se vuoi impostare valori in base a una Regione, puoi creare una mappatura che usa il nome della Regione come chiave e contiene i valori da specificare per ogni Regione specifica. Utilizzi quindi la funzione intrinseca Fn::FindInMap per recuperare i valori in una mappa.

Non puoi includere parametri, pseudoparametri o funzioni intrinseche nella sezione Mappings.

Sintassi

La sezione Mappings è composta dal nome di chiave Mappings. Le chiavi nelle mappature devono essere stringhe letterali. I valori possono essere tipi String o List. L'esempio seguente mostra una sezione Mappings contenente una singola mappatura denominata Mapping01 (nome logico).

All'interno di una mappatura, ogni mappa è una chiave seguita da un'altra mappatura. La chiave deve essere una mappa di coppie nome-valore e univoca all'interno della mappatura.

JSON

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

YAML

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

Esempi

Mappatura di base

L'esempio seguente mostra una sezione Mappings con una mappa RegionMap, che contiene cinque chiavi mappate a coppie nome-valore contenenti singoli valori stringa. Le chiavi sono i nomi di Regione. Ogni coppia nome-valore è l'ID AMI per l'AMI HVM64 nella Regione rappresentata dalla chiave.

Le coppie nome-valore dispongono di un nome (HVM64 nell'esempio) e un valore. Se definisci i valori, puoi mappare più di un set di valori a una chiave.

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"

Mappatura con più valori

L'esempio seguente fa riferimento a chiavi di Regione mappate a due set di valori: uno denominato HVM64 e l'altro 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

Restituzione di un valore da una mappatura

Puoi utilizzare la funzione Fn::FindInMap per restituire un valore denominato in base a una chiave specificata. Il modello di esempio seguente contiene una risorsa Amazon EC2 la cui proprietà ImageId viene assegnata dalla funzione FindInMap. La FindInMap funzione specifica key come regione in cui viene creato lo stack (utilizzando lo AWS::Region pseudo parametro) e HVM64 come nome del valore a cui mappare.

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

Parametro di input e Fn::FindInMap

Puoi utilizzare un parametro di input con la funzione Fn::FindInMap per fare riferimento a un valore specifico in una mappa. Ad esempio, supponiamo che tu abbia un elenco di Regioni e tipi di ambiente mappati a un determinato ID AMI. Puoi selezionare l'ID AMI utilizzato dallo stack mediante un parametro di input (EnvironmentType). Per determinare la regione, utilizzate il parametro AWS::Region pseudo, che ottiene la AWS regione in cui create lo stack.

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]