1

以下代码正在工作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())

OUT-PUT-RESULT 此结果在 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"
    }
}
}
4

1 回答 1

2

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

所以这样的事情应该有效:

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

或者

    "MyS3bucket": s3.Bucket(
         "MybucketName",
         AccessControl=s3.PublicRead,
         Condition="OneEqualsFoo"
   )
于 2015-02-03T23:02:04.970 回答