kol*_*man 1 python amazon-web-services boto3
我正在尝试使用 boto3 编写一个脚本来启动一个实例并等待它启动。根据wait_until_running的文档,它应该等到实例完全启动(我假设检查应该没问题)但不幸的是它只适用于wait_until_stopped和incase wait_until_running它只是启动实例而不是等到它完全开始了。不确定我在这里做错了什么,或者这是 boto3 的错误。
这是代码:
import boto3
ec2 = boto3.resource('ec2',region_name="ap-southeast-2")
ec2_id = 'i-xxxxxxxx'
instance = ec2.Instance(id=ec2_id)
print("starting instance " + ec2_id)
instance.start()
instance.wait_until_running()
print("instance started")
Run Code Online (Sandbox Code Playgroud)
感谢@Mark B @Madhurya Gandi,这里是适用于我的解决方案:
import boto3,socket
retries = 10
retry_delay=10
retry_count = 0
ec2 = boto3.resource('ec2',region_name="ap-southeast-2")
ec2_id = 'i-xxxxxxxx'
instance = ec2.Instance(id=ec2_id)
print("starting instance " + ec2_id)
instance.start()
instance.wait_until_running()
while retry_count <= retries:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
result = sock.connect_ex((instance.public_ip_address,22))
if result == 0:
Print "Instance is UP & accessible on port 22, the IP address is: ",instance.public_ip_address
break
else:
print "instance is still down retrying . . . "
time.sleep(retry_delay)
except ClientError as e:
print('Error', e)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5845 次 |
| 最近记录: |