Retrieving public dns of EC2 instance with BOTO3

前端 未结 3 1626
后悔当初
后悔当初 2021-02-19 02:22

I\'m using ipython to get an understanding of Boto3 and interacting with EC2 instances. Here is the code I\'m using to create an instance:

import boto3

ec2 = bo         


        
3条回答
  •  无人共我
    2021-02-19 02:34

    import boto3
    import pandas as pd
    session = boto3.Session(profile_name='aws_dev')
    dev_ec2_client = session.client('ec2')
    response = dev_ec2_client.describe_instances()
    df = pd.DataFrame(columns=['InstanceId', 'InstanceType', 'PrivateIpAddress','PublicDnsName'])
    i = 0
    for res in response['Reservations']:
        df.loc[i, 'InstanceId'] = res['Instances'][0]['InstanceId']
        df.loc[i, 'InstanceType'] = res['Instances'][0]['InstanceType']
        df.loc[i, 'PrivateIpAddress'] = res['Instances'][0]['PrivateIpAddress']
        df.loc[i, 'PublicDnsName'] = res['Instances'][0]['PublicDnsName']
        i += 1
    print df
    

    Note:

    1. Change this profile with your AWS profile name profile_name='aws_dev'
    2. This code is working for Python3

提交回复
热议问题