Skip to content

Instantly share code, notes, and snippets.

@phwelo
Created August 11, 2022 14:43
Show Gist options
  • Select an option

  • Save phwelo/6d4c267a51eb145e7ee9bd0836e2cdab to your computer and use it in GitHub Desktop.

Select an option

Save phwelo/6d4c267a51eb145e7ee9bd0836e2cdab to your computer and use it in GitHub Desktop.

Revisions

  1. phwelo created this gist Aug 11, 2022.
    25 changes: 25 additions & 0 deletions public_instances.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,25 @@
    #!/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))%