-
Notifications
You must be signed in to change notification settings - Fork 330
Expand file tree
/
Copy pathlambda_ec2_state_change_time.py
More file actions
39 lines (33 loc) · 1.07 KB
/
lambda_ec2_state_change_time.py
File metadata and controls
39 lines (33 loc) · 1.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
"""
-*- coding: utf-8 -*-
========================
AWS Lambda
========================
Contributor: Chirag Rathod (Srce Cde)
========================
"""
import json
import boto3
import re
def lambda_handler(event, context):
client = boto3.client("ec2")
s3 = boto3.client("s3")
# fetch information about all the instances
status = client.describe_instances()
for i in status["Reservations"]:
instance_details = i["Instances"][0]
if instance_details["State"]["Name"].lower() in [
"shutting-down",
"stopped",
"stopping",
"terminated",
]:
print("InstanceId: ", instance_details["InstanceId"])
print("Launch time: ", instance_details["LaunchTime"])
print("State change reason: ", instance_details["StateTransitionReason"])
print(
"State change Time: ",
re.findall("\((.*?) *\)", instance_details["StateTransitionReason"]),
)
print("\n")
return {"statusCode": 200, "body": json.dumps("Thanks!")}