import json
from urllib.request import Request, urlopen
def lambda_handler(event, context):
#webhookurl
webhookurl = "https://hooks.slack.com/services/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"
# ec2
detail = event['detail']
instance_id = detail['instance-id']
instance_state = detail['state']
messe = "msg here"
if instance_id == "i-000d520e3ec8d6f97" :
messe = "STG"
elif instance_id == "i-0e2b0157636751cc7" :
messe = "sest"
elif instance_id == "i-018fcaabe46773e38" :
messe = "prod"
elif instance_id == "i-06fcd00b94795927c" :
messe = "live"
elif instance_id == "" :
messe = "STG02"
# (attachmen
if instance_state == "stopped":
slack_message = {
"attachments": [
{
"color" : "good",
"title" : messe + "EC2 ...\n EC2 State Noticee",
"pretext" : "お疲れ様でした。",
# state
'text': "State: %s" % (instance_state)
}
]
}
else:
slack_message = {
"attachments": [
{
"color" : "good",
"title" : messe + "EC2 ...。\n EC2 State Notice",
"pretext" : "おはようございます!",
# ...
'text': "State: %s" % (instance_state)
}
]
}
req = Request(webhookurl, json.dumps(slack_message).encode('utf-8'))
responce = urlopen(req)
responce.read()
AutoStartStopInstance
Constant JSON:
{"Action": "start", "Region": "ap-northeast-1", "Instances": ["i-0e2b0157636751cc7"]}
Python 2.7 Function:
import boto3
def lambda_handler(event, context):
region = event['Region']
instances = event['Instances']
ec2 = boto3.client('ec2', region_name=region)
if event['Action'] == 'start':
ec2.start_instances(InstanceIds=instances)
print 'started your instances: ' + ", ".join(instances)
elif event['Action'] == 'stop':
ec2.stop_instances(InstanceIds=instances)
print 'stopped your instances: ' + ", ".join(instances)
Comments
Post a Comment