Day 17–101 Days of DevOps —Stopping/Starting EC2 Instance using Boto3

import boto3
ec2 = boto3.resource("ec2")
regions = []
for region in ec2.meta.client.describe_regions()['Regions']:
regions.append(region['RegionName'])
for region in regions:
ec2 = boto3.resource("ec2", region_name=region)

print(“EC2 region is:", region)
ec2_instance = {"Name": "instance-state-name", "Values": ["running"]}

instances = ec2.instances.filter(Filters=[ec2_instance])
for instance in instances:
instance.stop()
print("The following EC2 instances is now in stopped state", instance.id)
  • 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()

--

--

--

AWS Community Builder, Ex-Redhat, Author, Blogger, YouTuber, RHCA, RHCDS, RHCE, Docker Certified,4XAWS, CCNA, MCP, Certified Jenkins, Terraform Certified, 1XGCP

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Declarative vs Imperative Programming

Logging in Python | Mattia’s blog

The Details of Architectural Decisions

Incentivized Testnet IDEP Sanford

Task 1:-How Social Media Platforms Using Big Data

AWS EC2 (Elastic Cloud Computing) Service

Introduction Goroutines and Concurrency in Go

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Prashant Lakhera

Prashant Lakhera

AWS Community Builder, Ex-Redhat, Author, Blogger, YouTuber, RHCA, RHCDS, RHCE, Docker Certified,4XAWS, CCNA, MCP, Certified Jenkins, Terraform Certified, 1XGCP

More from Medium

Learn VPC in Alibaba Cloud

Installing and Configuring the AWS CLI

Deploying an EC2 Instance using Terraform

Kubernetes Multiple Watches Using Threads in Python