For more information about CloudWatch alarm actions, see
`Create Alarms to Stop, Terminate, Reboot, or Recover an Instance <http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/UsingAlarmActions.html>`_
in the *Amazon CloudWatch User Guide*.
All the example code for the Amazon Web Services (AWS) SDK for Python is available `here on GitHub <https://github.com/awsdocs/aws-doc-sdk-examples/tree/master/python/example_code>`_.
Prerequisite Task
=================
* Configure your AWS credentials, as described in :doc:`quickstart`.
* Create an IAM role whose policy grants permission to describe, reboot, stop, or terminate an Amazon
EC2 instance. For more information about creating an IAM role, see
`Creating a Role to Delegate Permissions to an AWS Service <http://docs.aws.amazon.com/IAM/latest/UserGuide/id_roles_create_for-service.html>`_
in the *IAM User Guide*.
Use the following role policy when creating the IAM role.
..code-block:: python
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"cloudwatch:Describe*",
"ec2:Describe*",
"ec2:RebootInstances",
"ec2:StopInstances*",
"ec2:TerminateInstances"
],
"Resource": [
"*"
]
}
]
}
Create and Enable Actions on an Alarm
=====================================
Create or update an alarm and associate it with the specified metric. Optionally, this operation
can associate one or more Amazon SNS resources with the alarm.
When this operation creates an alarm, the alarm state is immediately set to :code:`INSUFFICIENT_DATA`.
The alarm is evaluated and its state is set appropriately. Any actions associated with the state are
then executed.
When you update an existing alarm, its state is left unchanged, but the update completely overwrites