问题
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