Skip to content

Commit c548de1

Browse files
authored
Merge pull request #109 from cloudcomponents/fix_105
feat: Add origin mutation
2 parents c552d98 + 07cc2a1 commit c548de1

File tree

5 files changed

+406
-0
lines changed

5 files changed

+406
-0
lines changed

packages/cdk-lambda-at-edge-pattern/README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,30 @@ new cloudfront.CloudFrontWebDistribution(this, 'MyDistribution', {
100100
});
101101
```
102102

103+
### HttpHeaders
104+
```typescript
105+
const httpHeaders = new HttpHeaders(this, 'HttpHeaders', {
106+
httpHeaders: {
107+
'Content-Security-Policy':
108+
"default-src 'none'; img-src 'self'; script-src 'self'; style-src 'self' 'unsafe-inline'; object-src 'none'; connect-src 'self'",
109+
'Strict-Transport-Security':
110+
'max-age=31536000; includeSubdomains; preload',
111+
'Referrer-Policy': 'same-origin',
112+
'X-XSS-Protection': '1; mode=block',
113+
'X-Frame-Options': 'DENY',
114+
'X-Content-Type-Options': 'nosniff',
115+
'Cache-Control': 'no-cache',
116+
},
117+
});
118+
```
119+
120+
### OriginMutation
121+
https://chrisschuld.com/2020/05/gatsby-hosting-on-cloudfront/
122+
123+
```typescript
124+
const originMutation = new OriginMutation(stack, 'OriginMutation');
125+
```
126+
103127
## API Reference
104128

105129
See [API.md](https://github.com/cloudcomponents/cdk-constructs/tree/master/packages/cdk-lambda-at-edge-pattern/API.md).
Lines changed: 293 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,293 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`default setup: us-east-1 1`] = `
4+
Object {
5+
"Parameters": Any<Object>,
6+
"Resources": Object {
7+
"AWS679f53fac002430cb0da5b7982bd22872D164C4C": Object {
8+
"DependsOn": Array [
9+
"AWS679f53fac002430cb0da5b7982bd2287ServiceRoleC1EA0FF2",
10+
],
11+
"Properties": Object {
12+
"Code": Any<Object>,
13+
"Handler": "index.handler",
14+
"Role": Object {
15+
"Fn::GetAtt": Array [
16+
"AWS679f53fac002430cb0da5b7982bd2287ServiceRoleC1EA0FF2",
17+
"Arn",
18+
],
19+
},
20+
"Runtime": "nodejs12.x",
21+
"Timeout": 120,
22+
},
23+
"Type": "AWS::Lambda::Function",
24+
},
25+
"AWS679f53fac002430cb0da5b7982bd2287ServiceRoleC1EA0FF2": Object {
26+
"Properties": Object {
27+
"AssumeRolePolicyDocument": Object {
28+
"Statement": Array [
29+
Object {
30+
"Action": "sts:AssumeRole",
31+
"Effect": "Allow",
32+
"Principal": Object {
33+
"Service": "lambda.amazonaws.com",
34+
},
35+
},
36+
],
37+
"Version": "2012-10-17",
38+
},
39+
"ManagedPolicyArns": Array [
40+
Object {
41+
"Fn::Join": Array [
42+
"",
43+
Array [
44+
"arn:",
45+
Object {
46+
"Ref": "AWS::Partition",
47+
},
48+
":iam::aws:policy/service-role/AWSLambdaBasicExecutionRole",
49+
],
50+
],
51+
},
52+
],
53+
},
54+
"Type": "AWS::IAM::Role",
55+
},
56+
"CustomWithConfigurationcloudcomponentscdklambdaatedgepatternwithconfigurationE415FB9B": Object {
57+
"DependsOn": Array [
58+
"CustomWithConfigurationcloudcomponentscdklambdaatedgepatternwithconfigurationServiceRoleDefaultPolicyA42C156A",
59+
"CustomWithConfigurationcloudcomponentscdklambdaatedgepatternwithconfigurationServiceRoleF04EA3EE",
60+
],
61+
"Properties": Object {
62+
"Code": Any<Object>,
63+
"Handler": "index.handler",
64+
"Role": Object {
65+
"Fn::GetAtt": Array [
66+
"CustomWithConfigurationcloudcomponentscdklambdaatedgepatternwithconfigurationServiceRoleF04EA3EE",
67+
"Arn",
68+
],
69+
},
70+
"Runtime": "nodejs12.x",
71+
"Timeout": 300,
72+
},
73+
"Type": "AWS::Lambda::Function",
74+
},
75+
"CustomWithConfigurationcloudcomponentscdklambdaatedgepatternwithconfigurationServiceRoleDefaultPolicyA42C156A": Object {
76+
"Properties": Object {
77+
"PolicyDocument": Object {
78+
"Statement": Array [
79+
Object {
80+
"Action": Array [
81+
"lambda:GetFunction",
82+
"lambda:UpdateFunctionCode",
83+
],
84+
"Effect": "Allow",
85+
"Resource": Object {
86+
"Fn::GetAtt": Array [
87+
"originmutationProviderA4786C4E",
88+
"Parameter.Value",
89+
],
90+
},
91+
},
92+
],
93+
"Version": "2012-10-17",
94+
},
95+
"PolicyName": "CustomWithConfigurationcloudcomponentscdklambdaatedgepatternwithconfigurationServiceRoleDefaultPolicyA42C156A",
96+
"Roles": Array [
97+
Object {
98+
"Ref": "CustomWithConfigurationcloudcomponentscdklambdaatedgepatternwithconfigurationServiceRoleF04EA3EE",
99+
},
100+
],
101+
},
102+
"Type": "AWS::IAM::Policy",
103+
},
104+
"CustomWithConfigurationcloudcomponentscdklambdaatedgepatternwithconfigurationServiceRoleF04EA3EE": Object {
105+
"Properties": Object {
106+
"AssumeRolePolicyDocument": Object {
107+
"Statement": Array [
108+
Object {
109+
"Action": "sts:AssumeRole",
110+
"Effect": "Allow",
111+
"Principal": Object {
112+
"Service": "lambda.amazonaws.com",
113+
},
114+
},
115+
],
116+
"Version": "2012-10-17",
117+
},
118+
"ManagedPolicyArns": Array [
119+
Object {
120+
"Fn::Join": Array [
121+
"",
122+
Array [
123+
"arn:",
124+
Object {
125+
"Ref": "AWS::Partition",
126+
},
127+
":iam::aws:policy/service-role/AWSLambdaBasicExecutionRole",
128+
],
129+
],
130+
},
131+
],
132+
},
133+
"Type": "AWS::IAM::Role",
134+
},
135+
"OriginMutationWithConfiguration4D5BA8BC": Object {
136+
"DeletionPolicy": "Delete",
137+
"Properties": Object {
138+
"Configuration": "{
139+
\\"logLevel\\": \\"warn\\"
140+
}",
141+
"FunctionName": Object {
142+
"Fn::Select": Array [
143+
6,
144+
Object {
145+
"Fn::Split": Array [
146+
":",
147+
Object {
148+
"Fn::GetAtt": Array [
149+
"originmutationProviderA4786C4E",
150+
"Parameter.Value",
151+
],
152+
},
153+
],
154+
},
155+
],
156+
},
157+
"Region": "us-east-1",
158+
"ServiceToken": Object {
159+
"Fn::GetAtt": Array [
160+
"CustomWithConfigurationcloudcomponentscdklambdaatedgepatternwithconfigurationE415FB9B",
161+
"Arn",
162+
],
163+
},
164+
},
165+
"Type": "Custom::WithConfiguration",
166+
"UpdateReplacePolicy": "Delete",
167+
},
168+
"originmutationFunction689DF756": Object {
169+
"DependsOn": Array [
170+
"originmutationRoleB8F97561",
171+
],
172+
"Properties": Object {
173+
"Code": Any<Object>,
174+
"Handler": "index.handler",
175+
"Role": Object {
176+
"Fn::GetAtt": Array [
177+
"originmutationRoleB8F97561",
178+
"Arn",
179+
],
180+
},
181+
"Runtime": "nodejs12.x",
182+
},
183+
"Type": "AWS::Lambda::Function",
184+
},
185+
"originmutationProviderA4786C4E": Object {
186+
"DeletionPolicy": "Delete",
187+
"DependsOn": Array [
188+
"originmutationProviderCustomResourcePolicyE087AD13",
189+
],
190+
"Properties": Object {
191+
"Create": Any<Object>,
192+
"InstallLatestAwsSdk": true,
193+
"ServiceToken": Object {
194+
"Fn::GetAtt": Array [
195+
"AWS679f53fac002430cb0da5b7982bd22872D164C4C",
196+
"Arn",
197+
],
198+
},
199+
"Update": Any<Object>,
200+
},
201+
"Type": "Custom::AWS",
202+
"UpdateReplacePolicy": "Delete",
203+
},
204+
"originmutationProviderCustomResourcePolicyE087AD13": Object {
205+
"Properties": Object {
206+
"PolicyDocument": Object {
207+
"Statement": Array [
208+
Object {
209+
"Action": "ssm:GetParameter",
210+
"Effect": "Allow",
211+
"Resource": Object {
212+
"Fn::Join": Array [
213+
"",
214+
Array [
215+
"arn:",
216+
Object {
217+
"Ref": "AWS::Partition",
218+
},
219+
":ssm:us-east-1:",
220+
Object {
221+
"Ref": "AWS::AccountId",
222+
},
223+
":parameter",
224+
Object {
225+
"Ref": "originmutationStringParameterFEABB344",
226+
},
227+
],
228+
],
229+
},
230+
},
231+
],
232+
"Version": "2012-10-17",
233+
},
234+
"PolicyName": "originmutationProviderCustomResourcePolicyE087AD13",
235+
"Roles": Array [
236+
Object {
237+
"Ref": "AWS679f53fac002430cb0da5b7982bd2287ServiceRoleC1EA0FF2",
238+
},
239+
],
240+
},
241+
"Type": "AWS::IAM::Policy",
242+
},
243+
"originmutationRoleB8F97561": Object {
244+
"Properties": Object {
245+
"AssumeRolePolicyDocument": Object {
246+
"Statement": Array [
247+
Object {
248+
"Action": "sts:AssumeRole",
249+
"Effect": "Allow",
250+
"Principal": Object {
251+
"Service": Array [
252+
"edgelambda.amazonaws.com",
253+
"lambda.amazonaws.com",
254+
],
255+
},
256+
},
257+
],
258+
"Version": "2012-10-17",
259+
},
260+
"ManagedPolicyArns": Array [
261+
Object {
262+
"Fn::Join": Array [
263+
"",
264+
Array [
265+
"arn:",
266+
Object {
267+
"Ref": "AWS::Partition",
268+
},
269+
":iam::aws:policy/service-role/AWSLambdaBasicExecutionRole",
270+
],
271+
],
272+
},
273+
],
274+
},
275+
"Type": "AWS::IAM::Role",
276+
},
277+
"originmutationStringParameterFEABB344": Object {
278+
"Properties": Object {
279+
"Description": "Parameter stored for cross region Lambda@Edge",
280+
"Name": "/cloudcomponents/edge-lambda/Dummy/origin-mutation/c83d99d8def072b82738c0d40222a792527c17f94d",
281+
"Type": "String",
282+
"Value": Object {
283+
"Fn::GetAtt": Array [
284+
"originmutationFunction689DF756",
285+
"Arn",
286+
],
287+
},
288+
},
289+
"Type": "AWS::SSM::Parameter",
290+
},
291+
},
292+
}
293+
`;
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { App, Stack } from '@aws-cdk/core';
2+
import 'jest-cdk-snapshot';
3+
4+
import { OriginMutation } from '../origin-mutation';
5+
6+
test('default setup: us-east-1', () => {
7+
// GIVEN
8+
const app = new App();
9+
10+
const stack = new Stack(app, 'Dummy', {
11+
env: {
12+
region: 'us-east-1',
13+
},
14+
});
15+
16+
// WHEN
17+
new OriginMutation(stack, 'OriginMutation');
18+
19+
// THEN
20+
const supportStack = app.node.tryFindChild(`lambda-at-edge-support-stack`);
21+
22+
expect(supportStack).toBeUndefined();
23+
24+
expect(stack).toMatchCdkSnapshot({
25+
ignoreAssets: true,
26+
propertyMatchers: {
27+
Resources: {
28+
originmutationProviderA4786C4E: {
29+
Properties: {
30+
Create: expect.any(Object),
31+
Update: expect.any(Object),
32+
},
33+
},
34+
},
35+
},
36+
});
37+
});
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import type { CloudFrontRequestHandler } from 'aws-lambda';
2+
import { getConfig, Config } from '../shared';
3+
4+
type OriginMutationConfig = Config<unknown>;
5+
6+
let CONFIG: OriginMutationConfig;
7+
8+
export const handler: CloudFrontRequestHandler = async (event) => {
9+
if (!CONFIG) {
10+
CONFIG = getConfig();
11+
}
12+
13+
CONFIG.logger.debug(event);
14+
15+
const request = event.Records[0].cf.request;
16+
const uri = request.uri;
17+
18+
if (uri.endsWith('/')) {
19+
request.uri += 'index.html';
20+
} else if (!uri.includes('.')) {
21+
request.uri += '/index.html';
22+
}
23+
24+
CONFIG.logger.debug('Returning request:\n', request);
25+
26+
return request;
27+
};

0 commit comments

Comments
 (0)