如何在对流层的cloudformation中添加扩展策略?

时间:2015-02-23 21:06:12

标签: python amazon-web-services troposphere

我创建了一个python对流层脚本,整体上运行得非常好。我刚刚添加了一段新代码,用于向自动缩放组添加策略以发出警报。

代码如下:

tintScaleDown = autoscaling.ScalingPolicy("tintScaleDown1")
tintScaleDown.AdjustmentType = "ChangeInCapacity"
tintScaleDown.AutoScalingGroupName(Ref("tintASG"))
tintScaleDown.Cooldown = "900"
tintScaleDown.ScalingAdjustment = "1"
t.add_resource(tintScaleDown)

错误是:

追踪(最近一次通话):   文件“inPowered.py”,第395行,in     tintScaleDown.AutoScalingGroupName(参考文献( “tintASG”))   文件“/usr/lib/python2.7/site-packages/troposphere/init.py”,第79行, getattr     引发AttributeError(名称)

参考文献应该在这一行中建立:

asg = autoscaling.AutoScalingGroup("tintASG")

CloudFormation脚本的部分应如下所示:

            "tintScaleDown1": {
                    "Type": "AWS::AutoScaling::ScalingPolicy",
                    "Properties": {
            "AdjustmentType": "ChangeInCapacity",
                            "AutoScalingGroupName": {
                                    "Ref": "tintASG"
                            },
                            "Cooldown": "900",
                            "ScalingAdjustment": "-1"
                    }
    },

建议?

2 个答案:

答案 0 :(得分:1)

我会这样回答。

from troposphere import Template, autoscaling
t = Template() # Add the template object as "t"
#Create the Autoscaling object as "asg" within the creation of the object, call the template to make the template format
asg = t.add_resource(autoscaling.ScalingPolicy(
    "tintScaleDown1",
    AdjustmentType="ChangeInCapacity",
    AutoScalingGroupName=Ref(tintASG),
    Cooldon="900",
    ScalingAdjustment="-1",

))
print(t.to_json())

答案 1 :(得分:0)

好的,所以对流层的创造者Mark Peek指出我的语法不正确。解决方案是

tintScaleDown.AutoScalingGroupName(Ref("tintASG"))

应该是

tintScaleDown.AutoScalingGroupName = Ref("tintASG")