Skip to content

Commit b793a25

Browse files
committed
feat(cloudfront): support for includeBody for Lambda@Edge
The `includeBody` flag enables a Lambda@Edge function to receive the body of the request or response. Enabled this flag for both the `CloudFrontWebDistribution` and `Distribution` constructs. fixes #7085
1 parent fd4be21 commit b793a25

File tree

6 files changed

+23
-0
lines changed

6 files changed

+23
-0
lines changed

packages/@aws-cdk/aws-cloudfront/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,7 @@ new cloudfront.Distribution(this, 'myDist', {
207207
{
208208
functionVersion: myFunc.currentVersion,
209209
eventType: cloudfront.LambdaEdgeEventType.ORIGIN_REQUEST,
210+
includeBody: true, // Optional - defaults to false
210211
},
211212
],
212213
},

packages/@aws-cdk/aws-cloudfront/lib/distribution.ts

+8
Original file line numberDiff line numberDiff line change
@@ -621,6 +621,14 @@ export interface EdgeLambda {
621621

622622
/** The type of event in response to which should the function be invoked. */
623623
readonly eventType: LambdaEdgeEventType;
624+
625+
/**
626+
* Allows a Lambda function to have read access to the body content.
627+
* See https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/lambda-include-body-access.html
628+
*
629+
* @default false
630+
*/
631+
readonly includeBody?: boolean;
624632
}
625633

626634
/**

packages/@aws-cdk/aws-cloudfront/lib/private/cache-behavior.ts

+1
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ export class CacheBehavior {
5151
? this.props.edgeLambdas.map(edgeLambda => ({
5252
lambdaFunctionArn: edgeLambda.functionVersion.edgeArn,
5353
eventType: edgeLambda.eventType.toString(),
54+
includeBody: edgeLambda.includeBody,
5455
}))
5556
: undefined,
5657
};

packages/@aws-cdk/aws-cloudfront/lib/web_distribution.ts

+9
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,14 @@ export interface LambdaFunctionAssociation {
424424
* A version of the lambda to associate
425425
*/
426426
readonly lambdaFunction: lambda.IVersion;
427+
428+
/**
429+
* Allows a Lambda function to have read access to the body content.
430+
* See https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/lambda-include-body-access.html
431+
*
432+
* @default false
433+
*/
434+
readonly includeBody?: boolean;
427435
}
428436

429437
export interface ViewerCertificateOptions {
@@ -897,6 +905,7 @@ export class CloudFrontWebDistribution extends cdk.Resource implements IDistribu
897905
.map(fna => ({
898906
eventType: fna.eventType,
899907
lambdaFunctionArn: fna.lambdaFunction && fna.lambdaFunction.edgeArn,
908+
includeBody: fna.includeBody,
900909
})),
901910
});
902911

packages/@aws-cdk/aws-cloudfront/test/distribution.test.ts

+2
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,7 @@ describe('with Lambda@Edge functions', () => {
468468
{
469469
functionVersion: lambdaFunction.currentVersion,
470470
eventType: LambdaEdgeEventType.ORIGIN_REQUEST,
471+
includeBody: true,
471472
},
472473
],
473474
},
@@ -479,6 +480,7 @@ describe('with Lambda@Edge functions', () => {
479480
LambdaFunctionAssociations: [
480481
{
481482
EventType: 'origin-request',
483+
IncludeBody: true,
482484
LambdaFunctionARN: {
483485
Ref: 'FunctionCurrentVersion4E2B2261477a5ae8059bbaa7813f752292c0f65e',
484486
},

packages/@aws-cdk/aws-cloudfront/test/web_distribution.test.ts

+2
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,7 @@ nodeunitShim({
444444
lambdaFunctionAssociations: [{
445445
eventType: LambdaEdgeEventType.ORIGIN_REQUEST,
446446
lambdaFunction: lambdaFunction.currentVersion,
447+
includeBody: true,
447448
}],
448449
},
449450
],
@@ -457,6 +458,7 @@ nodeunitShim({
457458
'LambdaFunctionAssociations': [
458459
{
459460
'EventType': 'origin-request',
461+
'IncludeBody': true,
460462
'LambdaFunctionARN': {
461463
'Ref': 'LambdaCurrentVersionDF706F6A97fb843e9bd06fcd2bb15eeace80e13e',
462464
},

0 commit comments

Comments
 (0)