How to get the public ipaddress of EC2 instance using boto3

We can retrieve the public ipaddress of EC2 instance using Python boto3 script.

import boto3, argparse
from botocore.exceptions import ClientError

client = boto3.client(


reservations = client.describe_instances(InstanceIds=[INSTANCE_ID]).get("Reservations")
for reservation in reservations:
    for instance in reservation['Instances']:
        print("Public Ipaddress:" + instance.get("PublicIpAddress"))

Here we need to update the values of region_name, aws_access_key_id, aws_secret_access_key and INSTANCE_ID to the required variables in the above script.

Output of the script will contains the public ipaddress of the specified instance.

That's all…