【问题标题】:AWS CloudFormation add condition in s3bucket using troposphereAWS CloudFormation 使用对流层在 s3bucket 中添加条件
【发布时间】:2015-02-04 07:02:04
【问题描述】:

以下代码适用于EC2,它在输出中添加条件语句,但对于S3,它不生成条件语句。我可以手动添加此语句以使其工作,但这有很多缺点。

from __future__ import print_function
from troposphere import (Template, Parameter, Ref, Condition, Equals)
from troposphere import ec2
from troposphere import s3

parameters = {
    "One": Parameter(
        "One",
         Type="String",
    ),
}

conditions = {
    "OneEqualsFoo": Equals(
        Ref("One"),
        "Foo"
  ),
}

resources = {
    "MyS3bucket": s3.Bucket(
         "MybucketName",
         Condition="OneEqualsFoo",
   ),

   "Ec2Instance": ec2.Instance(
       "Ec2Instance",
       Condition="OneEqualsFoo",
       ImageId="ami-1234556",
       InstanceType="t1.micro",
       KeyName="mykeypair",
       SecurityGroups=["default"],
   )
}

def template():
     t = Template()
 for p in parameters.values():
    t.add_parameter(p)
 for k in conditions:
    t.add_condition(k, conditions[k])
 for r in resources.values():
    t.add_resource(r)
return t
print(template().to_json())

输出结果 此结果缺少 S3 模板部分中的条件语句

{
"Conditions": {
    "OneEqualsFoo": {
        "Fn::Equals": [
            {
                "Ref": "One"
            },
            "Foo"
        ]
    }
},
"Parameters": {
    "One": {
        "Type": "String"
    }
},
"Resources": {
    "Ec2Instance": {
        "Condition": "OneEqualsFoo",
        "Properties": {
            "ImageId": "ami-1234556",
            "InstanceType": "t1.micro",
            "KeyName": "mykeypair",
            "SecurityGroups": [
                "default"
            ]
        },
        "Type": "AWS::EC2::Instance"
    },
    "MybucketName": {
        "Type": "AWS::S3::Bucket"
    }
}
}

【问题讨论】:

    标签: python-2.7 amazon-web-services troposphere


    【解决方案1】:

    我认为你需要在添加Conditions之前添加Properties

    所以这样的事情应该可以工作:

     "MyS3bucket": s3.Bucket(
             "MybucketName",
             Tags=s3.Tags(),
             Condition="OneEqualsFoo"
       ),
    

        "MyS3bucket": s3.Bucket(
             "MybucketName",
             AccessControl=s3.PublicRead,
             Condition="OneEqualsFoo"
       )
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-08-16
      • 2019-01-28
      • 2021-10-09
      • 2021-05-01
      • 1970-01-01
      • 2021-02-06
      • 2015-06-25
      • 1970-01-01
      相关资源
      最近更新 更多