我正在嘗試在Amazon EC2中啟動實例(初始狀態(tài)為stop),然后等待實例狀態(tài)從初始化變?yōu)楦鶕?jù)此處給出的文檔運行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'}])有很多代碼我已經(jīng)注釋掉了,但這就是我試圖解決這個錯誤的原因,但事情沒有奏效。請查看代碼并讓我知道我應(yīng)該更改什么以使其按預(yù)期工作。我想在ec2資源正在等待啟動的同時在idel shell窗口上打印一些東西,即它的狀態(tài)從停止變?yōu)檫\行。這是我在這里無法實現(xiàn)的。
1 回答

嚕嚕噠
TA貢獻1784條經(jīng)驗 獲得超7個贊
異常明確指出該Values值必須是列表/元組。有問題的代碼行應(yīng)該更改為使用如下列表。我也修復(fù)了錯字,盡管這不是您眼前問題的根源。
foo=image.wait_until_exists('self',Filters=[{'Name':'state','Values':['available']}])
添加回答
舉報
0/150
提交
取消