错误:运行时退出时未在 python lambda 中提供原因

vic*_*ick 8 aws-lambda python-3.8

该代码用于执行以下操作

  1. 该 lambda 通过事件规则触发
  2. 当实例状态转为运行/终止时发送事件
  3. 当实例运行时,实例属性将保存到 dynamodb 表中
  4. 当实例处于运行/终止状态时,Route53记录集被创建/删除。
  5. 现在 lambda 在实例启动时创建记录并抛出以下错误

RequestId:9f4fb9ed-88db-442a-bc4f-079744f5bbcf 错误:运行时退出但未提供原因 Runtime.ExitError

import ipaddress
import os
import time
from datetime import datetime
from typing import Dict, List, Optional

import boto3
from botocore.exceptions import ClientError, ParamValidationError
from pynamodb.attributes import UnicodeAttribute, UTCDateTimeAttribute
from pynamodb.exceptions import DoesNotExist
from pynamodb.models import Model



def lambda_handler(event, context):
    """Registers or de-registers private DNS resource records for a given EC2 instance."""

    # Retrieve details from invocation event object.
    try:
        account_id = event["account"]
        instance_id = event["detail"]["instance-id"]
        instance_region = event["region"]
        instance_state = event["detail"]["state"]
    except KeyError as err:
        raise RuntimeError(
            f"One or more required fields missing from event object {err}"
        )

    print(
        f"EC2 instance {instance_id} changed to state `{instance_state}` in account "
        f"{account_id} and region {instance_region}."
    )
    print(f"Creating a new aws session in {instance_region} for account {account_id}.")

    target_session = aws_session(
    region=instance_region, account_id=account_id, assume_role=ASSUME_ROLE_NAME,
    )

    print(f"Retrieving instance and VPC attributes for instance {instance_id}.")
    instance_resource = get_instance_resource(instance_id, target_session)
    vpc_resource = get_vpc_resource(instance_resource.vpc_id, target_session)
    route53_client = target_session.client("route53")

    print(f"Retrieving DNS configuration from VPC {instance_resource.vpc_id}.")
    forward_zone = get_vpc_domain(vpc_resource)

    print(f"Calculating reverse DNS configuration for instance {instance_id}.")
    reverse_lookup = determine_reverse_lookup(instance_resource, vpc_resource)

    if instance_state == "running":
        print(f"Building DNS registration record for instance {instance_id}.")
        #vpc_resource = get_vpc_resource(instance_resource.vpc_id, target_session)
        #print(f"Retrieving DNS configuration from VPC {instance_resource.vpc_id}.")
        #forward_zone = get_vpc_domain(vpc_resource)
        #print(f"Calculating reverse DNS configuration for instance {instance_id}.")
        #reverse_lookup = determine_reverse_lookup(instance_resource, vpc_resource)
        record = Registration(
            account_id=account_id,
            hostname=generate_hostname(instance_resource),
            instance_id=instance_resource.id,
            forward_zone=forward_zone,
            forward_zone_id=get_zone_id(forward_zone, route53_client),
            private_address=instance_resource.private_ip_address,
            region=instance_region,
            reverse_hostname=reverse_lookup["Host"],
            reverse_zone=reverse_lookup["Zone"],
            reverse_zone_id=get_zone_id(reverse_lookup["Zone"], route53_client),
            vpc_id=instance_resource.vpc_id,
        )
        print(record)
        try:
            if record.forward_zone_id is not None:
                manage_resource_record(record, route53_client)
            if record.forward_zone_id and record.reverse_zone_id is not None:
                manage_resource_record(record, route53_client, record_type="PTR")
        except RuntimeError as err:
            print(f"An error occurred while creating records: {err}")
            exit(os.EX_IOERR)

        if record.forward_zone_id:
            print(
                f"Saving DNS registration record to database for instance {instance_id}."
            )
            record.save()
        else:
            print(
                f"No matching hosted zone for {record.forward_zone} associated "
                f"with {record.vpc_id}."
            )
    else:
        try:
            print(
                f"Getting DNS registration record from database for instance {instance_id}."
            )
            record = Registration.get(instance_id)
            if record.forward_zone_id is not None:
                manage_resource_record(record, route53_client, action="DELETE")
            if record.reverse_zone_id is not None:
                manage_resource_record(record, route53_client, record_type="PTR", action="DELETE")
            print(
                "Deleting DNS registration record from database for "
                f"instance {instance_id}."
            )
            record.delete()
        except DoesNotExist:
            print(f"A registration record for instance {instance_id} does not exist.")
            exit(os.EX_DATAERR)
        except RuntimeError as err:
            print(f"An error occurred while removing resource records: {err}")
            exit(os.EX_IOERR)

    exit(os.EX_OK)
Run Code Online (Sandbox Code Playgroud)

Stu*_*Stu 6

我在使用 dotnet lambda 时遇到了这个问题。结果发现内存不足了。提高内存上限让它可以通过。


vic*_*ick 1

最后一行中的 exit(os.EX_OK) 语句导致了此问题。删除这一行解决了我的问题。