【问题标题】:boto3 python to start EC2boto3 python启动EC2
【发布时间】:2020-09-30 20:40:02
【问题描述】:

我正在使用下面的 python boto3 代码来启动 Ec2

import boto3
region='us-east-1'
instance_id = 'i-06ce851edfXXXXXX'
ec2 = boto3.client('ec2', region_name=region)

def lambda_handler(event, context):
resp = ec2.describe_instance_status(InstanceIds=[str(instance_id)],
        IncludeAllInstances=True)
print("Response = ",resp)

instance_status = resp['InstanceStatuses'][0]['InstanceState']['Code']
print("Instance status =", instance_status)

if instance_status == 80:
    ec2.start_instances(InstanceIds=[instance_id])
    print("Started instance with Instance_id",instance_id)

elif instance_status == 16:
     ec2.stop_instances(InstanceIds=[instance_id])
     print("Stopped EC2 with Instance-ID",instance_id)
else:
     print("No desired state found")

当实例处于运行状态时,我可以通过运行这个 lambda 来停止实例。

但是当实例处于停止状态并且我运行 Lambda 时,我收到以下消息并且它没有显示错误。但是当我签入控制台实例仍处于停止状态时。我无法找出实例没有进入的原因运行阶段。 实例状态 = 80 使用 Instance_id i-06ce851edfXXXXXX 启动实例

以下是使用的 IAM 角色

 {
    "Action": [
        "ec2:StopInstances",
        "ec2:StartInstances",
        "ec2:RebootInstances"
    ],
     "Resource": [
             "arn:aws:ec2:us0east-1:2x83xxxxxxxxxx:instance/i-06ce851edfXXXXXX"
      ],
     "Effect": "Allow"

【问题讨论】:

  • 您是否检查过 CloudWatch 日志中的输出?
  • 是的,和我上面给出的一样。

标签: python-3.x amazon-web-services amazon-ec2 aws-lambda boto3


【解决方案1】:

您的代码正在运行。我用我的 lambda 在 我的测试实例 上验证了它。

为了便于阅读,我对其进行了重新格式化,但它无需任何更改即可工作(实例 ID 除外)。我可以停止运行实例。然后我可以启动停止的实例。

需要注意的一点是停止和启动需要时间。如果您快速执行函数,它将无法以stopping 状态启动实例。也许这就是你认为它不起作用的原因。

还要确保将 lambda 的默认超时时间从 3 秒增加到 10 秒或更多。

import boto3

region='us-east-1'
instance_id = 'i-08a1e399b3d299c2d'

ec2 = boto3.client('ec2', region_name=region)

def lambda_handler(event, context):

    resp = ec2.describe_instance_status(
            InstanceIds=[str(instance_id)],
            IncludeAllInstances=True)

    print("Response = ",resp)

    instance_status = resp['InstanceStatuses'][0]['InstanceState']['Code']

    print("Instance status =", instance_status)

    if instance_status == 80:
        ec2.start_instances(InstanceIds=[instance_id])
        print("Started instance with Instance_id",instance_id)

    elif instance_status == 16:
         ec2.stop_instances(InstanceIds=[instance_id])
         print("Stopped EC2 with Instance-ID",instance_id)
    else:
         print("No desired state found")

【讨论】:

  • 我没有快速运行 lambda,而且我在控制台中检查了当前状态已停止但当我运行时它仍然无法启动。lambda 超时为 1 分钟。
  • @AWS_Beginner 因此,除了这段代码之外,肯定还有其他事情发生。所以对你来说,你只能停止一个实例,但不能使用这段代码启动它?
  • 我在帖子中添加了我正在使用的角色。IAM 角色可能有问题
  • @AWS_Beginner 我明白了。它应该是ec2:StartInstances 而不是ec2.StartInstances。其他动作也一样。我会更新我的答案。
  • 它的 ec2:仅在我的角色中。这是我的错字。我现在纠正了我的问题。
【解决方案2】:

我发现了问题。EC2 的根卷已加密,因此我在角色中添加了 KMS 权限并且它起作用了。

【讨论】:

  • 您能否分享一下您附加到 ec2 角色的 KMS 政策,我也面临类似的问题。
【解决方案3】:

确实,根卷的加密是这里的问题。 您可以为角色添加内联策略:

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "kms:*"
            ],
            "Resource": "*"
        }]
}

请注意,它将授予 KMS 对所有资源的完全访问权限。如果需要,您可以将此策略的范围限制为某些特定资源。

关于这个问题的更多信息在这里:https://aws.amazon.com/premiumsupport/knowledge-center/encrypted-volumes-stops-immediately/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-06-07
    • 1970-01-01
    • 2020-01-22
    • 2019-02-25
    • 2016-08-20
    • 2019-06-22
    • 2017-05-21
    相关资源
    最近更新 更多