2016-04-22 8 views
0

私はテンプレート内で複数回繰り返す必要がある構造を持っていますが、唯一の違いは"Fn::Join":で使用できる変数です。CloudFormationテンプレート他のテンプレートをインポート

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

んCloudFormationがこれをサポートしているか、この単純な操作を行うために、いくつかのツールがあります:

私はこのような解決策を期待しますか?

答えて

2

troposphereを使用してください。 CloudFormationテンプレートを生成するPythonコードを書くことができます.JSONを直接書く必要はありません。必要に応じて、コメント、ループ、型チェック、さらに高度なプログラミング構造についてお話してください。

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テンプレート:このスニペットはbucket_namesリストをループで2つのS3バケットのテンプレートを生成します

{ 
    "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" 
     } 
    } 
} 

N.B. CloudFormationのスタック名の接頭辞とランダムな文字列の接尾辞のため、バケットの名前はfoobarではありません。実際の名前は、CloudFormationの出力セクションに表示されます。

その他の対流圏の例:https://github.com/cloudtools/troposphere/tree/master/examples

関連する問題