Escolar Documentos
Profissional Documentos
Cultura Documentos
I want to reduce my Amazon Elastic Compute Cloud (Amazon EC2) usage by stopping and
starting instances at predefined times or utilization thresholds. Can I configure AWS Lambda
and Amazon CloudWatch Events to help me do that automatically?
Short Description
You can use CloudWatch Events to trigger an AWS Lambda function to start and stop your EC2
instances at scheduled intervals.
Resolution
CloudWatch Events allows you to create an event that is triggered at a specified time or interval
in response to events that take place in your account. For example, you can create an event using
CloudWatch Events for a specific time of day, or you can create an alarm when CPU utilization
for an instance reaches a specific threshold. You can also configure a Lambda function to start
and stop instances when triggered by these events.
In this example, we use Lambda functions to start and stop EC2 instances, and then we use
CloudWatch Events to start instances in the morning and stop the instances at night.
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"logs:CreateLogGroup",
"logs:CreateLogStream",
"logs:PutLogEvents"
],
"Resource": "arn:aws:logs:*:*:*"
},
{
"Effect": "Allow",
"Action": [
"ec2:Start*",
"ec2:Stop*"
],
"Resource": "*"
}
]
}
8. Choose Allow to complete the role and return to the AWS Lambda console, and then choose
Create Function.
9. To stop your instances, enter the following into the Function code editor:
import boto3
# Enter the region your instances are in. Include only the region without
specifying Availability Zone; e.g., 'us-east-1'
region = 'XX-XXXXX-X'
# Enter your instances here: ex. ['X-XXXXXXXX', 'X-XXXXXXXX']
instances = ['X-XXXXXXXX']
12. Repeat these steps to create another function that starts your instances again by using the
following:
import boto3
# Enter the region your instances are in. Include only the region without
specifying Availability Zone; e.g.; 'us-east-1'
region = 'XX-XXXXX-X'
# Enter your instances here: ex. ['X-XXXXXXXX', 'X-XXXXXXXX']
instances = ['X-XXXXXXXX']
Note: Use a Name and Description that indicate this function is used to start instances. You can
use the previously created role.
Note: The body of the test event doesn't affect your function, because the function doesn't use it.
4. Enter an interval of time or cron expression that tells Lambda when to stop your
instances.
Note: Cron expressions are evaluated in UTC. Be sure to adjust the expression for your
preferred time zone.
5. Choose Add target, and then choose Lambda function.
6. For Function, choose the Lambda function that stops your instances.
7. Choose Configure details.
8. Use the following information in the provided fields:
For Name, type a meaningful name, such as "StopEC2Instances."
For Description, add a meaningful description, such as “stops EC2 instances every
day at night.”
For State, choose Enabled.
Choose Create rule.
To restart your instances in the morning, repeat these steps and use your preferred start time.