Retrieving public dns of EC2 instance with BOTO3

前端 未结 3 1628
后悔当初
后悔当初 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:23

    Here my wrapper:

    import boto3
    from boto3.session import Session
    
    def credentials():
        """Credentials:"""
        session = Session(aws_access_key_id= 'XXXXXXXXX',
                          aws_secret_access_key= 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx')
        ec2 = boto3.resource('ec2', region_name='us-east-2')
        return ec2
    
    def get_public_dns(instance_id):
        """having the instance_id, gives you the public DNS"""
        ec2 = credentials()
        instance = ec2.Instance(instance_id)
        instancePublicDNS = managerInstance.public_dns_name
        return instancePublicDNS
    

    Then you just need to use your instance_id to get public id of any of your active ec2:

    dns = get_public_dns(instance_id)
    

    Remember to change "region_name" to your zone and add your "aws_access_key_id" and "aws_secret_access_key"

提交回复
热议问题