forked from sd031/python-for-cloud
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlambda_test.py
More file actions
31 lines (25 loc) · 781 Bytes
/
lambda_test.py
File metadata and controls
31 lines (25 loc) · 781 Bytes
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
import boto3
import json
import os
def lambda_handler(event, context):
# TODO implement
ec2_client = boto3.client("ec2", region_name="us-west-2")
reservations = ec2_client.describe_instances(Filters=[
{
"Name": "instance-state-name",
"Values": ["running"],
}
]).get("Reservations")
instances_list = []
for reservation in reservations:
for instance in reservation["Instances"]:
instance_id = instance["InstanceId"]
instance_type = instance["InstanceType"]
public_ip = instance["PublicIpAddress"]
private_ip = instance["PrivateIpAddress"]
instances_list.append(instance_id)
print(f"{instance_id}, {instance_type}, {public_ip}, {private_ip}")
return {
'statusCode': 200,
'body': json.dumps(instances_list)
}