Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Increasing AWS Lambda response body payload size

I am using AWS API gateway to invoke my aws lambda functions. They are REST web-services that will return response back to the caller. Some of the responses are more than 20 MB. Due to the nature of the response data (data is polylines that represents complex structures) I cannot use pagination here. AWS Lambda got the limitation of lambda response body cannot exceed 6MB size and due to this limitation some of my responses that are above 6MB are failing with 'body size is too long' message. I would like to know is there a way to increase the 6MB limitation of lambda responses?

like image 622
S.Jose Avatar asked Aug 03 '18 15:08

S.Jose


People also ask

Can we increase Lambda payload size?

Function configuration, deployment, and executionThey cannot be changed. The Lambda documentation, log messages, and console use the abbreviation MB (rather than MiB) to refer to 1024 KB. 128 MB to 10,240 MB, in 1-MB increments.

What is Lambda payload limit?

Lambda Payload Limit There is a hard limit of 6mb when it comes to AWS Lambda payload size. This means we cannot send more than 6mb of data to AWS Lambda in a single request.

What is the payload size limit of the Lambda synchronous invocation mode for both the request and the response?

AWS Lambda Payload Size Limit There is a 6 MB payload size limit for synchronous invocations and a 250 KB size limit for asynchronous invocations.


1 Answers

Thanks for the comments, I have implemented in the same way as mentioned in the comments

Uploaded the response to s3 bucket, and provided a redirect url to the caller. Here is the sample code that works. You may also enable CORS at s3 bucket level to make it work well. You may change to the correct region

public String uploadToS3(Set<MyClass> myclassSet, String bucketName) throws IOException {
    Region region = Regions.getCurrentRegion();
    if (region == null) {
      region = Region.getRegion(Regions.US_EAST_1);
    }
    AmazonS3 s3Client = AmazonS3ClientBuilder.standard()
                                .withRegion(region.getName())
                                .withCredentials(DefaultAWSCredentialsProviderChain.getInstance())
                                .build();
    ObjectMapper mapper = new ObjectMapper();
    String jsonClass = mapper.writeValueAsString(myclassSet);
    s3Client.putObject(bucketName, "random_key.json", jsonClass);
    GeneratePresignedUrlRequest req = new GeneratePresignedUrlRequest(bucketName, "random_key.json");
    URL url = s3Client.generatePresignedUrl(req);
    String redirectUrl = url.toString();
    return redirectUrl;
  }
like image 124
S.Jose Avatar answered Oct 30 '22 01:10

S.Jose