I don't need to run my Amazon EC2 instances at certain times, such as weekends or nights. How can I stop and start my Amazon EC2 instances at scheduled intervals without terminating the instances?
I would like a convenient method for stopping and starting my EC2 instances at scheduled intervals. There are Powershell and AWS CLI scripts that can accomplish this, but scripts can be error prone, require management of access keys, and introduce external dependencies that I would like to avoid.
The AWS Data Pipeline is uniquely suited to this task. Data Pipeline uses AWS technologies and can be configured to run AWS CLI commands on a set schedule with no external dependencies. Data Pipeline can write logs to S3 and runs in the context of an IAM role, which eliminates key management requirements. Data Pipeline is also cost effective; for example, the Data Pipeline free tier can be used to stop and start instances once per day. For more information, see AWS Data Pipeline Pricing.
Follow these steps to create and configure Data Pipeline to run AWS CLI commands that stop and start Amazon EC2 instances at scheduled intervals:
1. Create pipelines
Open the Data Pipeline console. Choose Create New Pipeline and enter the following information to create two pipelines:
Name: for example, "Start EC2 instances" and "Stop EC2 instances".
Description: Provide relevant details about the pipeline as needed.
Source: Choose Build using template and choose the template Run AWS CLI command.
AWS CLI command: This is where you specify what the pipeline does. Create two pipelines, one to run the aws ec2 start-instances command and another to run the aws ec2 stop-instances command.
Note: Both ec2 start-instances and ec2 stop-instances require valid values for the --region parameter.
For example, the following command could be used to start the specified EC2 instances:
aws ec2 start-instances --instance-ids i-abcd1234 i-987a654b i-ba154f3c --region us-east-x
This command provides the syntax used to stop the same EC2 instances you started:
aws ec2 stop-instances --instance-ids i-abcd1234 i-987a654b i-ba154f3c --region us-east-x
Important
If any instance ids passed to the --instance-ids parameter do not exist, the entire command fails and no instances are stopped or started. This would be a problem if any of the specified instance ids has been terminated. For example, if the instance id i-abcd1234 has been terminated, the aws ec2 start-instances example would not start any of the designated instances. Consider issuing separate, semicolon-delimited commands to guard against this scenario:
aws ec2 start-instances --instance-ids i-abcd1234 --region us-east-x;
aws ec2 start-instances --instance-ids i-987a654b --region us-east-x;
aws ec2 start-instances --instance-ids i-ba154f3c --region us-east-x
For more information about using the AWS CLI to start and stop EC2 instances, see start-instances and stop-instances in the AWS CLI documentation.
2. Apply Data Pipeline schedules
After creating separate pipelines to start and stop your instances, configure each pipeline with appropriate scheduling information. For more information, see Scheduling Pipelines.
3. Configure Data Pipeline logging
Enable logging for each pipeline and specify an S3 bucket in the same region to store the pipeline logs. Data Pipeline logging is not mandatory; however, if it is not enabled, the console displays a warning when you perform Data Pipeline validation.
4. Implement security access
Set the following options for implementing appropriate security access:
IAM Roles: Choose Custom
Pipeline Role: DataPipelineDefaultRole
EC2 Instance Role: DataPipelineDefaultResourceRole
Note
Data Pipeline creates the necessary IAM roles for you. You will then need to edit the policy for the DataPipelineDefaultResourceRole in IAM to grant it permission for the "ec2:Start*", "ec2:RunInstances", and "ec2:Stop*" actions (the next step).
5. Update role permissions
In the AWS Management Console, choose IAM, and then choose Roles. Select the "DataPipelineDefaultResourceRole" role and edit the policy "DataPipelineDefaultResourceRole_Policy". By default, this policy allows only the "ec2:Describe*" action. This action can be changed to ec2:* to allow all actions for EC2, or you can add only the actions required for your task. To start, run, and stop EC2 instances, add the "ec2:Start*", "ec2:RunInstances", and "ec2:Stop*" actions. For more information, see Editing Your Pipeline.
6. Activate the pipelines
Choose Activate in the console to activate the pipelines. You can monitor Data Pipeline activities in the console to verify that actions are completed successfully and on schedule.
EC2, Data Pipeline, schedule, stop, start, instances
Did this page help you? Yes | No
Back to the AWS Support Knowledge Center
Need help? Visit the AWS Support Center.