forked from scalefactory/terraform-cloudfront-auth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
225 lines (189 loc) · 4.97 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
#
# S3
#
resource "aws_s3_bucket" "default" {
bucket = local.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
ignore_public_acls = true
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 = local.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([local.bucket_name], var.cloudfront_aliases)
comment = var.name
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.
locals {
name = replace(var.name, ".", "-")
}
resource "aws_lambda_function" "default" {
function_name = "${local.name}-auth"
description = "${var.name}-auth"
runtime = "nodejs14.x"
role = aws_iam_role.lambda_role.arn
filename = var.lambda_filename
handler = "index.handler"
publish = true
timeout = 5
source_code_hash = sha256(var.lambda_filename)
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 = "${var.name}-auth-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 = "${var.name}-log-access"
policy = data.aws_iam_policy_document.lambda_log_access.json
}