[英]Convert CloudFormation template (yaml) to cdk python code
[英]Cloudformation YAML custom variable
我正在嘗試在 AWS Cloudformation YAML 文件中實現類似於下面的內容:
AWSTemplateFormatVersion: 2010-09-09
testAttribute = "test"
Resources:
Lambda:
Type: AWS::Lambda::Function
Properties:
Runtime: python3.7
Role: !GetAtt iam.Arn
MemorySize: 128
Timeout: 10
Handler: lambda_function.lambda_handler
FunctionName: "testName"+${testAttribute}
Description: 'This is my lambda'
Code:
S3Bucket: myBucket
S3Key: "lambda/testName"+${testAttribute}+".zip"
我知道上面的內容不太正確,但是在搜索如何實現它時找不到好的答案。 有人對此事有指導嗎?
這取決於用例,但如果“變量”為 static 並且您在部署堆棧時不需要更改它,我建議使用映射部分的替代解決方案。
這允許您定義一些 static 值而無需在部署堆棧時發送它們(您將擁有更清晰的部署命令,並且邏輯將在模板端而不是部署端)。
在這種情況下,我使用帶有映射的!Sub intrinsic function(您可以設置多個變量以使用:Sub 替換):
AWSTemplateFormatVersion: 2010-09-09
Mappings:
attributes:
lambda:
testAttribute: "test"
Resources:
Lambda:
Type: AWS::Lambda::Function
Properties:
Runtime: python3.7
Role: !GetAtt iam.Arn
MemorySize: 128
Timeout: 10
Handler: lambda_function.lambda_handler
FunctionName: !Sub
- "testName${attr}"
- {attr: !FindInMap [attributes, lambda, testAttribute]}
Description: 'This is my lambda'
Code:
S3Bucket: myBucket
S3Key: !Sub
- "lambda/testName${attr}.zip"
- {attr: !FindInMap [attributes, lambda, testAttribute]}
注意:映射具有強制性的三級嵌套,在設計解決方案時請考慮到這一點
您可以使用具有默認值的Parameters ,然后在模板中使用Sub :
AWSTemplateFormatVersion: 2010-09-09
Parameters:
testAttribute:
Type: String
Default: test
Resources:
Lambda:
Type: AWS::Lambda::Function
Properties:
Runtime: python3.7
Role: !GetAtt iam.Arn
MemorySize: 128
Timeout: 10
Handler: lambda_function.lambda_handler
FunctionName: !Sub "testName${testAttribute}"
Description: 'This is my lambda'
Code:
S3Bucket: myBucket
S3Key: !Sub "lambda/testName${testAttribute}.zip"
[因錯別字而編輯]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.