forked from scalefactory/terraform-cloudfront-auth
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.tf
323 lines (276 loc) · 8.09 KB
/
main.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
#
# Local nvm presence check
#
resource "null_resource" "check_nvm" {
provisioner "local-exec" {
command = <<EOF
if not which nvm; then
echo "ERROR: nvm is not installed"
exit 1
fi
EOF
}
}
#
# Local nodejs dependency install.
#
resource "null_resource" "provision_nodejs" {
depends_on = [null_resource.check_nvm]
provisioner "local-exec" {
command = <<EOF
nvm install -s ${var.nodejs_version} &&\
nvm use ${var.nodejs_version}
EOF
}
}
#
# Lambda Packaging
#
resource "null_resource" "copy_source" {
depends_on = [null_resource.provision_nodejs]
triggers = {
build_resource = null_resource.provision_nodejs.id
always_run = timestamp()
}
provisioner "local-exec" {
command = <<EOF
if [ ! -d "build" ]; then
if [ ! -L "build" ]; then
curl -L https://github.com/mslipets/cloudfront-auth/archive/${var.cloudfront_auth_branch}.zip \
--output cloudfront-auth-${var.cloudfront_auth_branch}.zip
unzip -q cloudfront-auth-${var.cloudfront_auth_branch}.zip -d build/
mkdir build/cloudfront-auth-${var.cloudfront_auth_branch}/distributions
nvm use ${var.nodejs_version}
cp ${data.local_file.build-js.filename} build/cloudfront-auth-${var.cloudfront_auth_branch}/build/build.js&&\
cd build/cloudfront-auth-${var.cloudfront_auth_branch} && npm i minimist shelljs && npm install && cd build && npm install
fi
fi
EOF
}
}
# Builds the Lambda zip artifact
resource "null_resource" "build_lambda" {
depends_on = [null_resource.copy_source]
# Trigger a rebuild on any variable change
triggers = {
copy_source = null_resource.copy_source.id
vendor = var.auth_vendor
cloudfront_distribution = var.cloudfront_distribution
client_id = var.client_id
client_secret = var.client_secret
base_uri = var.base_uri
redirect_uri = var.redirect_uri
session_duration = var.session_duration
authz = var.authz
}
provisioner "local-exec" {
command = <<EOF
nvm use ${var.nodejs_version}&&\
cd build/cloudfront-auth-${var.cloudfront_auth_branch} &&\
node build/build.js --AUTH_VENDOR=${var.auth_vendor} \
--BASE_URL=${var.base_uri} \
--CLOUDFRONT_DISTRIBUTION=${var.cloudfront_distribution} \
--CLIENT_ID=${var.client_id} \
--CLIENT_SECRET=${var.client_secret == "" ? "none" : var.client_secret} \
--REDIRECT_URI=${var.redirect_uri} --HD=${var.hd} \
--SESSION_DURATION=${var.session_duration} --AUTHZ=${var.authz} \
--GITHUB_ORGANIZATION=${var.github_organization}
EOF
}
}
# Copies the artifact to the root directory
resource "null_resource" "copy_lambda_artifact" {
depends_on = [null_resource.build_lambda]
triggers = {
build_resource = null_resource.build_lambda.id
}
provisioner "local-exec" {
command = "cp build/cloudfront-auth-${var.cloudfront_auth_branch}/distributions/${var.cloudfront_distribution}/${var.cloudfront_distribution}.zip ${local.lambda_filename}"
}
}
# workaround to sync file creation
data "null_data_source" "lambda_artifact_sync" {
inputs = {
file = local.lambda_filename
trigger = null_resource.copy_lambda_artifact.id # this is for sync only
}
}
data "local_file" "build-js" {
filename = "${path.module}/build.js"
}
#
# S3
#
resource "aws_s3_bucket" "default" {
bucket = var.bucket_name
acl = "private"
tags = var.tags
region = var.region
}
data "aws_iam_policy_document" "s3_bucket_policy" {
statement {
actions = [
"s3:GetObject",
"s3:ListBucket"
]
resources = [
aws_s3_bucket.default.arn,
"${aws_s3_bucket.default.arn}/*"
]
principals {
type = "AWS"
identifiers = [
aws_cloudfront_origin_access_identity.default.iam_arn,
]
}
}
dynamic "statement" {
for_each = var.bucket_access_roles_arn_list
iterator = arn
content {
actions = [
"s3:GetBucketLocation",
"s3:ListBucket",
"s3:GetObject",
"s3:PutObject"
]
resources = [
aws_s3_bucket.default.arn,
"${aws_s3_bucket.default.arn}/*"
]
principals {
type = "AWS"
identifiers = [arn.value]
}
}
}
}
resource "aws_s3_bucket_policy" "bucket_policy" {
bucket = aws_s3_bucket.default.id
policy = data.aws_iam_policy_document.s3_bucket_policy.json
}
#
# Cloudfront
#
resource "aws_cloudfront_origin_access_identity" "default" {
comment = var.bucket_name
}
resource "aws_cloudfront_distribution" "default" {
origin {
domain_name = aws_s3_bucket.default.bucket_regional_domain_name
origin_id = local.s3_origin_id
s3_origin_config {
origin_access_identity = aws_cloudfront_origin_access_identity.default.cloudfront_access_identity_path
}
}
aliases = concat([var.cloudfront_distribution], [var.bucket_name], var.cloudfront_aliases)
comment = "Managed by Terraform"
default_root_object = var.cloudfront_default_root_object
enabled = true
http_version = "http2"
is_ipv6_enabled = true
price_class = var.cloudfront_price_class
tags = var.tags
default_cache_behavior {
target_origin_id = local.s3_origin_id
// Read only
allowed_methods = [
"GET",
"HEAD",
]
cached_methods = [
"GET",
"HEAD",
]
forwarded_values {
query_string = true
headers = [
"Access-Control-Request-Headers",
"Access-Control-Request-Method",
"Origin",
]
cookies {
forward = "none"
}
}
lambda_function_association {
event_type = "viewer-request"
lambda_arn = aws_lambda_function.default.qualified_arn
}
viewer_protocol_policy = "redirect-to-https"
}
restrictions {
geo_restriction {
restriction_type = (var.geo_restriction_whitelisted_locations == "") ? "none" : "whitelist"
locations = (var.geo_restriction_whitelisted_locations == "") ? [] : [var.geo_restriction_whitelisted_locations]
}
}
viewer_certificate {
acm_certificate_arn = var.cloudfront_acm_certificate_arn
ssl_support_method = "sni-only"
cloudfront_default_certificate = false
}
}
#
# Lambda
#
data "aws_iam_policy_document" "lambda_log_access" {
// Allow lambda access to logging
statement {
actions = [
"logs:CreateLogGroup",
"logs:CreateLogStream",
"logs:PutLogEvents",
]
resources = [
"arn:aws:logs:*:*:*",
]
effect = "Allow"
}
}
# This function is created in us-east-1 as required by CloudFront.
resource "aws_lambda_function" "default" {
depends_on = [null_resource.check_nvm, null_resource.copy_lambda_artifact]
provider = aws.us-east-1
description = "Managed by Terraform"
runtime = "nodejs10.x"
role = aws_iam_role.lambda_role.arn
filename = local.lambda_filename
function_name = "cloudfront_auth"
handler = "index.handler"
publish = true
timeout = 5
source_code_hash = filebase64sha256(data.null_data_source.lambda_artifact_sync.outputs["file"])
tags = var.tags
}
data "aws_iam_policy_document" "lambda_assume_role" {
// Trust relationships taken from blueprint
// Allow lambda to assume this role.
statement {
actions = [
"sts:AssumeRole",
]
principals {
type = "Service"
identifiers = [
"edgelambda.amazonaws.com",
"lambda.amazonaws.com",
]
}
effect = "Allow"
}
}
resource "aws_iam_role" "lambda_role" {
name = "lambda_role"
assume_role_policy = data.aws_iam_policy_document.lambda_assume_role.json
}
# Attach the logging access document to the above role.
resource "aws_iam_role_policy_attachment" "lambda_log_access" {
role = aws_iam_role.lambda_role.name
policy_arn = aws_iam_policy.lambda_log_access.arn
}
# Create an IAM policy that will be attached to the role
resource "aws_iam_policy" "lambda_log_access" {
name = "cloudfront_auth_lambda_log_access"
policy = data.aws_iam_policy_document.lambda_log_access.json
}