Skip to content

Commit 28ea163

Browse files
authored
fix (CloudFrontToS3 - use cloudfront function instead of lambda@edge) (#272)
* fix (CloudFrontToS3 - use cloudfront function instead of lambda@edge) (#233) * fix (generate stable functionId for the cloudfront function)
1 parent 1f2078d commit 28ea163

12 files changed

+610
-636
lines changed

source/patterns/@aws-solutions-constructs/aws-cloudfront-s3/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ _Parameters_
5959
| **Name** | **Type** | **Description** |
6060
|:-------------|:----------------|-----------------|
6161
|cloudFrontWebDistribution|[`cloudfront.CloudFrontWebDistribution`](https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-cloudfront.CloudFrontWebDistribution.html)|Returns an instance of cloudfront.CloudFrontWebDistribution created by the construct|
62-
|edgeLambdaFunctionVersion|[`lambda.Version`](https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-lambda.Version.html)|Returns an instance of the edge Lambda function version created by the pattern.|
62+
|cloudFrontFunction?|[`cloudfront.Function`](https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-cloudfront.Function.html)|Returns an instance of the Cloudfront function created by the pattern.|
6363
|cloudFrontLoggingBucket|[`s3.Bucket`](https://docs.aws.amazon.com/cdk/api/latest/docs/aws-s3-readme.html)|Returns an instance of the logging bucket for CloudFront WebDistribution.|
6464
|s3BucketInterface|[`s3.IBucket`](https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-s3.IBucket.html)|Returns an instance of s3.IBucket created by the construct|
6565
|s3Bucket?|[`s3.Bucket`](https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-s3.Bucket.html)|Returns an instance of s3.Bucket created by the construct. IMPORTANT: If existingBucketInterface was provided in Pattern Construct Props, this property will be `undefined`|

source/patterns/@aws-solutions-constructs/aws-cloudfront-s3/lib/index.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
import * as cloudfront from '@aws-cdk/aws-cloudfront';
1515
import * as s3 from '@aws-cdk/aws-s3';
16-
import * as lambda from '@aws-cdk/aws-lambda';
1716
import { Construct } from '@aws-cdk/core';
1817
import * as defaults from '@aws-solutions-constructs/core';
1918

@@ -50,7 +49,7 @@ export interface CloudFrontToS3Props {
5049

5150
export class CloudFrontToS3 extends Construct {
5251
public readonly cloudFrontWebDistribution: cloudfront.Distribution;
53-
public readonly edgeLambdaFunctionVersion?: lambda.Version;
52+
public readonly cloudFrontFunction?: cloudfront.Function;
5453
public readonly cloudFrontLoggingBucket?: s3.Bucket;
5554
public readonly s3BucketInterface: s3.IBucket;
5655
public readonly s3Bucket?: s3.Bucket;
@@ -77,7 +76,7 @@ export class CloudFrontToS3 extends Construct {
7776
this.s3BucketInterface = props.existingBucketInterface;
7877
}
7978

80-
[this.cloudFrontWebDistribution, this.edgeLambdaFunctionVersion, this.cloudFrontLoggingBucket] =
79+
[this.cloudFrontWebDistribution, this.cloudFrontFunction, this.cloudFrontLoggingBucket] =
8180
defaults.CloudFrontDistributionForS3(this, this.s3BucketInterface,
8281
props.cloudFrontDistributionProps, props.insertHttpSecurityHeaders);
8382
}

source/patterns/@aws-solutions-constructs/aws-cloudfront-s3/test/__snapshots__/test.cloudfront-s3.test.js.snap

+14-144
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,14 @@ Object {
1919
"DefaultCacheBehavior": Object {
2020
"CachePolicyId": "658327ea-f89d-4fab-a63d-7e88639e58f6",
2121
"Compress": true,
22-
"LambdaFunctionAssociations": Array [
22+
"FunctionAssociations": Array [
2323
Object {
24-
"EventType": "origin-response",
25-
"LambdaFunctionARN": Object {
26-
"Ref": "testcloudfronts3SetHttpSecurityHeadersVersionF1C744BB",
24+
"EventType": "viewer-response",
25+
"FunctionARN": Object {
26+
"Fn::GetAtt": Array [
27+
"testcloudfronts3SetHttpSecurityHeaders6C5A1E69",
28+
"FunctionARN",
29+
],
2730
},
2831
},
2932
],
@@ -368,149 +371,16 @@ Object {
368371
"Type": "AWS::S3::BucketPolicy",
369372
},
370373
"testcloudfronts3SetHttpSecurityHeaders6C5A1E69": Object {
371-
"DependsOn": Array [
372-
"testcloudfronts3SetHttpSecurityHeadersServiceRoleDefaultPolicy7A7EF47A",
373-
"testcloudfronts3SetHttpSecurityHeadersServiceRole74D1E252",
374-
],
375-
"Metadata": Object {
376-
"cfn_nag": Object {
377-
"rules_to_suppress": Array [
378-
Object {
379-
"id": "W58",
380-
"reason": "Lambda functions has the required permission to write CloudWatch Logs. It uses custom policy instead of arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole with tighter permissions.",
381-
},
382-
Object {
383-
"id": "W89",
384-
"reason": "This is not a rule for the general case, just for specific use cases/industries",
385-
},
386-
Object {
387-
"id": "W92",
388-
"reason": "Impossible for us to define the correct concurrency for clients",
389-
},
390-
],
391-
},
392-
},
393-
"Properties": Object {
394-
"Code": Object {
395-
"ZipFile": "exports.handler = (event, context, callback) => { const response = event.Records[0].cf.response; const headers = response.headers; headers['x-xss-protection'] = [ { key: 'X-XSS-Protection', value: '1; mode=block' } ]; headers['x-frame-options'] = [ { key: 'X-Frame-Options', value: 'DENY' } ]; headers['x-content-type-options'] = [ { key: 'X-Content-Type-Options', value: 'nosniff' } ]; headers['strict-transport-security'] = [ { key: 'Strict-Transport-Security', value: 'max-age=63072000; includeSubdomains; preload' } ]; headers['referrer-policy'] = [ { key: 'Referrer-Policy', value: 'same-origin' } ]; headers['content-security-policy'] = [ { key: 'Content-Security-Policy', value: \\"default-src 'none'; base-uri 'self'; img-src 'self'; script-src 'self'; style-src 'self' https:; object-src 'none'; frame-ancestors 'none'; font-src 'self' https:; form-action 'self'; manifest-src 'self'; connect-src 'self'\\" } ]; callback(null, response); };",
396-
},
397-
"Handler": "index.handler",
398-
"Role": Object {
399-
"Fn::GetAtt": Array [
400-
"testcloudfronts3SetHttpSecurityHeadersServiceRole74D1E252",
401-
"Arn",
402-
],
403-
},
404-
"Runtime": "nodejs12.x",
405-
"TracingConfig": Object {
406-
"Mode": "Active",
407-
},
408-
},
409-
"Type": "AWS::Lambda::Function",
410-
},
411-
"testcloudfronts3SetHttpSecurityHeadersServiceRole74D1E252": Object {
412-
"Properties": Object {
413-
"AssumeRolePolicyDocument": Object {
414-
"Statement": Array [
415-
Object {
416-
"Action": "sts:AssumeRole",
417-
"Effect": "Allow",
418-
"Principal": Object {
419-
"Service": "lambda.amazonaws.com",
420-
},
421-
},
422-
Object {
423-
"Action": "sts:AssumeRole",
424-
"Effect": "Allow",
425-
"Principal": Object {
426-
"Service": "edgelambda.amazonaws.com",
427-
},
428-
},
429-
],
430-
"Version": "2012-10-17",
431-
},
432-
"Policies": Array [
433-
Object {
434-
"PolicyDocument": Object {
435-
"Statement": Array [
436-
Object {
437-
"Action": Array [
438-
"logs:CreateLogGroup",
439-
"logs:CreateLogStream",
440-
"logs:PutLogEvents",
441-
],
442-
"Effect": "Allow",
443-
"Resource": Object {
444-
"Fn::Join": Array [
445-
"",
446-
Array [
447-
"arn:",
448-
Object {
449-
"Ref": "AWS::Partition",
450-
},
451-
":logs:",
452-
Object {
453-
"Ref": "AWS::Region",
454-
},
455-
":",
456-
Object {
457-
"Ref": "AWS::AccountId",
458-
},
459-
":log-group:/aws/lambda/*",
460-
],
461-
],
462-
},
463-
},
464-
],
465-
"Version": "2012-10-17",
466-
},
467-
"PolicyName": "LambdaFunctionServiceRolePolicy",
468-
},
469-
],
470-
},
471-
"Type": "AWS::IAM::Role",
472-
},
473-
"testcloudfronts3SetHttpSecurityHeadersServiceRoleDefaultPolicy7A7EF47A": Object {
474-
"Metadata": Object {
475-
"cfn_nag": Object {
476-
"rules_to_suppress": Array [
477-
Object {
478-
"id": "W12",
479-
"reason": "Lambda needs the following minimum required permissions to send trace data to X-Ray and access ENIs in a VPC.",
480-
},
481-
],
482-
},
483-
},
484-
"Properties": Object {
485-
"PolicyDocument": Object {
486-
"Statement": Array [
487-
Object {
488-
"Action": Array [
489-
"xray:PutTraceSegments",
490-
"xray:PutTelemetryRecords",
491-
],
492-
"Effect": "Allow",
493-
"Resource": "*",
494-
},
495-
],
496-
"Version": "2012-10-17",
497-
},
498-
"PolicyName": "testcloudfronts3SetHttpSecurityHeadersServiceRoleDefaultPolicy7A7EF47A",
499-
"Roles": Array [
500-
Object {
501-
"Ref": "testcloudfronts3SetHttpSecurityHeadersServiceRole74D1E252",
502-
},
503-
],
504-
},
505-
"Type": "AWS::IAM::Policy",
506-
},
507-
"testcloudfronts3SetHttpSecurityHeadersVersionF1C744BB": Object {
508374
"Properties": Object {
509-
"FunctionName": Object {
510-
"Ref": "testcloudfronts3SetHttpSecurityHeaders6C5A1E69",
375+
"AutoPublish": true,
376+
"FunctionCode": "function handler(event) { var response = event.response; var headers = response.headers; headers['strict-transport-security'] = { value: 'max-age=63072000; includeSubdomains; preload'}; headers['content-security-policy'] = { value: \\"default-src 'none'; img-src 'self'; script-src 'self'; style-src 'self'; object-src 'none'\\"}; headers['x-content-type-options'] = { value: 'nosniff'}; headers['x-frame-options'] = {value: 'DENY'}; headers['x-xss-protection'] = {value: '1; mode=block'}; return response; }",
377+
"FunctionConfig": Object {
378+
"Comment": "SetHttpSecurityHeadersc829e98c76feafa0d7fd7eb5237e54317e9ff52687",
379+
"Runtime": "cloudfront-js-1.0",
511380
},
381+
"Name": "SetHttpSecurityHeadersc829e98c76feafa0d7fd7eb5237e54317e9ff52687",
512382
},
513-
"Type": "AWS::Lambda::Version",
383+
"Type": "AWS::CloudFront::Function",
514384
},
515385
},
516386
}

0 commit comments

Comments
 (0)