Cloudformation to Change Ec2 Instance State

天大地大妈咪最大 提交于 2019-12-12 01:47:43

问题


Currently I am designing Cloudformation template to create an EC2 instance and monitor the EC2 instance, enable detail monitoring using cloudwatch and change the instance state to stop once it reached the metrics,

I am able to create a instance able to enable metrics but I am not able to change the instance state to stop.


回答1:


If you want to change the EC2 instance state to stop when a specified AWS::CloudWatch::Alarm Resouce transitions into ALARM state, you can accomplish this using the special arn:aws:automate:region:ec2:stop ARN in the AlarmActions property for the alarm.

Note however that before this will work, you need to manually create at least one stop, terminate, or reboot alarm using the Amazon EC2 or CloudWatch console to create the EC2ActionsAccess IAM role. After this IAM role is created, you can create stop, terminate, or reboot alarms using the CloudFormation resource directly. See the PutMetricAlarm documentation for more details.




回答2:


Looking at the official docs for the CloudFormation AWS::EC2::Instance resource, this functionality does not seem to be supported.

You can always delete the resource from your sources, but that would - of course - terminate the instance, which I don't think is what you're looking for.

Perhaps one workaround would be to change the UserData section, so that it runs something along the lines of shutdown -h now while also changing the InstanceInitiatedShutdownBehavior to stop.

Might be worth a shot.



来源:https://stackoverflow.com/questions/42249382/cloudformation-to-change-ec2-instance-state

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!