Skip to content

Commit 20725fe

Browse files
committed
feat: Add origin mutation
1 parent c552d98 commit 20725fe

File tree

5 files changed

+418
-0
lines changed

5 files changed

+418
-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: 315 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,315 @@
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": Object {
192+
"Fn::Join": Array [
193+
"",
194+
Array [
195+
"{\\"service\\":\\"SSM\\",\\"action\\":\\"getParameter\\",\\"parameters\\":{\\"Name\\":\\"",
196+
Object {
197+
"Ref": "originmutationStringParameterFEABB344",
198+
},
199+
"\\"},\\"region\\":\\"us-east-1\\",\\"physicalResourceId\\":{\\"id\\":\\"1626942620734\\"}}",
200+
],
201+
],
202+
},
203+
"InstallLatestAwsSdk": true,
204+
"ServiceToken": Object {
205+
"Fn::GetAtt": Array [
206+
"AWS679f53fac002430cb0da5b7982bd22872D164C4C",
207+
"Arn",
208+
],
209+
},
210+
"Update": Object {
211+
"Fn::Join": Array [
212+
"",
213+
Array [
214+
"{\\"service\\":\\"SSM\\",\\"action\\":\\"getParameter\\",\\"parameters\\":{\\"Name\\":\\"",
215+
Object {
216+
"Ref": "originmutationStringParameterFEABB344",
217+
},
218+
"\\"},\\"region\\":\\"us-east-1\\",\\"physicalResourceId\\":{\\"id\\":\\"1626942620734\\"}}",
219+
],
220+
],
221+
},
222+
},
223+
"Type": "Custom::AWS",
224+
"UpdateReplacePolicy": "Delete",
225+
},
226+
"originmutationProviderCustomResourcePolicyE087AD13": Object {
227+
"Properties": Object {
228+
"PolicyDocument": Object {
229+
"Statement": Array [
230+
Object {
231+
"Action": "ssm:GetParameter",
232+
"Effect": "Allow",
233+
"Resource": Object {
234+
"Fn::Join": Array [
235+
"",
236+
Array [
237+
"arn:",
238+
Object {
239+
"Ref": "AWS::Partition",
240+
},
241+
":ssm:us-east-1:",
242+
Object {
243+
"Ref": "AWS::AccountId",
244+
},
245+
":parameter",
246+
Object {
247+
"Ref": "originmutationStringParameterFEABB344",
248+
},
249+
],
250+
],
251+
},
252+
},
253+
],
254+
"Version": "2012-10-17",
255+
},
256+
"PolicyName": "originmutationProviderCustomResourcePolicyE087AD13",
257+
"Roles": Array [
258+
Object {
259+
"Ref": "AWS679f53fac002430cb0da5b7982bd2287ServiceRoleC1EA0FF2",
260+
},
261+
],
262+
},
263+
"Type": "AWS::IAM::Policy",
264+
},
265+
"originmutationRoleB8F97561": Object {
266+
"Properties": Object {
267+
"AssumeRolePolicyDocument": Object {
268+
"Statement": Array [
269+
Object {
270+
"Action": "sts:AssumeRole",
271+
"Effect": "Allow",
272+
"Principal": Object {
273+
"Service": Array [
274+
"edgelambda.amazonaws.com",
275+
"lambda.amazonaws.com",
276+
],
277+
},
278+
},
279+
],
280+
"Version": "2012-10-17",
281+
},
282+
"ManagedPolicyArns": Array [
283+
Object {
284+
"Fn::Join": Array [
285+
"",
286+
Array [
287+
"arn:",
288+
Object {
289+
"Ref": "AWS::Partition",
290+
},
291+
":iam::aws:policy/service-role/AWSLambdaBasicExecutionRole",
292+
],
293+
],
294+
},
295+
],
296+
},
297+
"Type": "AWS::IAM::Role",
298+
},
299+
"originmutationStringParameterFEABB344": Object {
300+
"Properties": Object {
301+
"Description": "Parameter stored for cross region Lambda@Edge",
302+
"Name": "/cloudcomponents/edge-lambda/Dummy/origin-mutation/c83d99d8def072b82738c0d40222a792527c17f94d",
303+
"Type": "String",
304+
"Value": Object {
305+
"Fn::GetAtt": Array [
306+
"originmutationFunction689DF756",
307+
"Arn",
308+
],
309+
},
310+
},
311+
"Type": "AWS::SSM::Parameter",
312+
},
313+
},
314+
}
315+
`;
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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+
});
27+
});

0 commit comments

Comments
 (0)