Skip to content

Commit 6b2a883

Browse files
committed
fix-lambda-ec2-state-change
1 parent 0697735 commit 6b2a883

File tree

2 files changed

+33
-34
lines changed

2 files changed

+33
-34
lines changed
+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
"""
2+
-*- coding: utf-8 -*-
3+
========================
4+
AWS Lambda
5+
========================
6+
Contributor: Chirag Rathod (Srce Cde)
7+
========================
8+
"""
9+
10+
import json
11+
import boto3
12+
import re
13+
14+
def lambda_handler(event, context):
15+
client = boto3.client("ec2")
16+
s3 = boto3.client("s3")
17+
18+
# fetch information about all the instances
19+
status = client.describe_instances()
20+
21+
for i in status["Reservations"]:
22+
instance_details = i['Instances'][0]
23+
if instance_details["State"]["Name"].lower() in ["shutting-down", "stopped", "stopping", "terminated"]:
24+
print("InstanceId: ", instance_details["InstanceId"])
25+
print("Launch time: ", instance_details["LaunchTime"])
26+
print("State change reason: ", instance_details["StateTransitionReason"])
27+
print("State change Time: ", re.findall('\((.*?) *\)', instance_details["StateTransitionReason"]))
28+
print("\n")
29+
30+
return {
31+
'statusCode': 200,
32+
'body': json.dumps('Thanks!')
33+
}

lambda/lambda_ec2_stop_time.py

-34
This file was deleted.

0 commit comments

Comments
 (0)