On 2019-02-26 20:06, Tim Dunphy wrote:
Hi all,

I'm trying to create a python 3 script that takes a list of AWS instance
IDs. It then prints out some information about the instances and then
terminates them.

I am very new to python, so still working through some basic issues.

This is the error I get when I run the script:

      python3 .\aws_ec2_termiante_instances.py
     Enter an instance ID separated by commas:
i-0463897140af217f8,i-0634fc5fa453462fb
     enter code here
     Deleting Instance IDs:
     i-0463897140af217f8
     i-0634fc5fa453462fb
     Traceback (most recent call last):
       File ".\aws_ec2_termiante_instances.py", line 37, in <module>
         NextToken='string'
       File
"C:\Users\tdunphy\AppData\Local\Programs\Python\Python37-32\lib\site-packages\botocore\client.py",
line 357, in _api_call
         return self._make_api_call(operation_name, kwargs)
       File
"C:\Users\tdunphy\AppData\Local\Programs\Python\Python37-32\lib\site-packages\botocore\client.py",
line 661, in _make_api_call
         raise error_class(parsed_response, operation_name)
     botocore.exceptions.ClientError: An error occurred
(InvalidParameterValue) when calling the DescribeInstances operation:
Unable to parse pagination token

This is my script that produces the above output:

     from collections import defaultdict
     import boto3
     # Connect to EC2
     ec2 = boto3.client('ec2')

     instance_id_list = input("Enter an instance ID separated by commas: ")
     instance_ids_text = instance_id_list.split(",")

instance_id_list is a string and instance_ids_text is a list of strings, so the names are somewhat misleading.

     print("Deleting Instance IDs:")
     for instance_id in instance_ids_text:
         print(instance_id)

     ec2info = defaultdict()
     for instance_id in instance_ids_text:
         instance = ec2.describe_instances(
             Filters=[
                 {
                     'Name': 'string',
                     'Values': [
                         'string',
                      ]
                 },
             ],
             InstanceIds=[
                 'instance_id',

Shouldn't this be the actual id, not the string 'instance_id'?

             ],
             MaxResults=123,
             NextToken='string'
         )
         for tag in instance.tags:
             if 'Name'in tag['Key']:
                 name = tag['Value']
         # Add instance info to a dictionary
         ec2info[instance.id] = {
             'Name': name,
             '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 = ['Name', '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("------")
         ec2.instances.filter(InstanceIds=instance.id).stop()
         ec2.instances.filter(InstanceIds=instance.id).terminate()

To recap, what I would like to do is give the script a list of instance
IDs, print out the list of instance IDs, then print some info about those
instances. Then terminate them.

How can I get past this error and produce that result?

--
https://mail.python.org/mailman/listinfo/python-list

Reply via email to