-
Notifications
You must be signed in to change notification settings - Fork 23
/
main.tf
289 lines (243 loc) · 6.91 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
#
# Lambda Packaging
#
resource "null_resource" "copy_source" {
provisioner "local-exec" {
command = <<EOF
if [ ! -d "build" ]; then
if [ ! -L "build" ]; then
curl -L https://github.com/Widen/cloudfront-auth/archive/master.zip --output cloudfront-auth-master.zip
unzip -q cloudfront-auth-master.zip -d build/
mkdir build/cloudfront-auth-master/distributions
cp ${data.local_file.build-js.filename} build/cloudfront-auth-master/build/build.js
cd build/cloudfront-auth-master && npm i minimist && 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 = {
vendor = var.auth_vendor
cloudfront_distribution = var.cloudfront_distribution
client_id = var.client_id
client_secret = var.client_secret
redirect_uri = var.redirect_uri
hd = var.hd
session_duration = var.session_duration
authz = var.authz
github_organization = try(var.github_organization, "")
}
provisioner "local-exec" {
command = local.build_lambda_command
}
}
# Copies the artifact to the root directory
resource "null_resource" "copy_lambda_artifact" {
triggers = {
build_resource = null_resource.build_lambda.id
}
provisioner "local-exec" {
command = "cp build/cloudfront-auth-master/distributions/${var.cloudfront_distribution}/${var.cloudfront_distribution}.zip ${local.lambda_filename}"
}
}
# workarout 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
}
# Block direct public access
resource "aws_s3_bucket_public_access_block" "default" {
bucket = aws_s3_bucket.default.id
block_public_acls = true
block_public_policy = true
restrict_public_buckets = true
}
data "aws_iam_policy_document" "s3_bucket_policy" {
statement {
actions = [
"s3:GetObject",
]
resources = [
"${aws_s3_bucket.default.arn}/*",
]
principals {
type = "AWS"
identifiers = [
aws_cloudfront_origin_access_identity.default.iam_arn,
]
}
}
statement {
actions = [
"s3:ListBucket",
]
resources = [
aws_s3_bucket.default.arn,
]
principals {
type = "AWS"
identifiers = [
aws_cloudfront_origin_access_identity.default.iam_arn,
]
}
}
}
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 = false
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 = "none"
locations = []
}
}
# Handle the case where no certificate ARN provided
dynamic "viewer_certificate" {
for_each = (var.cloudfront_acm_certificate_arn == null ? { use_acm = false } : {})
content {
ssl_support_method = "sni-only"
cloudfront_default_certificate = true
}
}
# Handle the case where certificate ARN was provided
dynamic "viewer_certificate" {
for_each = (var.cloudfront_acm_certificate_arn != null ? { use_acm = true } : {}
)
content {
ssl_support_method = "sni-only"
acm_certificate_arn = var.cloudfront_acm_certificate_arn
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" {
provider = aws.us-east-1
description = "Managed by Terraform"
runtime = "nodejs12.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
depends_on = [null_resource.copy_lambda_artifact]
}
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
}