'botocore.exceptions.ClientError: The instance ID 'i-XXXXXXXXdaXX8' does not exist

With this program i should be able to create tags and list ec2 instances from multiple regions along with the tags as CSV output. I dont exactly know what the problem is. When i tried with single region it worked but now its showing " botocore.exceptions.ClientError: An error occurred (InvalidInstanceID.NotFound) when calling the CreateTags operation: The instance ID 'i-XXXXXXXXdaXX8 does not exist". Even though the ID exists.

    from pprint import pprint

import boto3

session = boto3.Session(profile_name='PowerUserAccess')
ec2 = session.client('ec2', region_name='af-south-1')
# response = ec2.describe_instances()

collect_all_regions = []
for each_region in ec2.describe_regions()['Regions']:
    collect_all_regions.append(each_region['RegionName'])
print(collect_all_regions)

import datetime
import csv

time = datetime.datetime.now().strftime('%Y-%m-%d-%H-%M-%S')
filename_describe_instances = ('audit_tag_dump' + time + '.csv')
fieldnames = ['Instance_Name', 'ImageId', 'InstanceId', 'InstanceType', 'Availability_Zone', 'Platform',
              'PrivateIpAddress', 'PublicIpAddress', 'State', 'SubnetId', 'VpcId', 'Environment', 'AccountId',
              'audit_server_proj', 'audit_server_env',
              'audit_server_client_country',
              'audit_server_type',
              'audit_server_app']

with open(filename_describe_instances, 'w', newline='') as csvFile:
    writer = csv.writer(csvFile, dialect='excel')
    writer.writerow(fieldnames)

    for each_region in collect_all_regions:
        session = boto3.Session(profile_name="PowerUserAccess")
        ec2_re = session.resource(service_name='ec2', region_name=each_region)
        response = ec2_re.meta.client.describe_instances()

        # pprint(response)
        for each_ins_in_reg in response['Reservations']:
            pprint(each_ins_in_reg)

                        # Data Fetching #

            for Insta in each_ins_in_reg['Instances']:
                instance_imageid = Insta.get('ImageId', 'NULL')
                instance_InstanceId = Insta.get('InstanceId', 'NULL')
                instance_InstanceType = Insta.get('InstanceType', 'NULL')
                instance_Availability_Zone = Insta['Placement'].get('AvailabilityZone', 'NULL')
                instance_Platform = Insta.get('Platform', 'Linux')
                instance_Private_IP = Insta.get('PrivateIpAddress', 'NULL')
                instance_Public_IP = Insta.get('PublicIpAddress', 'NULL')
                instance_State = Insta['State'].get('Name', 'NULL')
                instance_Subnet = Insta.get('SubnetId', 'NULL')
                instance_VPCID = Insta.get('VpcId', 'NULL')
                instance_OwnerId = each_ins_in_reg.get('OwnerId', 'NULL')

                tags_list = []
                for n in Insta.get('Tags', 'NULL'):
                    if n.get('Key', 'NULL') == 'audit_server_proj':
                        audit_server_proj = n.get('Value', 'NULL')

                    if n.get('Key', 'NULL') == 'audit_server_env':
                        audit_server_env = n.get('Value', 'NULL')

                    if n.get('Key', 'NULL') == 'audit_server_client_country':
                        audit_server_client_country = n.get('Value', 'NULL')

                    if n.get('Key', 'NULL') == 'audit_server_type':
                        audit_server_type = n.get('Value', 'NULL')

                    if n.get('Key', 'NULL') == 'audit_server_app':
                        audit_server_app = n.get('Value', 'NULL')

                    if n.get('Key', 'NULL') == 'Environment':
                        instance_Environment = n.get('Value', 'NULL')

                    if n.get('Key', 'NULL') == 'Name':
                        instance_Name = n.get('Value', 'NULL')

                        tag_creation = ec2.create_tags(
                            Resources=[instance_InstanceId],
                            Tags=[
                                {
                                    'Key': 'audit_server_proj',
                                    'Value': instance_Name[:2]
                                },
                                {
                                    'Key': 'audit_server_env',
                                    'Value': instance_Name[3:7]
                                },
                                {
                                    'Key': 'audit_server_client_country',
                                    'Value': instance_Name[8:10]
                                },
                                {
                                    'Key': 'audit_server_type',
                                    'Value': instance_InstanceType
                                },
                                {
                                    'Key': 'audit_server_app',
                                    'Value': instance_Name[11:-7]
                                }

                            ]
                        )

                raw = [instance_Name,
                       instance_imageid,
                       instance_InstanceId,
                       instance_InstanceType,
                       instance_Availability_Zone,
                       instance_Platform,
                       instance_Private_IP,
                       instance_Public_IP,
                       instance_State,
                       instance_Subnet,
                       instance_VPCID,
                       instance_Environment,
                       instance_OwnerId,
                       audit_server_proj,
                       audit_server_env,
                       audit_server_client_country,
                       audit_server_type,
                       audit_server_app]

                writer.writerow(raw)
                for o in raw:
                    o = 'NULL'
                raw = []

csvFile.close()


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source