Skip to content

Commit fd753f1

Browse files
authored
fix (CloudFrontToMediaStore - use cloudfront function instead of lambda@edge) (#317)
* fix (CloudFrontToApiGateway - use cloudfront function instead of lambda@edge) (#295) * fix (CloudFrontToMediStore - use cloudfront function instead of lambda@edge) (#296)
1 parent 7ed052c commit fd753f1

11 files changed

+142
-857
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ _Parameters_
6565
|cloudFrontLoggingBucket|[`s3.Bucket`](https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-s3.Bucket.html)|Returns an instance of s3.Bucket as the logging bucket for the CloudFront Web Distribution.|
6666
|cloudFrontOriginRequestPolicy|[`cloudfront.OriginRequestPolicy`](https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-cloudfront.OriginRequestPolicy.html)|Returns an instance of cloudfront.OriginRequestPolicy created by the construct for the CloudFront Web Distribution.|
6767
|cloudFrontOriginAccessIdentity?|[`cloudfront.OriginAccessIdentity`](https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-cloudfront.OriginAccessIdentity.html)|Returns an instance of cloudfront.OriginAccessIdentity created by the construct for the CloudFront Web Distribution origin custom headers and the MediaStore Container policy.|
68-
|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.|
68+
|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.|
6969

7070
## Default settings
7171

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

+2-3
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
import * as cloudfront from '@aws-cdk/aws-cloudfront';
1515
import * as mediastore from '@aws-cdk/aws-mediastore';
1616
import * as s3 from '@aws-cdk/aws-s3';
17-
import * as lambda from '@aws-cdk/aws-lambda';
1817
import * as defaults from '@aws-solutions-constructs/core';
1918
import { Construct, Aws} from '@aws-cdk/core';
2019

@@ -55,7 +54,7 @@ export class CloudFrontToMediaStore extends Construct {
5554
public readonly cloudFrontLoggingBucket: s3.Bucket;
5655
public readonly cloudFrontOriginRequestPolicy: cloudfront.OriginRequestPolicy;
5756
public readonly cloudFrontOriginAccessIdentity?: cloudfront.OriginAccessIdentity;
58-
public readonly edgeLambdaFunctionVersion?: lambda.Version;
57+
public readonly cloudFrontFunction?: cloudfront.Function;
5958

6059
/**
6160
* @summary Constructs a new instance of CloudFrontToMediaStore class.
@@ -122,7 +121,7 @@ export class CloudFrontToMediaStore extends Construct {
122121
this.mediaStoreContainer = defaults.MediaStoreContainer(this, mediaStoreProps);
123122
}
124123

125-
[this.cloudFrontWebDistribution, this.cloudFrontLoggingBucket, this.cloudFrontOriginRequestPolicy, this.edgeLambdaFunctionVersion]
124+
[this.cloudFrontWebDistribution, this.cloudFrontLoggingBucket, this.cloudFrontOriginRequestPolicy, this.cloudFrontFunction]
126125
= defaults.CloudFrontDistributionForMediaStore(this, this.mediaStoreContainer, cloudFrontDistributionProps, props.insertHttpSecurityHeaders);
127126
}
128127
}

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

+14-144
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,14 @@ Object {
2929
"OPTIONS",
3030
],
3131
"Compress": true,
32-
"LambdaFunctionAssociations": Array [
32+
"FunctionAssociations": Array [
3333
Object {
34-
"EventType": "origin-response",
35-
"LambdaFunctionARN": Object {
36-
"Ref": "testcloudfrontmediastoreSetHttpSecurityHeadersVersionE87B65C3",
34+
"EventType": "viewer-response",
35+
"FunctionARN": Object {
36+
"Fn::GetAtt": Array [
37+
"testcloudfrontmediastoreSetHttpSecurityHeaders9995A63D",
38+
"FunctionARN",
39+
],
3740
},
3841
},
3942
],
@@ -307,149 +310,16 @@ Object {
307310
"Type": "AWS::MediaStore::Container",
308311
},
309312
"testcloudfrontmediastoreSetHttpSecurityHeaders9995A63D": Object {
310-
"DependsOn": Array [
311-
"testcloudfrontmediastoreSetHttpSecurityHeadersServiceRoleDefaultPolicy73DF1407",
312-
"testcloudfrontmediastoreSetHttpSecurityHeadersServiceRole2F1F5449",
313-
],
314-
"Metadata": Object {
315-
"cfn_nag": Object {
316-
"rules_to_suppress": Array [
317-
Object {
318-
"id": "W58",
319-
"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.",
320-
},
321-
Object {
322-
"id": "W89",
323-
"reason": "This is not a rule for the general case, just for specific use cases/industries",
324-
},
325-
Object {
326-
"id": "W92",
327-
"reason": "Impossible for us to define the correct concurrency for clients",
328-
},
329-
],
330-
},
331-
},
332-
"Properties": Object {
333-
"Code": Object {
334-
"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); };",
335-
},
336-
"Handler": "index.handler",
337-
"Role": Object {
338-
"Fn::GetAtt": Array [
339-
"testcloudfrontmediastoreSetHttpSecurityHeadersServiceRole2F1F5449",
340-
"Arn",
341-
],
342-
},
343-
"Runtime": "nodejs12.x",
344-
"TracingConfig": Object {
345-
"Mode": "Active",
346-
},
347-
},
348-
"Type": "AWS::Lambda::Function",
349-
},
350-
"testcloudfrontmediastoreSetHttpSecurityHeadersServiceRole2F1F5449": Object {
351-
"Properties": Object {
352-
"AssumeRolePolicyDocument": Object {
353-
"Statement": Array [
354-
Object {
355-
"Action": "sts:AssumeRole",
356-
"Effect": "Allow",
357-
"Principal": Object {
358-
"Service": "lambda.amazonaws.com",
359-
},
360-
},
361-
Object {
362-
"Action": "sts:AssumeRole",
363-
"Effect": "Allow",
364-
"Principal": Object {
365-
"Service": "edgelambda.amazonaws.com",
366-
},
367-
},
368-
],
369-
"Version": "2012-10-17",
370-
},
371-
"Policies": Array [
372-
Object {
373-
"PolicyDocument": Object {
374-
"Statement": Array [
375-
Object {
376-
"Action": Array [
377-
"logs:CreateLogGroup",
378-
"logs:CreateLogStream",
379-
"logs:PutLogEvents",
380-
],
381-
"Effect": "Allow",
382-
"Resource": Object {
383-
"Fn::Join": Array [
384-
"",
385-
Array [
386-
"arn:",
387-
Object {
388-
"Ref": "AWS::Partition",
389-
},
390-
":logs:",
391-
Object {
392-
"Ref": "AWS::Region",
393-
},
394-
":",
395-
Object {
396-
"Ref": "AWS::AccountId",
397-
},
398-
":log-group:/aws/lambda/*",
399-
],
400-
],
401-
},
402-
},
403-
],
404-
"Version": "2012-10-17",
405-
},
406-
"PolicyName": "LambdaFunctionServiceRolePolicy",
407-
},
408-
],
409-
},
410-
"Type": "AWS::IAM::Role",
411-
},
412-
"testcloudfrontmediastoreSetHttpSecurityHeadersServiceRoleDefaultPolicy73DF1407": Object {
413-
"Metadata": Object {
414-
"cfn_nag": Object {
415-
"rules_to_suppress": Array [
416-
Object {
417-
"id": "W12",
418-
"reason": "Lambda needs the following minimum required permissions to send trace data to X-Ray and access ENIs in a VPC.",
419-
},
420-
],
421-
},
422-
},
423-
"Properties": Object {
424-
"PolicyDocument": Object {
425-
"Statement": Array [
426-
Object {
427-
"Action": Array [
428-
"xray:PutTraceSegments",
429-
"xray:PutTelemetryRecords",
430-
],
431-
"Effect": "Allow",
432-
"Resource": "*",
433-
},
434-
],
435-
"Version": "2012-10-17",
436-
},
437-
"PolicyName": "testcloudfrontmediastoreSetHttpSecurityHeadersServiceRoleDefaultPolicy73DF1407",
438-
"Roles": Array [
439-
Object {
440-
"Ref": "testcloudfrontmediastoreSetHttpSecurityHeadersServiceRole2F1F5449",
441-
},
442-
],
443-
},
444-
"Type": "AWS::IAM::Policy",
445-
},
446-
"testcloudfrontmediastoreSetHttpSecurityHeadersVersionE87B65C3": Object {
447313
"Properties": Object {
448-
"FunctionName": Object {
449-
"Ref": "testcloudfrontmediastoreSetHttpSecurityHeaders9995A63D",
314+
"AutoPublish": true,
315+
"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; }",
316+
"FunctionConfig": Object {
317+
"Comment": "SetHttpSecurityHeadersc82c7d803c8b3899ab37eeee5cc2bce7beb7673c36",
318+
"Runtime": "cloudfront-js-1.0",
450319
},
320+
"Name": "SetHttpSecurityHeadersc82c7d803c8b3899ab37eeee5cc2bce7beb7673c36",
451321
},
452-
"Type": "AWS::Lambda::Version",
322+
"Type": "AWS::CloudFront::Function",
453323
},
454324
},
455325
}

source/patterns/@aws-solutions-constructs/aws-cloudfront-mediastore/test/cloudfront-mediastore.test.ts

+23-14
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ test('Test the default deployment pattern variables', () => {
4545
expect(cloudFrontToMediaStore.cloudFrontLoggingBucket).not.toEqual(undefined);
4646
expect(cloudFrontToMediaStore.cloudFrontOriginRequestPolicy).not.toEqual(undefined);
4747
expect(cloudFrontToMediaStore.cloudFrontOriginAccessIdentity).not.toEqual(undefined);
48-
expect(cloudFrontToMediaStore.edgeLambdaFunctionVersion).not.toEqual(undefined);
48+
expect(cloudFrontToMediaStore.cloudFrontFunction).not.toEqual(undefined);
4949
});
5050

5151
// --------------------------------------------------------------
@@ -135,7 +135,7 @@ test('Test the deployment without HTTP security headers', () => {
135135
]
136136
}
137137
});
138-
expect(cloudFrontToMediaStore.edgeLambdaFunctionVersion).toEqual(undefined);
138+
expect(cloudFrontToMediaStore.cloudFrontFunction).toEqual(undefined);
139139
});
140140

141141
// --------------------------------------------------------------
@@ -167,11 +167,14 @@ test('Test the deployment with existing MediaStore container', () => {
167167
'OPTIONS'
168168
],
169169
Compress: true,
170-
LambdaFunctionAssociations: [
170+
FunctionAssociations: [
171171
{
172-
EventType: 'origin-response',
173-
LambdaFunctionARN: {
174-
Ref: 'testcloudfrontmediastoreSetHttpSecurityHeadersVersionE87B65C3'
172+
EventType: "viewer-response",
173+
FunctionARN: {
174+
"Fn::GetAtt": [
175+
"testcloudfrontmediastoreSetHttpSecurityHeaders9995A63D",
176+
"FunctionARN"
177+
]
175178
}
176179
}
177180
],
@@ -303,11 +306,14 @@ test('Test the deployment with the user provided MediaStore properties', () => {
303306
'OPTIONS'
304307
],
305308
Compress: true,
306-
LambdaFunctionAssociations: [
309+
FunctionAssociations: [
307310
{
308-
EventType: 'origin-response',
309-
LambdaFunctionARN: {
310-
Ref: 'testcloudfrontmediastoreSetHttpSecurityHeadersVersionE87B65C3'
311+
EventType: "viewer-response",
312+
FunctionARN: {
313+
"Fn::GetAtt": [
314+
"testcloudfrontmediastoreSetHttpSecurityHeaders9995A63D",
315+
"FunctionARN"
316+
]
311317
}
312318
}
313319
],
@@ -446,11 +452,14 @@ test('Test the deployment with the user provided CloudFront properties', () => {
446452
'HEAD'
447453
],
448454
Compress: true,
449-
LambdaFunctionAssociations: [
455+
FunctionAssociations: [
450456
{
451-
EventType: 'origin-response',
452-
LambdaFunctionARN: {
453-
Ref: 'testcloudfrontmediastoreSetHttpSecurityHeadersVersionE87B65C3'
457+
EventType: "viewer-response",
458+
FunctionARN: {
459+
"Fn::GetAtt": [
460+
"testcloudfrontmediastoreSetHttpSecurityHeaders9995A63D",
461+
"FunctionARN"
462+
]
454463
}
455464
}
456465
],

0 commit comments

Comments
 (0)