This repository is a demo sample that Lambda response large payload ( > 6MB) using Lambda Streaming Response via CloudFront.
This is not for production, use it for testing.
The following is an architecture deployed by CDK.
It is not practical because it only retrieves and responds to files stored in S3. This is done for simplicity.
All resources for this project are provisioned by the AWS Cloud Development Kit (CDK). If you have not installed the CDK, first install it by referring to this document.
git clone https://github.com/msysh/aws-sample-lambda-stream-response-via-cloudfront.git
cd aws-sample-lambda-stream-response-via-cloudfront
cdk deploy
After completion deployment, you can get following values and so on. Please note them.
- CloudFront Distribution URL (
OutputCloudFrontDistributionUrl
) - S3 Bucket name (
OutputS3BucketName
)
Upload a large file (> 6MB) to S3 for Lambda Streaming Response.
aws s3 cp large.pdf s3://${BUCKET_NAME}/
${BUCKET_NAME}
is able to get as OutputS3BucketName
from CDK Output.
Request to Lambda Function URL via CloudFront
curl -v https://${CLOUDFRONT_DOMAIN_NAME}/large.pdf
https://${CLOUDFRONT_DOMAIN_NAME}
is able to get as OutputCloudFrontDistributionUrl
from CDK Output.
You should get the following output:
* (snip)
> GET /large.pdf HTTP/2
> Host: dXXXXXXXXXXXXX.cloudfront.net
> User-Agent: curl/8.4.0
> Accept: */*
>
< HTTP/2 200
< content-type: application/pdf
< (snip)
{ [15922 bytes data]
You can see that the response is returned in streaming mode.
If you request with --http1.1
, the HTTP response header will be set to Transfer-Encoding: chunked
> (snip)
> GET /large.pdf HTTP/1.1
> Host: dXXXXXXXXXXXXX.cloudfront.net
> User-Agent: curl/8.4.0
> Accept: */*
>
< Content-Type: application/pdf
< Transfer-Encoding: chunked
< Connection: keep-alive
< (snip)
0 0 0 0 0 0 0 0 --:--:-- 0:00:02 --:--:-- 0{ [16384 bytes data]
100 6858k 0 6858k 0 0 1352k 0 --:--:-- 0:00:05 --:--:-- 1792k
Remove resources by CDK.
cdk destroy
MIT