简体   繁体   English

如何等到EC2(在Lambda中创建)2/2状态检查在Python中使用Boto3和从Shell脚本调用Lambda函数通过

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

The Use case that I am executing is: 我正在执行的用例是:

  1. Create EC2 instance 创建EC2实例
  2. In that wait till that EC2 instance is up and running 在那之后,直到该EC2实例启动并运行
  3. Deploy build by running a shell script in EC2 instance 通过在EC2实例中运行Shell脚本来部署构建
  4. Get the success response from shell script 从shell脚本获取成功响应
  5. Terminate the instance 终止实例
  6. After the instance is terminated and the response of running shell script is with success code I need to run different pipeline in Jenkins which will in turn call creating EC2 instance and so on. 在实例终止并且运行shell脚本的响应是成功代码之后,我需要在Jenkins中运行不同的管道,这反过来又将调用创建EC2实例,依此类推。

The main purpose of waiting is: 等待的主要目的是:

  1. If my lambda function exit without executing the entire code my Build will not be deployed and next jenkins pipeline (step 6) will run which I don't want until I complete the first deployment successfully. 如果我的lambda函数退出而不执行完整的代码,则将不会部署Build,并且将运行下一个jenkins管道(第6步),直到成功完成第一个部署,我才不需要这样做。
  2. If there is any error in build deploy (Step 3), it should return that error and should not run my next pipeline this I am not able to handle as my response code is returned well before my EC2 instances is running. 如果构建部署(第3步)中存在任何错误,它应该返回该错误,并且不应该运行我无法处理的下一个管道,因为在EC2实例运行之前就已很好地返回了响应代码。

I am invoking the Lambda function from shell script (shellScript.sh), and the command to invoke is in below code which is from AWS documentation. 我从外壳程序脚本(shellScript.sh)调用Lambda函数,并且要调用的命令在以下代码中,该代码来自AWS文档。

In the Lambda function I have following python code to create an EC2 instance first and then detect whether an EC2 is running along with 2/2 status check, but it exits and return 200 success code to shell script when "instance state" shows running (Lambda still continues with other commands to run after that which are not mentioned here and EC2 status is in Initializing state). 在Lambda函数中,我具有以下python代码,首先创建EC2实例,然后检测EC2是否正在运行以及2/2状态检查,但是当“实例状态”显示为运行时,它退出并向shell脚本返回200成功代码( Lambda仍会继续运行其他命令(此处未提及,并且EC2状态处于“初始化”状态)。

Which API function should I use to block until EC2 "status check" show "2/2 checks passed"? 在EC2“状态检查”显示“通过2/2检查”之前,应该使用哪个API函数阻止? (I don't want my Lambda function to exit until the status shows 2/2 checks passed.) (我不希望我的Lambda函数退出,直到状态显示通过2/2检查为止。)

Note: Print response gives the status of 'ok' 注意:打印响应的状态为“ ok”

I referred to Stack overflow link below and tried those options (as seen in my code below) but it didn't work for me. 我参考了下面的堆栈溢出链接并尝试了这些选项(如下面的代码所示),但对我而言不起作用。

How to block until EC2 status check is passed using Python Boto3? 如何阻止直到使用Python Boto3通过EC2状态检查?

ShellScript.sh: ShellScript.sh:

aws lambda invoke --invocation-type RequestResponse --function-name <my-function-name> --payload '{"Key": "'$value'"}' /dev/stdout

Lambda_function.py: 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)

boto3 wait_until_running doesn't work as desired boto3 wait_until_running无法正常工作

@ UNH Intern, @ UNH实习生,

Try the attached link and see if it is help in your case 尝试附件中的链接,看看是否对您有帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM