Day 17 – Stopping/Starting EC2 Instance using Boto3
Welcome to Day 17 of 101 Days of DevOps. The topic for today is Stopping/Starting EC2 Instance using Boto3. On Day 15, you have learned the basics of Boto3 https://www.101daysofdevops.com/courses/101-days-of-devops/lessons/day-15-introduction-to-boto3/. On Day 16, you have learned Boto3 concepts Concepts(Waiter, Meta, and Paginator) https://www.101daysofdevops.com/courses/101-days-of-devops/lessons/day-16/. Let’s solidify the Boto3 idea further with the help of one practical example, how to stop/start EC2 instance using Boto3.
This is one of the ask I came across in Dev env in terms of saving cost where I need to shut down all the EC2 instance at 6 pm and bring it back the next day at 9 am. In this blog, I am only going to cover how to shutdown instances; in the next part of the blog, I will cover how to stop instances at the specified interval using the combination of Lambda and CloudWatch rules.
To write this code, we need to follow the series of steps
Step1: Importing the standard library boto3.
import boto3
Step2: Create the ec2 resource
ec2 = boto3.resource("ec2")
Step3: In the next step, you need to collect all the regions. There may be a possibility that your Developer spun up the instance in any region in AWS.
NOTE: I am using meta here because the ec2 resource doesn’t provide any method to describe regions. If you still have any doubts, please check Day 16 blog.
regions = [] for region in ec2.meta.client.describe_regions()['Regions']: regions.append(region['RegionName'])
Step4: Is to create the ec2 resource but this time for all the instances
for region in regions: ec2 = boto3.resource("ec2", region_name=region) print(“EC2 region is:", region)
Step5: We need to filter only the instances which are in running state
ec2_instance = {"Name": "instance-state-name", "Values": ["running"]} instances = ec2.instances.filter(Filters=[ec2_instance])
Step6: Stop all the instances which are in running state
for instance in instances: instance.stop() print("The following EC2 instances is now in stopped state", instance.id)
If you still doubt how this code, please check the above youtube, where you will find the complete description of how this code works.
NOTE: To start the instance, you need to make a few minor modifications to your code.
- First, your filter Values are based on stopped instances.
- Rather than using stop, you need to use the start() method.
ec2_instance = {"Name": "instance-state-name", "Values": ["stopped"]} instance.start()
GitHub link: https://github.com/100daysofdevops/100daysofdevops/tree/master/aws-lambda/ec2_stop_start_instances
Please join me with my journey by following any of the below links
- Website: https://101daysofdevops.com/
- Twitter: @100daysofdevops OR @lakhera2015
- Facebook: https://www.facebook.com/groups/795382630808645/
- Medium: https://medium.com/@devopslearning
- GitHub: https://github.com/100daysofdevops/100daysofdevops
- YouTube Channel: https://www.youtube.com/user/laprashant/videos
- Slack: https://join.slack.com/t/100daysofdevops/shared_invite/zt-au03logz-YfDUp_FJF4rAUeDEbgWmsg
- Reddit: r/101DaysofDevops
- Meetup: https://www.meetup.com/100daysofdevops/