我正在尝试在Amazon EC2中启动实例(初始状态为stop),然后等待实例状态从初始化变为根据此处给出的文档运行https://docs.aws.amazon.com/AWSEC2/latest/ UserGuide / ec2-instance-lifecycle.html这是相同的程序import sysimport boto3instance_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 foofoo = get_name(instance_id)print (foo)我遇到的问题是在这一行foo=image.wait_until_exists('self',Filters=[{'Name':'state','Values':'avaialable'}])有很多代码我已经注释掉了,但这就是我试图解决这个错误的原因,但事情没有奏效。请查看代码并让我知道我应该更改什么以使其按预期工作。我想在ec2资源正在等待启动的同时在idel shell窗口上打印一些东西,即它的状态从停止变为运行。这是我在这里无法实现的。
1 回答
噜噜哒
TA贡献1784条经验 获得超7个赞
异常明确指出该Values值必须是列表/元组。有问题的代码行应该更改为使用如下列表。我也修复了错字,尽管这不是您眼前问题的根源。
foo=image.wait_until_exists('self',Filters=[{'Name':'state','Values':['available']}])
添加回答
举报
0/150
提交
取消