[PYTHON] I stopped my instance at a specific time using AWS Lambda

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

I stopped my instance at a specific time using AWS Lambda
[Python] I wrote a REST API using AWS API Gateway and Lambda.
Delete 1000 objects stored in AWS S3 at a time.
I tried to get an AMI using AWS Lambda
I just built a virtual environment with AWS lambda layer
I made a script to say hello at my Koshien
Time measurement using a clock
Web scraping using AWS lambda
I tried using AWS Chalice
A story that I was addicted to calling Lambda from AWS Lambda.
I tried to make a regular expression of "time" using Python
I compared Node.js and Python in creating thumbnails using AWS Lambda
Summary if using AWS Lambda (Python)
I made a Line-bot using Python!
Tweet WakaTime Summary using AWS Lambda
Using Lambda with AWS Amplify with Go
Stop your AWS instance using Boto3
AWS Lambda Development My Best Practices
Tweet in Chama Slack Bot ~ How to make a Slack Bot using AWS Lambda ~
I changed my job to a machine learning engineer at AtCoder Jobs
I won't have a hard time anymore. .. Multi-process using Python's standard library.
Get a datetime instance at any time of the day in Python
I wrote a Slack bot that notifies delay information with AWS Lambda