Created
August 11, 2022 14:43
-
-
Save phwelo/6d4c267a51eb145e7ee9bd0836e2cdab to your computer and use it in GitHub Desktop.
Public Instances - List any EC2 instances that have a public address associated
This file contains 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 python3 | |
import boto3 | |
import json | |
client = boto3.client('ec2') | |
ec2_result = client.describe_instances() | |
results = [] | |
for result in ec2_result["Reservations"]: | |
for instance in result["Instances"]: | |
obj = { | |
"id": instance["InstanceId"], | |
"state": instance["State"]["Name"], | |
"subnet": instance["SubnetId"], | |
"vpc_id": instance["VpcId"] | |
} | |
try: | |
obj["public_ip"] = instance["PublicIpAddress"] | |
except KeyError: | |
continue | |
if "public_ip" in obj and obj["state"] == "running": | |
results.append(obj) | |
print(json.dumps(results))% |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment