forked from cloudposse/terraform-aws-cloudfront-s3-cdn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlambda-at-edge.tf
132 lines (113 loc) · 3.77 KB
/
lambda-at-edge.tf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
provider "aws" {
region = "us-east-1"
alias = "us-east-1"
}
data "aws_iam_policy_document" "s3_policy" {
statement {
sid = "AllowS3GetObjectFoo"
effect = "Allow"
actions = [
"s3:GetObject",
]
resources = [
"arn:aws:s3:::example-bucket-foo/*",
]
}
statement {
sid = "AllowS3PutObjectBar"
effect = "Allow"
actions = [
"s3:PutObject",
]
resources = [
"arn:aws:s3:::example-bucket-bar/*",
]
}
}
module "lambda_at_edge" {
source = "../../modules/lambda@edge"
enabled = local.enabled && var.lambda_at_edge_enabled
functions = {
# Just for the sake of a viewer-request example, inject a useless header into the request from the viewer to CF
viewer_request = {
source = [{
content = <<-EOT
'use strict';
exports.handler = (event, context, callback) => {
const { request } = event.Records[0].cf;
request.headers['useless-header'] = [
{
key: 'Useless-Header',
value: 'This header is absolutely useless.'
}
];
return callback(null, request);
};
EOT
filename = "index.js"
}]
runtime = "nodejs16.x"
handler = "index.handler"
memory_size = 128
timeout = 3
event_type = "viewer-request"
include_body = false
},
# Add custom header to the response
viewer_response = {
source_dir = "lib"
runtime = "nodejs16.x"
handler = "index.handler"
memory_size = 128
timeout = 3
event_type = "viewer-response"
include_body = false
},
origin_request = {
source_zip = "origin-request.zip"
runtime = "nodejs16.x"
handler = "index.handler"
memory_size = 128
timeout = 3
event_type = "origin-request"
include_body = false
additional_policy = data.aws_iam_policy_document.s3_policy.json
},
# Add security headers to the request from CF to the origin
origin_response = {
source = [{
# https://aws.amazon.com/blogs/networking-and-content-delivery/adding-http-security-headers-using-lambdaedge-and-amazon-cloudfront/
content = <<-EOT
'use strict';
exports.handler = (event, context, callback) => {
//Get contents of response
const response = event.Records[0].cf.response;
const headers = response.headers;
//Set new headers
headers['strict-transport-security'] = [{key: 'Strict-Transport-Security', value: 'max-age=63072000; includeSubdomains; preload'}];
headers['content-security-policy'] = [{key: 'Content-Security-Policy', value: "default-src 'none'; img-src 'self'; script-src 'self'; style-src 'self'; object-src 'none'"}];
headers['x-content-type-options'] = [{key: 'X-Content-Type-Options', value: 'nosniff'}];
headers['x-frame-options'] = [{key: 'X-Frame-Options', value: 'DENY'}];
headers['x-xss-protection'] = [{key: 'X-XSS-Protection', value: '1; mode=block'}];
headers['referrer-policy'] = [{key: 'Referrer-Policy', value: 'same-origin'}];
//Return modified response
callback(null, response);
};
EOT
filename = "index.js"
}]
runtime = "nodejs16.x"
handler = "index.handler"
memory_size = 128
timeout = 3
event_type = "origin-response"
include_body = false
}
}
# A destruction delay is always enabled due to automated tests (see variable description for more information).
destruction_delay = "20m"
providers = {
aws = aws.us-east-1
}
context = module.this.context
}