[英]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.