我使用对流层构建CloudFormation堆栈,并且只希望在配置中设置弹性负载均衡器ConnectionSettings属性,否则我不想指定它。
如果我将其设置为默认的None,则会得到一个错误,该值不属于预期的troposphere.elasticloadbalancing.ConnectionSettings类型。
我宁愿避免在调用中设置一个显式的默认设置,因为它可能会覆盖其他设置。
理想上,我希望能够向现有对象添加属性,例如:
lb = template.add_resource(elb.LoadBalancer(
...
))
if condition:
lb.add_attribute(ConnectionSettings = elb.ConnectionSettings(
...
))有办法做到这一点吗?
UPDATE:我使用一个隐藏的Troposphere方法实现了它,这个方法可以工作,但我不满意:
if condition:
lb.__setattr__('ConnectionSettings', elb.ConnectionSettings(
....
))我仍然对一种解决方案感兴趣,它不涉及从模块外部使用私有方法。
发布于 2015-11-27 01:22:46
主要的README没有使用这样的属性名称:
from troposphere import Template
import troposphere.elasticloadbalancing as elb
template = Template()
webelb = elb.LoadBalancer(
'ElasticLoadBalancer',
Listeners=[
elb.Listener(
LoadBalancerPort="80",
InstancePort="80",
Protocol="HTTP",
),
],
)
if True:
webelb.ConnectionSettings = elb.ConnectionSettings(IdleTimeout=30)
elasticLB = template.add_resource(webelb)
print(template.to_json())发布于 2015-11-26 06:10:29
因此,最大的问题是- ConnectionSettings的配置从何而来?在Cloudformation本身(和对流层)中,有条件、参数和AWS::NoValue Ref。我在堆栈RDS模板中大量使用了这种方法:
下面是参数:https://github.com/remind101/stacker/blob/master/stacker/blueprints/rds/base.py#L126 --这是条件:https://github.com/remind101/stacker/blob/master/stacker/blueprints/rds/base.py#L243 --这是以后在资源中使用它的方式--如果StorageType参数为空,我们使用AWS::NoValue,这是一个伪引用,用于不实际设置某些内容:(对不起,不能发布超过2个链接-转到同一个文件中的第304行,看看我在说什么)
但是,如果不使用参数,而是在python中执行所有条件,则可以执行类似的操作。类似于:
connection_setting = condition and <actual connection setting code> or Ref("AWS::NoValue")另一种选择是完全在python中完成,这基本上就是您的例子。希望这有助于解决这个问题,包括创建两个不同的ELB对象(一个具有连接设置,一个没有),然后选择一个带有python代码(如果是condtion)或cloudformation条件的对象。
发布于 2017-10-01 13:35:25
如果该值是在中已知的(即它的不是来自CloudFormation参数),则可以使用字典向对流层模板中的资源添加可选属性:
from troposphere import Template
import troposphere.elasticloadbalancing as elb
template = Template()
my_idle_timeout = 30 # replace this with your method for determining the value
my_elb_params = {}
if my_idle_timeout is not None:
my_elb_params['ConnectionSettings'] = elb.ConnectionSettings(
IdleTimeout=my_idle_timeout,
)
my_elb = template.add_resource(elb.LoadBalancer(
'ElasticLoadBalancer',
Listeners=[
elb.Listener(
LoadBalancerPort="80",
InstancePort="80",
Protocol="HTTP",
),
],
**my_elb_params,
))
print(template.to_json())如果该值来自CloudFormation参数,则需要创建一个Condition来测试参数的值,如果参数没有提供值,则使用Ref("AWS::NoValue"),例如:
from troposphere import Template, Parameter, Equals, Ref, If
import troposphere.elasticloadbalancing as elb
template = Template()
my_idle_timeout = template.add_parameter(Parameter(
"ElbIdleTimeout",
Description="Idle timeout for the Elastic Load Balancer",
Type="Number",
))
no_idle_timeout = "NoIdleTimeout"
template.add_condition(
no_idle_timeout,
Equals(Ref(my_idle_timeout), ""),
)
my_elb = template.add_resource(elb.LoadBalancer(
'ElasticLoadBalancer',
Listeners=[
elb.Listener(
LoadBalancerPort="80",
InstancePort="80",
Protocol="HTTP",
),
],
ConnectionSettings=If(
no_idle_timeout,
Ref("AWS::NoValue"),
elb.ConnectionSettings(
IdleTimeout=Ref(my_idle_timeout),
),
),
))
print(template.to_json())https://stackoverflow.com/questions/33929511
复制相似问题