Python 尝试从AWS Lambda连接到Boto3客户端并接收超时

Python 尝试从AWS Lambda连接到Boto3客户端并接收超时,python,amazon-web-services,aws-lambda,boto3,Python,Amazon Web Services,Aws Lambda,Boto3,当我在Amazon虚拟私有云(Amazon VPC)之外运行AWS Lambda函数代码时,它工作正常。但是,当我将函数配置为连接到VPC时,会出现函数超时错误。我怎么修理这些 def get_db_connection_config(): # Create a Secrets Manager client. session = boto3.session.Session() client = session.client( service_name='s

当我在Amazon虚拟私有云(Amazon VPC)之外运行AWS Lambda函数代码时,它工作正常。但是,当我将函数配置为连接到VPC时,会出现函数超时错误。我怎么修理这些

def get_db_connection_config():
    # Create a Secrets Manager client.
    session = boto3.session.Session()
    client = session.client(
        service_name='secretsmanager',
        region_name=region_name
    )

    # In this sample we only handle the specific exceptions for the 'GetSecretValue' API.
    # See https://docs.aws.amazon.com/secretsmanager/latest/apireference/API_GetSecretValue.html
    # We rethrow the exception by default.

    try:
        logger.info("Retrieving MySQL database configuration...")
        get_secret_value_response = client.get_secret_value(
            SecretId=secret_name
        )

    except ClientError as error:
        logger.error(error)
        sys.exit()

    else:
        # Decrypts secret using the associated KMS CMK.
        # Depending on whether the secret is a string or binary, one of these fields will be populated.
        if 'SecretString' in get_secret_value_response:
            secret = get_secret_value_response['SecretString']
            return json.loads(secret)
        else:
            return base64.b64decode(get_secret_value_response['SecretBinary'])

当Lambda驻留在AWS网络中时,它可以使用internet连接到这些服务,但是一旦它加入您的VPC,出站internet流量也会通过您的VPC路由。由于可能没有出站互联网连接,Lambda无法连接互联网

如果您的功能需要访问internet,请使用网络地址转换(NAT)。将函数连接到公共子网不会为其提供internet访问或公共IP地址

为了使您的Lambda能够在驻留在VPC内时与其他AWS服务通信,必须具备以下条件之一

第一个选项是创建或,然后将其添加到Lambda所在的。为明确起见,此子网应仅为专用子网,因为通过对
0.0.0/0
记录使用NAT,它将停止具有共享同一子网的公共IP地址的实例的入站流量

第二种选择是,您可以利用这些服务,通过这样做,以前通过公共互联网的任何流量都将使用直接到AWS服务本身的专用连接。请注意,并不是所有AWS服务都包含在这项服务中