Dear Readers,
In this article, we will see the following How to Get Instance Details By Using Boto3.
Steps to Follow :
- Logging to AWS Account
- Launch Linux Server
- Connect to Linux EC2 Instance by Using Putty
- Install Python Package
- Install Boto3 Package
- Connect to AWS CLI
- Script for getting all the instances information
- Script for getting Instance ID
- Script for getting Instance ID,Instance Type
- Script for getting Instance ID,Instance Type and Instance State
- Script for getting Instance ID,Instance Type,Instance State and Instance Name
1. Logging to AWS Account
First, we need to AWS Console page by using below link.
https://aws.amazon.com/console/
Click on sign in to Console button.
Logging to aws account
Login using username & password and click on sign in.
Enter to AWS Management Console
We can see the AWS Management Console Dashboard.
Go to Services, under the compute module click EC2 service to open.
2. Launch Linux EC2 Instance
Go to Services, under the compute module click EC2 service to open.
We can launch Windows Server by using below link.
How to Launch Linux EC2 Instance
We can see the windows server has been launched successfully.
3. Connect to Linux EC2 Instance Using Putty
We can connect putty by using below link.
Connect to EC2 Instance Using Putty
Go inside the Linux Server Terminal.
1 2 3 4 5 6 7 8 9 10 11 |
login as: ec2-user Authenticating with public key "imported-openssh-key" __| __|_ ) _| ( / Amazon Linux 2 AMI ___|\___|___| https://aws.amazon.com/amazon-linux-2/ 2 package(s) needed for security, out of 13 available Run "sudo yum update" to apply all updates. [ec2-user@ip-172-31-6-132 ~]$ |
6. Connect to AWS CLI
If we want to connect to AWS CLI,we need access key and secret key for that we generate credentials from IAM.
Generate Access Key ID and Secret Access Key
Go to IAM Dashboard and click on My access key.
Click on Create New Access Key.
We can see the Generated Access Key ID and Secret Access Key.
These keys are necessary to access the CLI.
Go to Linux Server Terminal and specify required credentials, region and output format for accessing to CLI
1 2 3 4 5 |
[root@ip-172-31-6-132 ~]# aws configure AWS Access Key ID [None]: AKIAJVGJXOUGKYGMPWSQ AWS Secret Access Key [None]: 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 Default region name [None]: ap-south-1 Default output format [None]: table |
Make directory pythonscripts
1 2 3 4 5 |
[root@ip-172-31-6-132 ~]# mkdir pythonscripts [root@ip-172-31-6-132 ~]# ll total 1844 -rw-r--r-- 1 root root 1885433 Sep 8 12:45 get-pip.py drwxr-xr-x 2 root root 6 Oct 12 05:43 pythonscripts |
Go inside the directory
1 2 3 |
[root@ip-172-31-6-132 ~]# cd pythonscripts/ [root@ip-172-31-6-132 pythonscripts]# pwd /root/pythonscripts |
7. Script for getting all the instances information
Make a file “describe.py” and write python script for getting instances information.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
[root@ip-172-31-6-132 pythonscripts]# vim describe.py #All the information '''import boto3 client = boto3.client('ec2') Myec2=client.describe_instances() print(Myec2)''' #Instances information import boto3 client = boto3.client('ec2') Myec2=client.describe_instances() for pythonins in Myec2['Reservations']: print(pythonins) |
Note: ESC and wq! to quit.
Run Python Script
1 |
[root@ip-172-31-6-132 pythonscripts]# python describe.py |
We can see all the information about instance in Mumbai region.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
{u'Instances': [{u'Monitoring': {u'State': 'disabled'}, u'PublicDnsName': 'ec2-13-233-154-38.ap-south-1.compute.amazonaws.com', u'State': {u'Code': 16, u'Name': 'running'}, u'EbsOptimized': False, u'LaunchTime': datetime.datetime(2020, 10, 11, 11, 53, 47, tzinfo=tzlocal()), u'PublicIpAddress': '13.233.154.38', u'PrivateIpAddress': '172.31.6.132', u'ProductCodes': [], u'VpcId': 'vpc-0f20c664', u'CpuOptions': {u'CoreCount': 1, u'ThreadsPerCore': 1}, u'StateTransitionReason': '', u'InstanceId': 'i-0a91d8aee3ab0061c', u'EnaSupport': True, u'ImageId': 'ami-0e306788ff2473ccb', u'PrivateDnsName': 'ip-172-31-6-132.ap-south-1.compute.internal', u'KeyName': 'linux', u'SecurityGroups': [{u'GroupName': 'launch-wizard-4', u'GroupId': 'sg-096b79a56d48ddc47'}], u'ClientToken': '', u'SubnetId': 'subnet-a0e29fec', u'InstanceType': 't2.micro', u'CapacityReservationSpecification': {u'CapacityReservationPreference': 'open'}, u'NetworkInterfaces': [{u'Status': 'in-use', u'MacAddress': '0a:49:b8:45:75:1c', u'SourceDestCheck': True, u'VpcId': 'vpc-0f20c664', u'Description': '', u'NetworkInterfaceId': 'eni-082d2827bafe19769', u'PrivateIpAddresses': [{u'PrivateDnsName': 'ip-172-31-6-132.ap-south-1.compute.internal', u'PrivateIpAddress': '172.31.6.132', u'Primary': True, u'Association': {u'PublicIp': '13.233.154.38', u'PublicDnsName': 'ec2-13-233-154-38.ap-south-1.compute.amazonaws.com', u'IpOwnerId': 'amazon'}}], u'PrivateDnsName': 'ip-172-31-6-132.ap-south-1.compute.internal', u'InterfaceType': 'interface', u'Attachment': {u'Status': 'attached', u'DeviceIndex': 0, u'DeleteOnTermination': True, u'AttachmentId': 'eni-attach-015a52ae0fd536208', u'AttachTime': datetime.datetime(2020, 10, 11, 11, 53, 47, tzinfo=tzlocal())}, u'Groups': [{u'GroupName': 'launch-wizard-4', u'GroupId': 'sg-096b79a56d48ddc47'}], u'Ipv6Addresses': [], u'OwnerId': '436798457774', u'PrivateIpAddress': '172.31.6.132', u'SubnetId': 'subnet-a0e29fec', u'Association': {u'PublicIp': '13.233.154.38', u'PublicDnsName': 'ec2-13-233-154-38.ap-south-1.compute.amazonaws.com', u'IpOwnerId': 'amazon'}}], u'SourceDestCheck': True, u'Placement': {u'Tenancy': 'default', u'GroupName': '', u'AvailabilityZone': 'ap-south-1b'}, u'Hypervisor': 'xen', u'BlockDeviceMappings': [{u'DeviceName': '/dev/xvda', u'Ebs': {u'Status': 'attached', u'DeleteOnTermination': True, u'VolumeId': 'vol-0e2c80f1704446225', u'AttachTime': datetime.datetime(2020, 10, 11, 11, 53, 48, tzinfo=tzlocal())}}], u'Architecture': 'x86_64', u'RootDeviceType': 'ebs', u'RootDeviceName': '/dev/xvda', u'VirtualizationType': 'hvm', u'Tags': [{u'Value': 'Linux Server', u'Key': 'Name'}], u'HibernationOptions': {u'Configured': False}, u'MetadataOptions': {u'State': 'applied', u'HttpEndpoint': 'enabled', u'HttpTokens': 'optional', u'HttpPutResponseHopLimit': 1}, u'AmiLaunchIndex': 0}], u'ReservationId': 'r-0747a3eb8e9245e34', u'Groups': [], u'OwnerId': '436798457774'}], 'ResponseMetadata': {'RetryAttempts': 0, 'HTTPStatusCode': 200, 'RequestId': 'ca53f1e8-f073-449a-904b-ad6f84acfc6c', 'HTTPHeaders': {'x-amzn-requestid': 'ca53f1e8-f073-449a-904b-ad6f84acfc6c', 'content-length': '7203', 'vary': 'accept-encoding', 'server': 'AmazonEC2', 'date': 'Mon, 12 Oct 2020 05:53:13 GMT', 'content-type': 'text/xml;charset=UTF-8'}}} |
8. Script for getting Instance ID
Modify file “describe.py” and write script for instance ID.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
#All inforamtion '''import boto3 client = boto3.client('ec2') Myec2=client.describe_instances() print(Myec2)''' #Instances information '''import boto3 client = boto3.client('ec2') Myec2=client.describe_instances() for pythonins in Myec2['Reservations']: print(pythonins)''' #Instance Id import boto3 client = boto3.client('ec2') Myec2=client.describe_instances() for pythonins in Myec2['Reservations']: for printout in pythonins['Instances']: print(printout['InstanceId']) |
Run Python Script
We can see the instance ID
1 2 |
[root@ip-172-31-6-132 pythonscripts]# python describe.py i-0a91d8aee3ab0061c |
9. Script for getting Instance ID,Instance Type
Modify file “describe.py” and write script for getting Instance ID,Instance Type.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
[root@ip-172-31-6-132 pythonscripts]# vim describe.py #All inforamtion '''import boto3 client = boto3.client('ec2') Myec2=client.describe_instances() print(Myec2)''' #Instances information '''import boto3 client = boto3.client('ec2') Myec2=client.describe_instances() for pythonins in Myec2['Reservations']: print(pythonins)''' #Instance Id import boto3 client = boto3.client('ec2') Myec2=client.describe_instances() for pythonins in Myec2['Reservations']: for printout in pythonins['Instances']: print(printout['InstanceId']) #Instance Type import boto3 client = boto3.client('ec2') Myec2=client.describe_instances() for pythonins in Myec2['Reservations']: for printout in pythonins['Instances']: print(printout['InstanceId']) print(printout['InstanceType']) |
Run Python Script
We can see the instance ID & Instance Type
1 2 3 |
[root@ip-172-31-6-132 pythonscripts]# python describe.py i-0a91d8aee3ab0061c t2.micro |
10. Script for getting Instance ID,Instance Type and Instance State
Modify file “describe.py” and write script for getting Instance ID,Instance Type and Instance State.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
[root@ip-172-31-6-132 pythonscripts]# vim describe.py #All inforamtion '''import boto3 client = boto3.client('ec2') Myec2=client.describe_instances() print(Myec2)''' #Instances information '''import boto3 client = boto3.client('ec2') Myec2=client.describe_instances() for pythonins in Myec2['Reservations']: print(pythonins)''' #Instance Id import boto3 client = boto3.client('ec2') Myec2=client.describe_instances() for pythonins in Myec2['Reservations']: for printout in pythonins['Instances']: print(printout['InstanceId']) #Instance Type import boto3 client = boto3.client('ec2') Myec2=client.describe_instances() for pythonins in Myec2['Reservations']: for printout in pythonins['Instances']: print(printout['InstanceId']) print(printout['InstanceType']) import boto3 client = boto3.client('ec2') Myec2=client.describe_instances() for pythonins in Myec2['Reservations']: for printout in pythonins['Instances']: print(printout['InstanceId']) print(printout['InstanceType']) print(printout['State']['Name']) |
Run Python Script
We can see the instance ID & Instance Type&Instance state
1 2 3 4 |
[root@ip-172-31-6-132 pythonscripts]# python describe.py i-0a91d8aee3ab0061c t2.micro running |
11. Script for getting Instance ID,Instance Type,Instance State and Instance Name
Modify file “describe.py” and write script for getting Instance ID,Instance Type,Instance State and Instance Name.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
[root@ip-172-31-6-132 pythonscripts]# vim describe.py #All inforamtion '''import boto3 client = boto3.client('ec2') Myec2=client.describe_instances() print(Myec2)''' #Instances information '''import boto3 client = boto3.client('ec2') Myec2=client.describe_instances() for pythonins in Myec2['Reservations']: print(pythonins)''' #Instance Id '''import boto3 client = boto3.client('ec2') Myec2=client.describe_instances() for pythonins in Myec2['Reservations']: for printout in pythonins['Instances']: print(printout['InstanceId'])''' #Instance Type '''import boto3 client = boto3.client('ec2') Myec2=client.describe_instances() for pythonins in Myec2['Reservations']: for printout in pythonins['Instances']: print(printout['InstanceId']) print(printout['InstanceType'])''' #import boto3 '''client = boto3.client('ec2') Myec2=client.describe_instances() for pythonins in Myec2['Reservations']: for printout in pythonins['Instances']: print(printout['InstanceId']) print(printout['InstanceType']) print(printout['State']['Name'])''' #Instance id,Instance type,Instance State,Instance Name import boto3 client = boto3.client('ec2') Myec2=client.describe_instances() for pythonins in Myec2['Reservations']: for printout in pythonins['Instances']: for printname in printout['Tags']: print(printout['InstanceId']) print(printout['InstanceType']) print(printout['State']['Name']) print(printname['Value']) |
Run Python Script
We can see the Instance id,Instance type, Instance State, Instance Name.
1 2 3 4 5 |
[root@ip-172-31-6-132 pythonscripts]# python describe.py i-0a91d8aee3ab0061c t2.micro running Linux Server |
Thank you for giving your valuable time to read the above information.
Follow us on
Website : www.ktexperts.com
Facebook Page : KTexperts
Linkedin Page : KT EXPERTS
Follow Me
Ramesh’s Linkedin : Ramesh Atchala