我正在尝试在Amazon EC2中启动实例(初始状态为stop),然后等待实例状态从初始化变为根据此处给出的文档运行https://docs.aws.amazon.com/AWSEC2/latest/ UserGuide / ec2-instance-lifecycle.html
这是相同的程序
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)
我遇到的问题是在这一行
foo=image.wait_until_exists('self',Filters=[{'Name':'state','Values':'avaialable'}])
有很多代码我已经注释掉了,但这就是我试图解决这个错误的原因,但事情没有奏效。请查看代码并让我知道我应该更改什么以使其按预期工作。我想在ec2资源正在等待启动的同时在idel shell窗口上打印一些东西,即它的状态从停止变为运行。这是我在这里无法实现的。
噜噜哒
相关分类