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