Python Script to get the AWS EC2 Instance Details

Used python version 3.7 and Boto3, Kindly configure the AWS IAM Credentials in your local or workstation

AWS Profile Configuration

aws configure --profile name(my_proile)
aws configure --profile name(my_proile)
aws configure --profile name(my_proile)

Enter fullscreen mode Exit fullscreen mode

import boto3
import sys
import time
session=boto3.Session(profile_name="my_profile", region_name="us-east-1") #boto3 session, aws profile
ec2_resource=session.resource(service_name="ec2") # resource object method
'''
client object method, commented because this script using resource object method
ec2_client=session.client(service_name="ec2")
'''
#using Resource Object
'''
for each_instances in ec2_resource.instances.all():
print(each_instances.id, each_instances.state['Name'])
'''
for instance in ec2_resource.instances.all():
print(
"Id: {0}\nPlatform: {1}\nType: {2}\nPublic IPv4: {3}\nAMI: {4}\nState: {5}\n".format(
instance.id, instance.platform, instance.instance_type, instance.public_ip_address, instance.image.id, instance.state
)
)
import boto3
import sys
import time

session=boto3.Session(profile_name="my_profile", region_name="us-east-1") #boto3 session, aws profile 

ec2_resource=session.resource(service_name="ec2") # resource object method

'''
client object method, commented because this script using resource object method
ec2_client=session.client(service_name="ec2")
'''

#using Resource Object
'''
for each_instances in ec2_resource.instances.all():
    print(each_instances.id, each_instances.state['Name'])
'''

for instance in ec2_resource.instances.all():
     print(
            "Id: {0}\nPlatform: {1}\nType: {2}\nPublic IPv4: {3}\nAMI: {4}\nState: {5}\n".format(
         instance.id, instance.platform, instance.instance_type, instance.public_ip_address, instance.image.id, instance.state
         )
     )
import boto3 import sys import time session=boto3.Session(profile_name="my_profile", region_name="us-east-1") #boto3 session, aws profile ec2_resource=session.resource(service_name="ec2") # resource object method ''' client object method, commented because this script using resource object method ec2_client=session.client(service_name="ec2") ''' #using Resource Object ''' for each_instances in ec2_resource.instances.all(): print(each_instances.id, each_instances.state['Name']) ''' for instance in ec2_resource.instances.all(): print( "Id: {0}\nPlatform: {1}\nType: {2}\nPublic IPv4: {3}\nAMI: {4}\nState: {5}\n".format( instance.id, instance.platform, instance.instance_type, instance.public_ip_address, instance.image.id, instance.state ) )

Enter fullscreen mode Exit fullscreen mode

原文链接:Python Script to get the AWS EC2 Instance Details

© 版权声明
THE END
喜欢就支持一下吧
点赞12 分享
If you don’t try, you’ll never know. So try.
如果你不去试,你永远也不知道结果,所以去试试吧
评论 抢沙发

请登录后发表评论

    暂无评论内容