19

I'm trying to simply list all the files in an S3 bucket using Lambda

The code looks as follows:

var AWS = require('aws-sdk');
var s3 = new AWS.S3();

exports.handler = (event, context, callback) => {

   s3.listObjectsV2({
       Bucket: "bucketname",
   }, function(err, data) {
       console.log("DONE : " + err + " : " + data); 

       callback(null, 'Hello from Lambda');
    });
};

Using the above, I never get the "DONE" printed at all. The log doesn't show any information except for the fact that it timed out.

Is there any troubleshooting I could do here? I would've thought that at least the error would've been shown in the "DONE" section.

Zeus
  • 369
  • 1
  • 4
  • 13
  • 6
    Is your lambda function running inside a VPC? If so, does its subnet have access to the Internet via a NAT instance or NAT gateway, or direct S3 access via an S4 VPC endpoint? – Michael - sqlbot Aug 16 '16 at 00:33
  • Possible duplicate of [Adding AWS Lambda with VPC configuration causes timeout when accessing S3](http://stackoverflow.com/questions/35423246/adding-aws-lambda-with-vpc-configuration-causes-timeout-when-accessing-s3) – Mark B Aug 16 '16 at 02:10

4 Answers4

14

Thanks to Michael above. The problem was that it was running inside a VPC. If I change it to No VPC, it works correctly. Your solution may be different if you require it to run in a VPC.

Zeus
  • 369
  • 1
  • 4
  • 13
6

If you are running your code inside the VPC, Make sure VPC subnet and its routing table entry should be proper (routing : Dest= 0.0.0.0/0 and target = igw-xxxx). Also VPC endpoint routing must be added in order to communicate to s3 via endpoint.

palani.p
  • 431
  • 1
  • 7
  • 11
4

In my case I have selected 2 different subnets, 1 is private and other is public. So it was working sometimes and sometimes not. I changed both subnets to private (having NAT gateway in route) and now it that worked without timeout error.

ExploringApple
  • 1,000
  • 13
  • 27
4

If you are running your code inside VPC make sure to create VPC Endpoint.
Here is the tutorial: https://aws.amazon.com/blogs/aws/new-vpc-endpoint-for-amazon-s3/

Dean Koštomaj
  • 440
  • 4
  • 10