AWS Boto3 ec2 inventory script

0 votes
Hi ,

I am trying to write a code in boto3 to get all ec2 inventory information with tags assigned to particular resources and output it to a csv file.

I want all the tags assigned to a resource to come in the same line with the resource information in csv file,but for every new tag value it is getting printed on the next line.

 any help will be much appreciated.

Thanks,

Aman
Jul 19, 2020 in AWS by khyati
• 190 points
1,638 views

1 answer to this question.

0 votes
Hi@khyatl,

You need to remove the newline character. Like in print function by default newline character is added.
answered Jul 20, 2020 by MD
• 95,320 points
How to remove that?
Can you share your script?
import boto3
import csv
aws_mang_con=boto3.session.Session(profile_name='root')
ec2_cli=aws_mang_con.client('ec2')
cnt=1
csv_ob=open("demo.csv","w",newline='')
csv_w=csv.writer(csv_ob)
csv_w.writerow(["S_NO","Instance_Id",'Instance_Type','PrivateIpAddress','Accountid','Name','Application'])
response=ec2_cli.describe_instances()['Reservations']
for each_item in response:
    for instances in each_item['Instances']:
        for tags in instances['Tags']:
                csv_w.writerow([cnt,instances['InstanceId'],instances['InstanceType'],instances['PrivateIpAddress'],each_item['OwnerId'],tags['Value']])
                cnt+=1
csv_ob.close()

Above is the script.

Added to the script i am getting output like 

S_NO Instance_Id Instance_Type PrivateIpAddress Accountid Name
1 i-0bf174186c4ab9302 t2.micro 172.31.86.32 2.9E+10 python-linux
2 i-0bf174186c4ab9302 t2.micro 172.31.86.32 2.9E+10 Cloudera
3 i-0b9c9a64f048a2022 t2.micro 172.31.93.49 2.9E+10 linux
4 i-0b9c9a64f048a2022 t2.micro 172.31.93.49 2.9E+10 DISCOVERY

 

But i need information like this
1 i-0bf174186c4ab9302 t2.micro 172.31.86.32 2.9E+10 python-linux Cloudera
2 i-0b9c9a64f048a2022 t2.micro 172.31.93.49 2.9E+10 linux

DISCOVERY


 
I need all tags information associated with the instance id in the same line it is coming on the different line for every new tag creating more number of information.

In your open function remove newline keyword and try once.

Related Questions In AWS

+1 vote
2 answers

How do I run python script on aws EC2 instance(ubuntu)?

I believe that you are using the ...READ MORE

answered Apr 17, 2018 in AWS by Cloud gunner
• 4,650 points
8,323 views
0 votes
1 answer
0 votes
1 answer

How to Pass the VPC ID while creating the Ec2 instance in AWS using Python Boto3

import boto3 ec2 = boto3.resource('ec2') instance = ec2.create_instances( ...READ MORE

answered Jan 29, 2019 in AWS by Priyaj
• 58,140 points
1,660 views
0 votes
1 answer

List All AWS EC2 Instances using boto3.

Hi@MD, You need to import the boto3 module ...READ MORE

answered Oct 7, 2020 in AWS by akhtar
• 38,210 points
8,700 views
+2 votes
3 answers

Is it possible to ping AWS EC2 instance

Start by adding a new EC2 security group inbound ...READ MORE

answered Apr 9, 2018 in AWS by hemant
• 5,810 points
6,465 views
0 votes
3 answers
0 votes
1 answer

AWS:Using S3 with EC2 instances

Yes, it can be used for instances ...READ MORE

answered Jun 26, 2018 in AWS by hemant
• 5,810 points
2,992 views
0 votes
1 answer

How to create an AWS EC2 Instance in the VPC with Boto3 module?

Hi@akhtar, You can use a network interface in ...READ MORE

answered Oct 11, 2020 in AWS by MD
• 95,320 points
583 views
0 votes
1 answer

What kind of data generates from EC2 to EBS in AWS

Hi, First thing is EC2 is a compute service whereas EBS is ...READ MORE

answered Mar 16, 2020 in AWS by MD
• 95,320 points
382 views