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 Risposta
1
Risposta accettata

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
con risposta 2 mesi fa
profile picture
ESPERTO
verificato 2 mesi fa
profile picture
ESPERTO
verificato 2 mesi fa
  • 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.

Accesso non effettuato. Accedi per postare una risposta.

Una buona risposta soddisfa chiaramente la domanda, fornisce un feedback costruttivo e incoraggia la crescita professionale del richiedente.

Linee guida per rispondere alle domande