简体   繁体   English

EC2 实例 state 返回挂起,即使它在 lambda function 中运行

[英]EC2 instance state return pending even when it is running in a lambda function

I made a lambda function to manage EC2 instances.我做了一个 lambda function 来管理 EC2 实例。 To create the instance I use this code in the lambda function:要创建实例,我在 lambda function 中使用此代码:

instance = boto3.resource('ec2', region_name='eu-west-3').Instance(instance_id)

To know the instance state, I get the state code as following:要知道实例 state,我得到 state 代码如下:

code = instance.state['Code']

When the instance is stopped, the code is 80, then, I can use instance.start() to launch the instance.当实例停止时,代码为 80,然后,我可以使用 instance.start() 启动实例。 Inmediately after that the code become 0 (pending) while the instance is launching.紧接着,代码在实例启动时变为 0(待处理)。 The problem is, when I see the instance already running in the EC2 console, the function still returning code 0 instead of code 16 (running).问题是,当我看到实例已经在 EC2 控制台中运行时,function 仍然返回代码 0 而不是代码 16(正在运行)。 But, if I deploy again the lambda function and run it, now yes, I got code 16, anyone knows the reason of this behaviour?但是,如果我再次部署 lambda function 并运行它,现在是的,我得到了代码 16,有人知道这种行为的原因吗?

You should call the instance.load() method to update the status, otherwise it is just retrieving the previous result:您应该调用instance.load()方法来更新状态,否则它只是检索先前的结果:

Calls EC2.Client.describe_instances() to update the attributes of the Instance resource.调用EC2.Client.describe_instances()以更新Instance资源的属性。 Note that the load and reload methods are the same method and can be used interchangeably.请注意,load 和 reload 方法是相同的方法,可以互换使用。

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

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