簡體   English   中英

CloudFormation模板導入其他模板

[英]CloudFormation template import other templates

我有一個需要在模板中重復多次的結構,唯一的區別是可以與"Fn::Join":一起使用的變量。

我期望這樣的解決方案:

   "Import" : [
      {
        "Path":"s3://...", 
        "Parameters":[
          {"Key":"name", "Value":"foobar"} 
        ]
   ]

CloudFormation是否支持此功能?是否有一些工具可以使此操作簡單?

使用對流層 它允許編寫生成CloudFormation模板的python代碼-不再需要直接編寫JSON。 如有必要,請向注釋,循環,類型檢查和更高級的編程結構打招呼。

該代碼段將通過循環bucket_names列表為2個S3存儲桶生成一個模板:

from troposphere import Output, Ref, Template
from troposphere.s3 import Bucket, PublicRead
t = Template()

# names of the buckets
bucket_names = ['foo', 'bar']

for bucket_name in bucket_names:
    s3bucket = t.add_resource(Bucket(bucket_name, AccessControl=PublicRead,))
    t.add_output(
        Output(
            bucket_name + "Bucket",
            Value=Ref(s3bucket),
            Description="Name of %s S3 bucket content" % bucket_name
        )
    )

print(t.to_json())

CloudFormation模板:

{
    "Outputs": {
        "barBucket": {
            "Description": "Name of bar S3 bucket content",
            "Value": {
                "Ref": "bar"
            }
        },
        "fooBucket": {
            "Description": "Name of foo S3 bucket content",
            "Value": {
                "Ref": "foo"
            }
        }
    },
    "Resources": {
        "bar": {
            "Properties": {
                "AccessControl": "PublicRead"
            },
            "Type": "AWS::S3::Bucket"
        },
        "foo": {
            "Properties": {
                "AccessControl": "PublicRead"
            },
            "Type": "AWS::S3::Bucket"
        }
    }
}

注意,由於CloudFormation為堆棧名稱加上前綴並為隨機字符串加上后綴,因此不會將存儲桶命名為foobar 實名可以在CloudFormation的輸出部分中看到。

更多對流層示例: https : //github.com/cloudtools/troposphere/tree/master/examples

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM