Created
November 14, 2017 02:29
-
-
Save makaaso/776f3733591a3bd0d62f0c78d1d9918a to your computer and use it in GitHub Desktop.
lambda-ec2-stop.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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