[英]What is the purpose of creating "Mappings" in CloudFormation?
See the code below:请参阅下面的代码:
Mappings:
RegionMap:
us-east-1:
bucketname: s3bucketname-us-east-1
us-east-2:
bucketname: s3bucketname-us-east-2
us-west-1:
bucketname: s3bucketname-us-west-1
us-west-2:
bucketname: s3bucketname-us-west-2
ap-south-1:
bucketname: s3bucketname-ap-south-1
ap-northeast-2:
bucketname: s3bucketname-ap-northeast-2
ap-southeast-1:
bucketname: s3bucketname-ap-southeast-1
ap-southeast-2:
bucketname: s3bucketname-ap-southeast-2
ap-northeast-1:
bucketname: s3bucketname-ap-northeast-1
ca-central-1:
bucketname: s3bucketname-ca-central-1
eu-central-1:
bucketname: s3bucketname-eu-central-1
eu-west-1:
bucketname: s3bucketname-eu-west-1
eu-west-2:
bucketname: s3bucketname-eu-west-2
eu-west-3:
bucketname: s3bucketname-eu-west-3
eu-north-1:
bucketname: s3bucketname-eu-north-1
sa-east-1:
bucketname: s3bucketname-east-1
af-south-1:
bucketname: s3bucketname-south-1
ap-east-1:
bucketname: s3bucketname-east-1
ap-northeast-3:
bucketname: s3bucketname-ap-northeast-3
eu-south-1:
bucketname: s3bucketname-eu-south-1
me-south-1:
bucketname: s3bucketname-me-south-1
Resources:
StateS3Bucket:
Type: AWS::S3::Bucket
Properties:
BucketName: !Sub "cfntf-${AWS::Region}-${AWS::AccountId}"
There is more to this code however I've only included the relevant snippets for the question.这段代码还有更多内容,但是我只包含了问题的相关片段。
To summarize - why include mappings for bucketname when the bucketname is set directly, using region and account ID in the 'Resources' section?总结一下 - 为什么在直接设置存储桶名称时包含存储桶名称的映射,使用“资源”部分中的区域和帐户 ID?
There is use of the Fn::FindInMap
function which is used here as part of the ExecutorLambdaFunction
:此处使用Fn::FindInMap
函数作为ExecutorLambdaFunction
一部分:
ExecutorLambdaFunction:
Type: AWS::Lambda::Function
Properties:
FunctionName: myfunction
Handler: index.handler
Role: !GetAtt ExecutorLambdaServiceRole.Arn
Environment:
Variables:
BUCKET: !Ref StateS3Bucket
Code:
S3Bucket: !If
- S3Defined
- !Ref S3Bucket
- Fn::FindInMap:
- RegionMap
- !Ref AWS::Region
- bucketname
S3Key: !If
- S3Defined
- !Ref S3Key
- /app.zip
Runtime: python3.8
StateS3Bucket
is being built from the region and account ID but that has nothing to do with the Mappings
section. StateS3Bucket
是根据区域和帐户 ID 构建的,但这与Mappings
部分无关。
The mappings, in this case, are being used to provide the correct region-specific S3 bucket name for where the packaged source code for ExecutorLambdaFunction
exists - that is why Fn::FindInMap
is being used in the Lambda declaration.在这种情况下,映射用于为ExecutorLambdaFunction
的打包源代码所在的位置提供正确的特定于区域的 S3 存储桶名称 - 这就是在 Lambda 声明中使用Fn::FindInMap
原因。
Sometimes you may want to have dynamic values based on specific keys - the CloudFormation Mappings
section is the perfect solution to this problem.有时您可能希望拥有基于特定键的动态值 - CloudFormation Mappings
部分是此问题的完美解决方案。
Your packaged Lambda (the source code) is pointing to an S3 location in this case & as S3 buckets are region-specific, you need a way of getting the correct bucket name for the region that the stack is being deployed in.在这种情况下,您打包的 Lambda(源代码)指向 S3 位置,并且由于 S3 存储桶是特定于区域的,因此您需要一种方法来获取部署堆栈的区域的正确存储桶名称。
The code for the ExecutorLambdaFunction
must be loaded from the S3 bucket in the relevant region otherwise it won't work. ExecutorLambdaFunction
的代码必须从相关区域的 S3 存储桶中加载,否则将无法工作。
For example, trying to load the Lambda source code from a bucket in us-east-1
won't work when your Lambda is deployed in eu-west-2
(note: it will work if it is deployed in us-east-2
as while that is in another availability zone, it is still within the same region) .例如,当您的 Lambda 部署在eu-west-2
时,尝试从us-east-1
的存储桶加载 Lambda 源代码将不起作用(注意:如果它部署在us-east-2
,它将起作用虽然它位于另一个可用区,但它仍在同一区域内) 。
Even if you only intend to have your stack in one region only, it won't harm you to have a Mappings
section as it will future proof your CloudFormation template.即使您只打算在一个区域中使用堆栈,拥有一个Mappings
部分也不会损害您的Mappings
因为它将在未来证明您的 CloudFormation 模板。
If you don't have any region-specific infrastructure (quite rare but eg only creating IAM roles which are global & are set at an account-level), then you can not include one.如果您没有任何特定于区域的基础设施(非常罕见,但例如仅创建全局 IAM 角色并在账户级别设置),则您不能包含一个。
You'll spend much more time trying to add one in later than if you just spent an extra 3 minutes defining & using Mappings
as you write your template - you'll thank yourself if you later decide to deploy your stack in another region.与在编写模板时仅花费 3 分钟定义和使用Mappings
的额外时间相比,您将花费更多时间尝试添加一个Mappings
- 如果您稍后决定将堆栈部署到另一个区域,您会感谢自己。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.