Cannot access S3 bucket using flask+lambda

0

I made a lambda function with the following code (runtime python3.7). The role of the lambda has "AmazonS3FullAccess". However, I got "botocore.exceptions.ConnectTimeoutError" at the line "ret= client.list_objects_v2(Bucket='myfiles');".

What is the problem? How can flask+lambda access S3 objects?

import awsgi
from flask import Flask
import boto3;

app = Flask(__name__)

def lambda_handler(event,context):
    return awsgi.response(app,event,context);

@app.route("/",methods=["GET"])
def index():
  s3 = boto3.resource('s3');
  client = s3.meta.client;
  ret= client.list_objects_v2(Bucket='myfiles');
  print(ret);
  return "hello world";

nemy
질문됨 2년 전395회 조회
1개 답변
0
수락된 답변

Is your lambda function VPC enabled? If not, you should not have any issues connecting to S3 from your lambda function, unless there are bucket policies preventing access.

If your lambda function is VPC enabled, you can create a NAT Gateway in your VPC as mentioned here - https://aws.amazon.com/premiumsupport/knowledge-center/internet-access-lambda-function/

Another better option is to create an interface VPC endpoint for S3 in your VPC. That way the lambda function will be able to connect to the S3 endpoint over the Amazon network without needing to go over the internet - https://docs.aws.amazon.com/AmazonS3/latest/userguide/privatelink-interface-endpoints.html

profile pictureAWS
전문가
답변함 2년 전
  • Many thanks! It is solved!

로그인하지 않았습니다. 로그인해야 답변을 게시할 수 있습니다.

좋은 답변은 질문에 명확하게 답하고 건설적인 피드백을 제공하며 질문자의 전문적인 성장을 장려합니다.

질문 답변하기에 대한 가이드라인

관련 콘텐츠