1
votes

I am trying to start an instance in amazon ec2,(the initial state is stopped ) and wait till the instance state changes from initializing to running as per doc given here https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-instance-lifecycle.html

here is the program for same

import sys
import boto3


instance_id = "i-03e7f6391a0f523ee"
action = 'ON'

ec2 = boto3.client('ec2')

if action == 'ON':
   response = ec2.start_instances(InstanceIds=[instance_id], DryRun=False)
else:
    response = ec2.stop_instances(InstanceIds=[instance_id], DryRun=False)
print(response)


#resp2=ec2.describe_instances()
#foo = response['Reservations'][0]['Instances'][0]['NetworkInterfaces'][0]['Association']['PublicDnsName']
#filter=[{'Name':'Association','Values':['PublicDnsName']}]

#print (foo)
#instance = ec2.resource('ec2').instance(instance_id)
#while instance.state['Name'] not in ('running', 'stopped'):
#        sleep(5)
#        print("the instance is initializing")
x2=boto3.resource('ec2')
image=x2.Image('instance_id')
foo=image.wait_until_exists('self',Filters=[{'Name':'state','Values':'avaialable'}])
print(foo)
resp=ec2.describe_network_interfaces();
print ("printing pub dns name")
print(resp['NetworkInterfaces'][0]['Association']['PublicDnsName'])

print ("going inside function to demonstrate usage of response returned from describe_instances() method")
def get_name(inst):
    client = boto3.client('ec2')
    response = client.describe_instances(InstanceIds = [instance_id])
    foo = response['Reservations'][0]['Instances'][0]['NetworkInterfaces'][0]['Association']['PublicDnsName']
    return foo


foo = get_name(instance_id)
print (foo)

The problem I am getting is in this line

foo=image.wait_until_exists('self',Filters=[{'Name':'state','Values':'avaialable'}])

I get error

Traceback (most recent call last):
  File "D:\start3.py", line 28, in <module>
    foo=image.wait_until_exists('self',Filters=[{'Name':'state','Values':'avaialable'}])
  File "E:\installation2\python3\lib\site-packages\boto3\resources\factory.py", line 369, in do_waiter
    waiter(self, *args, **kwargs)
  File "E:\installation2\python3\lib\site-packages\boto3\resources\action.py", line 202, in __call__
    response = waiter.wait(**params)
  File "E:\installation2\python3\lib\site-packages\botocore\waiter.py", line 53, in wait
    Waiter.wait(self, **kwargs)
  File "E:\installation2\python3\lib\site-packages\botocore\waiter.py", line 297, in wait
    response = self._operation_method(**kwargs)
  File "E:\installation2\python3\lib\site-packages\botocore\waiter.py", line 84, in __call__
    return self._client_method(**kwargs)
  File "E:\installation2\python3\lib\site-packages\botocore\client.py", line 314, in _api_call
    return self._make_api_call(operation_name, kwargs)
  File "E:\installation2\python3\lib\site-packages\botocore\client.py", line 586, in _make_api_call
    api_params, operation_model, context=request_context)
  File "E:\installation2\python3\lib\site-packages\botocore\client.py", line 621, in _convert_to_request_dict
    api_params, operation_model)
  File "E:\installation2\python3\lib\site-packages\botocore\validate.py", line 291, in serialize_to_request
    raise ParamValidationError(report=report.generate_report())
botocore.exceptions.ParamValidationError: Parameter validation failed:
Invalid type for parameter Filters[0].Values, value: avaialable, type: <class 'str'>, valid types: <class 'list'>, <class 'tuple'>

I am unable to understand the error, I read here https://boto3.readthedocs.io/en/latest/reference/services/ec2.html#EC2.Image.wait_until_exists but I could not fix the error. I also tried using wait_until_running from doc here instead of wait_until_exists()

x2=boto3.resource('ec2')
image=x2.wait_until_running()
foo=image.wait_until_running('self',Filters=[{'Name':'state','Values':'running'}])
print(foo)

This gives me following error

start3.py", line 27, in <module>
    image=x2.wait_until_running()
AttributeError: 'ec2.ServiceResource' object has no attribute 'wait_until_running'

There is a lot of code which I have commented out but that is what I have tried to fix this error and things did not worked.Please see the code and let me know what should I change so that it works as desired. I want to print some thing on idel shell window while the ec2 resource is waiting to get started i.e its state changes from stopped to running.That is what I am not able to achieve here.

1
Posting as a comment: you had a typo avaialable instead of available.Marco A.

1 Answers

0
votes

The exception clearly states that the Values value must be a list/tuple. The problematic line of code should be changed to use a list as follows. I've fixed the typo as well, though that wasn't the source of your immediate problem.

foo=image.wait_until_exists('self',Filters=[{'Name':'state','Values':['available']}])