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.