gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 02:50:31 25 4
gpt4 key购买 nike

当我在 Amazon Virtual Private Cloud (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 网络中时,它能够使用互联网连接到这些服务,但是一旦它加入您的 VPC,出站互联网流量也会通过您的 VPC 进行路由。由于可能没有出站互联网连接,Lambda 无法访问互联网。

If your function needs internet access, use network address translation (NAT). Connecting a function to a public subnet doesn't give it internet access or a public IP address.

要使您的 Lambda 在位于 VPC 中时能够与其他 AWS 服务通信,必须具备以下条件之一。

第一个选项是创建一个 NAT gatewayNAT instance , 然后将其添加到 route table您的 Lambda 所在的位置。需要明确说明的是,此子网应该只是一个私有(private)子网,因为通过对 0.0.0.0/0 记录使用 NAT,它将停止到具有公共(public) IP 地址的实例的入站流量共享同一子网。

第二个选项是您利用 VPC endpoints对于这些服务,通过这样做,之前会遍历公共(public)互联网的任何流量都将改为使用直接连接到 AWS 服务本身的专用连接。请注意,目前并未涵盖所有 AWS 服务。

关于python - 尝试从 AWS Lambda 连接到 Boto3 客户端并接收超时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63647970/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com