如何遍历CloudFormation模板中的值 [英] How to loop through values in a CloudFormation template
问题描述
我试图在AWS CloudFormation模板中传递逗号分隔的参数列表,并基于这些值创建多个Amazon S3存储桶.
I am trying to pass a list of comma separated parameters in an AWS CloudFormation template and create multiple Amazon S3 buckets based on those values.
我有一个要求,我将传递逗号分隔的国家/地区名称列表,然后CloudFormation模板将构建许多S3存储桶(基于传入参数的国家/地区名称).
I have a requirement where I will be passing a comma separated- list of country names and then the CloudFormation template would build that many S3 buckets (based on the names of countries passed in parameters).
例如,如果我在参数中传递 fr,us,gb
,则堆栈应创建 fr_myprod_bucket
, us_myprod_bucket
,gb_myprod_bucket
.
For example, if I pass fr,us,gb
in a parameter, the stack should create fr_myprod_bucket
, us_myprod_bucket
, gb_myprod_bucket
.
我知道CloudFormation中没有for循环,所以不确定如何实现此目标吗?
I know there is no for loop in CloudFormation, so not sure how I can achieve this?
推荐答案
https://palletsprojects.com/p/jinja/是用于向CloudFormation模板添加for循环的另一个选项.您将需要先将Jinja模板呈现给它,然后再传递给CloudFormation,因为CloudFormation本身当前无法处理Jinja模板.
https://palletsprojects.com/p/jinja/ is another option for adding for-loops to CloudFormation templates. You will need to render the Jinja template before passing it to CloudFormation, as CloudFormation itself cannot currently process Jinja templates.
{% for country in ["fr", "us", "gb"] %}
{{country}}_myprod_bucket:
Type: AWS::S3::Bucket
{% endfor %}
Jinja代码段将产生的渲染结果:
Rendering that Jinja snippet would produce:
fr_myprod_bucket:
Type: AWS::S3::Bucket
us_myprod_bucket:
Type: AWS::S3::Bucket
gb_myprod_bucket:
Type: AWS::S3::Bucket
这篇关于如何遍历CloudFormation模板中的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!