Skip to content

Instantly share code, notes, and snippets.

@makaaso
Created November 14, 2017 02:29
Show Gist options
  • Save makaaso/776f3733591a3bd0d62f0c78d1d9918a to your computer and use it in GitHub Desktop.
Save makaaso/776f3733591a3bd0d62f0c78d1d9918a to your computer and use it in GitHub Desktop.
lambda-ec2-stop.py
#!/usr/bin/env python
# -*- coding:utf-8 -*-
"""
# lambda-ec2-stop.py
This repository is AWS Lambda function's upload files.
"""
__authour__ = "masaru.kawabata"
__version__ = 1.3
import botocore
import boto3
import os
def lambda_handler(event, context):
""" Create Connection """
try:
ec2 = boto3.resource('ec2', region_name='ap-northeast-1')
except:
print('Connection Error')
return 1
""" Get EC2 Instance Info """
instance = [i for i in ec2.instances.all() for t in i.tags if t["Value"] == os.environ['TAG_NAME']][0]
""" Stop EC2 Instance """
try:
instance.stop()
except:
print('Stop EC2 Instance Error')
return 1
return 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment