[英]How to Stop and Start EC2 Instance using boto3 and lambda Function
[英]How to wait untill EC2 (created in Lambda) 2/2 status check is passed in Python using Boto3 and Calling Lambda function from Shell script
我正在執行的用例是:
等待的主要目的是:
我從外殼程序腳本(shellScript.sh)調用Lambda函數,並且要調用的命令在以下代碼中,該代碼來自AWS文檔。
在Lambda函數中,我具有以下python代碼,首先創建EC2實例,然后檢測EC2是否正在運行以及2/2狀態檢查,但是當“實例狀態”顯示為運行時,它退出並向shell腳本返回200成功代碼( Lambda仍會繼續運行其他命令(此處未提及,並且EC2狀態處於“初始化”狀態)。
在EC2“狀態檢查”顯示“通過2/2檢查”之前,應該使用哪個API函數阻止? (我不希望我的Lambda函數退出,直到狀態顯示通過2/2檢查為止。)
注意:打印響應的狀態為“ ok”
我參考了下面的堆棧溢出鏈接並嘗試了這些選項(如下面的代碼所示),但對我而言不起作用。
如何阻止直到使用Python Boto3通過EC2狀態檢查?
ShellScript.sh:
aws lambda invoke --invocation-type RequestResponse --function-name <my-function-name> --payload '{"Key": "'$value'"}' /dev/stdout
Lambda_function.py:
instance = EC2.run_instances(
ImageId=<image-id>,
InstanceType=<instance-type>,
MinCount=1,
MaxCount=1,
.......
)
instance_id = instance['Instances'][0]['InstanceId']
ec2instance = ec2.Instance(id=instance_id)
ec2instance.wait_until_running()
waiter = EC2.get_waiter('instance_status_ok')
waiter.wait(
InstanceIds=[instance_id],
WaiterConfig={
'Delay': 15,
'MaxAttempts': 40
},
IncludeAllInstances = True
)
response = EC2.describe_instance_status(
InstanceIds=[instance_id],
IncludeAllInstances=True
)
print(response)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.