Using boto3 script need to find the list of AWS EC2 instances which are tagged and untagged and to export the instances results in excel

0 votes
Using boto3 script, tried to get the ec2 instance with tags and without tags resources, facing error as described below:

#EC2 Instances
import boto3
session = boto3.Session(
        region_name='eu-west-1',
        profile_name='dev'
        )
ec2_client = session.client('ec2')
dict_of_ec2 = ec2_client.describe_instances().get("Reservations")
ec2list=[]
for reservation in dict_of_ec2:
        for instance in reservation['Instances']:
            ec2instance=instance.get("InstanceId")
            ec2list.append(ec2instance)
i=0
ec2_tag_status={}
while i<len(ec2list):
    ec2_instance_name = ec2list[i]
    try:
        response = ec2_client.describe_tags(Filters=[
        {
            'Name': ec2_instance_name,
        },
        ],)
        print(response)
        tags = response['Tags']
        ec2_tag_status[ec2_instance_name]=tags
        tagKeys = [d['Key'] for d in tags if 'Key' in d]
        print(tagKeys)
        if ('app' in tagKeys or 'App' in tagKeys) and ('Department' in tagKeys or 'department' in tagKeys) and ('Owner' in tagKeys or 'owner' in tagKeys) and ('Environment' in tagKeys or 'environment' in tagKeys) and ('Product' in tagKeys or 'product' in tagKeys) and ('Module' in tagKeys or 'module' in tagKeys) :  
            print(ec2_instance_name + " is compliant resource")
        elif len(tags) == 0:
            print(ec2_instance_name + " does not have tags which is non-complaint resource")
        else:
            print(ec2_instance_name + " is non-compliant resource")
    except ClientError:
        print(ec2_instance_name, "error")
        no_tags='does not have tags'
        ec2_tag_status[ec2_instance_name]=no_tags
    i+=1

First, need to find the list of AWS EC2 instances which are tagged and untagged and to export the instances results in excel.

Second, likewise need for all the AWS resources particularly for API Gateway, Route53, Cloudfront, Cognito.

Please anyone can assist on this as soon as possible.
Oct 11, 2021 in DevOps on Cloud by SathishKumarD
• 120 points
1,743 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.

Related Questions In DevOps on Cloud

0 votes
1 answer

How to create user and password in Windows EC2 instance using boto3 Script?

Hi, I don't think AWS will allow you ...READ MORE

answered Sep 23, 2020 in DevOps on Cloud by MD
• 95,440 points
1,884 views
0 votes
0 answers

How to mount the S3 bucket using IAM role or S3fs and use in ElasticBeanstalk

Hi Team, Can you please confirm how we ...READ MORE

Sep 20, 2019 in DevOps on Cloud by rahulkathpal
• 120 points
1,906 views
+1 vote
3 answers
+2 votes
0 answers

Using .ebextensions to run commands while deploying the app in dev/uat/prod

Hey, We earlier had a plaintext credentials in ...READ MORE

Sep 10, 2019 in DevOps on Cloud by Sai
1,112 views
0 votes
1 answer
+1 vote
2 answers
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP