LoginSignup
2
1

More than 5 years have passed since last update.

AWS Lambdaを使ってインスタンスを特定の時間に停止した

Last updated at Posted at 2017-06-03

AWS Lambdaを使ってインスタンスを停止する処理を作ってみました。

lambda_handlerの使い方がいまいち分かりませんが、、、
AWS Lambdaのファンクションをcronで作成して0時とかに設定しておけば幸せになれるかもですかね。

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

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1