Cloud service providers charges us on the bases of what resources we use and run. For the organizations there might be possibility that their few servers are unused during the nights and weekends. But if your instances are running, you would be charged no matter you use them or not. To overcome this issue and save the overall cost, you should think to stop your unused EC2 instances in the night as well as in the weekends. This can reduce 30-40% over all cost.
You can either stop your EC2 instances manually or you can set a schedule to Auto Start Stop EC2 instance. If you have a large number of running instances the manual method cannot be possible or might be too irritating. However, you can schedule auto start stop EC2 instances at regular intervals using Lambda functions. In this article, we are going to explain a step by step guide how to start and stop your EC2 instance at specific time, nights, and/or weekends.
To Schedule Auto Start Stop EC2 Instance, you need to perform the following tasks.
- Create a Lambda Function
- Create an Event Schedule
- Test and Validate your EC2 Schedule
Create an Auto Start EC2 Instance Lambda Function
To create a Lambda function you need to perform the following steps:
- Open the AWS Lambda Console and click Create a Lambda Function as shown in the following figure to create a Lambda function.
- On the Select Blueprint page, click on Blank Function to choose it as shown in the following figure.
- On the Configure Triggers page, click Next to proceed.
- On the Configure Function page, set the following values:
- Name: AutoStartEC2Insatnce
- Description: Auto Start EC2 Instance
- Runtime: Python 2.7.
- On the Code entry type area, type the following script carefully.
import boto3 # Specify the region where your insatnces are running. For example 'ap-southeast-1' region = 'ap-southeast-1' # Specify the insatnce IDs that you want to start at specific time. For example, ['i-abcd01234567', 'i-efgh01234567'] instances = ['i-00bc7ba840f6a6520'] def lambda_handler(event, context): ec2 = boto3.client('ec2', region_name=region) ec2.start_instances(InstanceIds=instances) print 'started your instances: ' + str(instances)
- The Configure function page is shown in the following figure
Note: Replace your instance ID and region appropriately.
- On the same page, scroll-down to the Lambda function and handler section and select Create a custom role as shown in the following figure.
- The IAM management console will be opened in a new tab, type a Role name. Click View policy document and then click Edit to edit it.
- Type the following script as-is in the edit policy box. Remove the existing script text.
{ "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": "*" } ] }
- Click Save to save the changes. Click Allow and return to the Lambda Function console.
- On the same page, scroll-down to the Advanced Settings section and set the Timeout value more than 1 minutes.
- Finally click Next to proceed. On the Review page, click Create Function to complete the wizard.
- To test that your function works properly, make sure that the instance you mentioned during creating the Lambda function is stopped.
- Click on the Test tab and then click Save and Test, if everything goes fine, you will see the the script execution result something like below.
Note: If you get any error, please check the Lambda Function script code or let us know in the comment box.
To verify that instance is started, go to EC2 running instance list and check the status of instance you mentioned in the script.
Creating an Auto Start Stop Event Schedule Rule
Till now, you have created the Lambda function and IAM role, however, you have still not defined the time when this instance should start. For this, you need to create an Event scheduler.
- Open the CloudWatch console, click Rules in the left pane.
- On the Create Rule page, select the Schedule button.
- In the Cron expression box, set the desired time when you want to start your instances. In our case, we have set it to start at 01:25 GMT at daily.
For more information about event scheduler, click here.
- In the right pane, select the Lambda Function as Targets and then select the Lambda function name you have created previously.
- Click Configure details to proceed. On the Configure details page, specify the rule name, description, and complete wizard.
Test and Validate Auto Start Stop EC2 Schedule
Now you have done all the steps. Just wait for the time you mentioned in the schedule expression and verify that your instance starts automatically.
Using the similar process you can also schedule auto stop event for specific instances at specific time. The only difference is that you need to use the following Lambda Function script as shown in the following figure.
In addition, you also do not need to create the custom IAM role as it is one time activity.
In this article, we have explained a step by step guide how to schedule auto start stop EC2 instances. Please let us know, if you get stuck anywhere, your suggestions are also invited to improve the quality of articles.
If your are interested to learn more AWS Cloud Step By Step Lab Exercises and Industry best practices, you can buy the following PDF guide.
[selz link=”http://selz.co/Vky1QZCWV” button_text=”Buy it now” show_logos=”false” background_color=”#7959c7″ text_color=”#ffffff” link_color=”#7959c7″ chbg_color=”#7959c7″ chtx_color=”#ffffff” type=”widget” interact=”modal”]