I tried to create a process to stop the instance using AWS Lambda.
I'm not sure how to use lambda_handler, but ... You might be happy if you create an AWS Lambda function with cron and set it to 0 o'clock.
from __future__ import print_function
import boto3
from boto3.session import Session
ec2 = boto3.client('ec2')
dev_list = []
def lambda_handler(event, context):
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"])
for instance_id in dev_list:
response = ec2.stop_instances(
InstanceIds=[
instance_id
]
)
return dev_list
https://github.com/handa3/study/blob/master/aws/lambda/stop_instance.py
Recommended Posts