Search code examples
node.jsamazon-web-servicesamazon-s3aws-lambda

Lambda function S3 small file download takes forever


I am writing an AWS lambda function in nodeJS to download a small (372 B) sql file and then run that file in RDS. The problem is that, i cant download this file with lambda function, as it will never completes... (I try to stay in free tier so the biggest time that i was tried to execute it was 1 minute & 2048 MB memory but that still timeouted). Max used memory was 104 MB.

I tried to run the full lambda function in my local machine, and there it was working, completed in 2-3 secs. The code for S3 retrieve is:

const { GetObjectCommand, S3Client } = require("@aws-sdk/client-s3");

async function getResourceFromS3() {
    const s3Client = new S3Client({});
    const command = new GetObjectCommand({
        Bucket: "devops-yamls",
        Key: "resource/test-init.sql"
    });

    const s3Response = await s3Client.send(command);
    const sqlStatement = s3Response.Body.transformToString();
    return sqlStatement;
}

Solution

  • I assume you have configured your Lambda function to run in the same VPC as your RDS instance. A Lambda function configured to run in a VPC will not have access to the Internet. The AWS S3 API is on the public Internet, so with your current configuration the Lambda function does not have access to the S3 API, so it is just hanging and timing out trying to access S3.

    You should add a Gateway endpoints for Amazon S3 to your VPC, to provide direct S3 access for your Lambda function, and any other resources in your VPC, without having to go over the Internet.


    To give your VPC Lambda function full access to the Internet, you would need to deploy it to a subnet that has a route to a NAT Gateway. However NAT Gateways are not free and you are trying to stay within the free-tier.