Skip to content

Instantly share code, notes, and snippets.

@phwelo
Created August 11, 2022 14:43
Show Gist options
  • Save phwelo/6d4c267a51eb145e7ee9bd0836e2cdab to your computer and use it in GitHub Desktop.
Save phwelo/6d4c267a51eb145e7ee9bd0836e2cdab to your computer and use it in GitHub Desktop.
Public Instances - List any EC2 instances that have a public address associated
#!/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