[PYTHON] Stop an instance with a specific tag in Boto3

I tried to power up the stop process with Boto3 for a specific instance created earlier.

As a premise Add the env tag to EC2 and prepare the instance set as dev as the value.

I couldn't handle Filters well, but I managed to do it. Blog up to commemorate. Next, let's work with Lambda.

# -*- coding: utf-8 -*-

# import
import boto3
from boto3.session import Session

ec2 = boto3.client('ec2')
dev_list = []

# def
def get_list():
  instance_list = ec2.describe_instances(
    Filters=[{'Name': 'tag:env', 'Values': ['dev']}]
  )
  for Reservations in instance_list['Reservations']:
    for dev_instances in Reservations['Instances']:
      dev_list.append(dev_instances["InstanceId"])
      return dev_list

def ec2_stop(dev_list):
  for instance_id in dev_list:
    response = ec2.stop_instances(
      InstanceIds=[
        instance_id
      ]
    )

# Main
if __name__ == "__main__":
  get_list()
  ec2_stop(dev_list)

https://github.com/handa3/study/blob/master/aws/ec2/dev_stop.py

Recommended Posts

Stop an instance with a specific tag in Boto3
Clone with a specific branch / tag in GitPython
Sort dict in dict (dictionary in dictionary) with a specific key
code-server online environment (3) Launch an EC2 instance with Boto3
Count specific strings in a file
Create an instance of a predefined class from a string in Python
Enumerate files with a specific extension
Save a specific variable in tensorflow.session
How to create a heatmap with an arbitrary domain in Python
Stop your AWS instance using Boto3
Send an email to a specific email address with python without SMTP settings
Get information about EC2 instances with arbitrary prefixes in instance names with boto
How to stop a program in python until a specific date and time
Generate all files with a specific extension
Spiral book in Python! Python with a spiral book! (Chapter 14 ~)
Put TensorFlow in P2 instance with pip3
Draw a heart in Ruby with PyCall
Stop optimization with early stopper in scikit-optimize
Operate an oscilloscope with a Raspberry Pi
Creating a virtual environment in an Anaconda environment
Note) Batch conversion of specific symbols contained in a character string with a dictionary
Send an email with a user other than EMAIL_HOST_USER written in settings in django
Searching for an efficient way to write a Dockerfile in Python with poetry
[Python] Get the files in a folder with Python
Try Tensorflow with a GPU instance on AWS
[Python] Make a game with Pyxel-Use an editor-
Make a monitoring device with an infrared sensor
Create an executable file in a scripting language
Delete data in a pattern with Redis Cluster
Start Django in a virtual environment with Pipenv
Create a virtual environment with conda in Python
Build a Django environment with Vagrant in 5 minutes
Get a list of IAM users with Boto3
Create an image with characters in python (Japanese)
Extract lines containing a specific "string" in Pandas
Work in a virtual environment with Python virtualenv.
Create a new page in confluence with Python
A story packed with absolute values in numpy.ndarray
Enter a specific value for variable in tensorflow
Configure a module with multiple files in Django
How to count numbers in a specific range
Throw costly instance health in Slack with Lambda
View logs in an easy-to-understand manner with Ansible
Get a row containing a specific element in np.where
Get the value of a specific key in a list from the dictionary type in the list with Python
Create an exe file that works in a Windows environment without Python with PyInstaller
[Python] Leave only the elements that start with a specific character string in the array
Try running a Schedule to start and stop an instance on AWS Lambda (Python)
I wrote a CLI tool in Go language to view Qiita's tag feed with CLI
Python2 / numpy> Replace only a specific column in a file with column data from another file> numpy.c_