相关疑难解决方法(0)

使用VPC配置添加AWS Lambda会导致访问S3时出现超时

我正在尝试从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 …
Run Code Online (Sandbox Code Playgroud)

amazon-s3 amazon-web-services amazon-vpc aws-lambda

31
推荐指数
1
解决办法
1万
查看次数