Feb-27-2019, 09:23 PM
I am trying to get a list of AWS instances using Boto3.
The user enters the instance IDs, then the script should print out info about the instance. Then it should terminate the instance.
But I am getting this error:
The user enters the instance IDs, then the script should print out info about the instance. Then it should terminate the instance.
But I am getting this error:
Error:python3 .\aws_ec2_terminate_instances.py
Enter an instance ID separated by commas: i-07c21656a79b8eb45
Deleting Instance IDs:
i-07c21656a79b8eb45
Traceback (most recent call last):
File ".\aws_ec2_terminate_instances.py", line 15, in <module>
instance['Tags'][{'Key': 'Name', 'Value': instance_id}]
TypeError: list indices must be integers or slices, not dict
Here is the current state of my code:import sys import boto3 import collections from collections import defaultdict ec2 = boto3.client('ec2') instance_id_list = input("Enter an instance ID separated by commas: ") instance_ids = instance_id_list.split(",") print("Deleting Instance IDs:") for instance_id in instance_ids: print(instance_id) instance = ec2.describe_instances( InstanceIds=[instance_id] ) ['Reservations'][0]['Instances'][0] instance['Tags'][{'Key': 'Name', 'Value': instance_id}] ec2info = defaultdict() for tag in instance.tags: if 'Name'in tag['Key']: print(tag['Key']) name = tag['Value'] # Add instance info to a dictionary print(instance.get('Instances')) ec2info[instance.id] = { 'Instance ID': instance.id, 'Type': instance.instance_type, 'State': instance.state['Name'], 'Private IP': instance.private_ip_address, 'Public IP': instance.public_ip_address, 'Launch Time': instance.launch_time } attributes = ['Instance ID', 'Type', 'State', 'Private IP', 'Public IP', 'Launch Time'] for instance_id, instance in ec2info.items(): for key in attributes: print("{0}: {1}".format(key, instance[key])) print(instance.terminate()) print("------") ec2.instances.filter(InstanceIds=instance).stop() ec2.instances.filter(InstanceIds=instance).terminate()How can I solve this error?