gpt4 book ai didi

amazon-web-services - 在访问S3时添加具有VPC配置的AWS Lambda会导致超时

转载 作者:行者123 更新时间:2023-12-03 08:56:28 26 4
gpt4 key购买 nike

我试图从AWS Lambda访问VPC上的S3和资源,但是由于我将AWS Lambda配置为访问VPC,因此访问S3时超时。
这是代码

from __future__ import print_function

import boto3
import logging
import json

print('Loading function')

s3 = boto3.resource('s3')

import urllib

def lambda_handler(event, context):
logging.getLogger().setLevel(logging.INFO)
# Get the object from the event and show its content type
bucket = event['Records'][0]['s3']['bucket']['name']
key = urllib.unquote_plus(event['Records'][0]['s3']['object']['key']).decode('utf8')
print('Processing object {} from bucket {}. '.format(key, bucket))
try:
response = s3.Object(bucket, key)
content = json.loads(response.get()['Body'].read())
# with table.batch_writer() as batch:
for c in content:
print(' Processing Item : ID' + str(c['id']))
# ##################
# Do custom processing here using VPC resources
# ##################
except Exception as e:
print('Error while processing object {} from bucket {}. '.format(key, bucket))
print(e)
raise e

我已经为子网和安全组设置了适当的出站规则,以访问Internet,如下所示,但是我的Lambda在访问S3时只是超时。

enter image description here

enter image description here

这也是测试输入的示例
# Test Event Configuration
{
"Records": [
{
"awsRegion": "us-east-1",
"eventName": "ObjectCreated:Put",
"eventSource": "aws:s3",
"eventTime": "2016-02-11T19:11:46.058Z",
"eventVersion": "2.0",
"requestParameters": {
"sourceIPAddress": "54.88.229.196"
},
"responseElements": {
"x-amz-id-2": "ljEg+Y/InHDO8xA9c+iz6DTKKenmTaGE9UzHOAabarRmpDF1z0eUJBdpGi37Z2BU9nbTh4p7oZg=",
"x-amz-request-id": "3D98A2325EC127C6"
},
"s3": {
"bucket": {
"arn": "arn:aws:s3:::social-gauge-data",
"name": "social-gauge-data",
"ownerIdentity": {
"principalId": "A1NCXDU7DLYS07"
}
},
"configurationId": "b5540417-a0ac-4ed0-9619-8f27ba949694",
"object": {
"eTag": "9c5116c70e8b3628380299e39e0e9d33",
"key": "posts/test/testdata",
"sequencer": "0056BCDCF1F544BD71",
"size": 72120
},
"s3SchemaVersion": "1.0"
},
"userIdentity": {
"principalId": "AWS:AROAIUFL6WAMNRLUBLL3K:AWSFirehoseDelivery"
}
}
]
}

最佳答案

在Lambda中启用VPC支持后,您的功能将无法再访问VPC之外的任何内容,包括S3。特别是在S3中,您可以使用VPC Endpoints解决此问题。对于VPC之外的几乎所有其他内容,您需要在VPC中创建NAT实例或托管NAT网关,以将流量从Lambda函数路由到VPC之外的终结点。

我将阅读Lambda VPC support announcement,并特别注意最后的“要了解的事情”部分。

关于amazon-web-services - 在访问S3时添加具有VPC配置的AWS Lambda会导致超时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35423246/

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