Cannot access S3 from lambda using boto3

0

Hello,

I have a lambda function attached to 3 subnets having the same route table in the VPC with a security group allowing all inbound and outbound IPV4 traffic. The route table has an IGW attached to route all IPv4 traffic (0.0.0.0/0). In the lambda code, I use the boto3 library to access an S3 bucket but I get a timeout. I had to attach an S3 gateway endpoint to the VPC to get the lambda function working.

My thoughts are since I already attached my lambda to subnets that have an IGW attached with all IPV4 traffic allowed, it should have worked the first time. Any thoughts on what might be the cause?

1 Answer
1
Accepted Answer

Lambda functions attached to a VPC use private IP addresses and can't use an IGW. You'll need to use a a NAT instance or NAT gateway to give the Lambda function access to the internet. You can also use a VPC endpoint to access S3 and not need to give the Lambda function access to the internet. You need to use an interface VPC endpoint and not a gateway VPC endpoint to connect the Lambda function to S3.

With all this noted, does the Lambda function need to be attached to the VPC? We only recommend attaching to a VPC if the Lambda function needs access to resources in the VPC.

AWS
answered 2 months ago
profile picture
EXPERT
reviewed a month ago
profile picture
EXPERT
reviewed 2 months ago
  • S3 gateway will also work for buckets in the same region as the lambda function exists.

  • The reason for attaching the lambda to VPC is that we want to attach an API gateway to the lambda so that we can implement authorization.

  • You don't need to attach a Lambda function to a VPC to integrate it with API Gateway. You can just integrate API Gateway with the Lambda function and the integration will communicate privately over the AWS backbone and not use the public internet.

    Examples of private resources in the VPC are databases, cache instances, or internal services and if the Lambda function needs to connect to any of those then you would need to attach it to the VPC. Otherwise, it is best practices not to do it.

  • Thanks for the heads up. We currently have RDS connections from lambda as well which crosses through public internet. Those are to be replaced with private connections as well. In that case, we need the lambda VPC attachment.

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions