/
main.tf
416 lines (358 loc) · 11.8 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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
### Create certiifcate in ACM for HTTPS traffic
resource "aws_acm_certificate" "acm-cert" {
domain_name = "${var.rootdomain}"
subject_alternative_names = [
"*.${var.rootdomain}",
"${var.rootdomain}",
]
validation_method = "DNS"
}
### SUBDOMAIN BUCKET CREATION WITH OAI POLICY
resource "aws_s3_bucket" "subdomain-bucket" {
# (resource arguments)
bucket = var.subdomain
force_destroy = true
}
resource "aws_s3_bucket_policy" "cloudfront-oai-access" {
bucket = aws_s3_bucket.subdomain-bucket.id
policy = data.aws_iam_policy_document.allow_access_from_cloudfront.json
}
data "aws_iam_policy_document" "allow_access_from_cloudfront" {
statement {
principals {
type = "AWS"
identifiers = ["arn:aws:iam::cloudfront:user/CloudFront Origin Access Identity ${aws_cloudfront_origin_access_identity.sub-oai.id}"]
}
actions = [
"s3:GetObject",
]
resources = [
aws_s3_bucket.subdomain-bucket.arn,
"${aws_s3_bucket.subdomain-bucket.arn}/*",
]
}
depends_on = [aws_s3_bucket.subdomain-bucket]
}
resource "aws_s3_bucket_public_access_block" "subdomain-public-access" {
block_public_acls = true
block_public_policy = true
bucket = aws_s3_bucket.subdomain-bucket.bucket
ignore_public_acls = true
restrict_public_buckets = true
}
### ROOTDOMAIN BUCKET CREATION WITH TRAFFIC REDIRECT TO SUBDOMAIN BUCKET
resource "aws_s3_bucket" "rootdomain-bucket" {
bucket = var.rootdomain
}
resource "aws_s3_bucket_public_access_block" "rootdomain-public-access" {
block_public_acls = true
block_public_policy = true
bucket = aws_s3_bucket.rootdomain-bucket.bucket
ignore_public_acls = true
restrict_public_buckets = true
}
resource "aws_s3_bucket_website_configuration" "root-redirect" {
bucket = aws_s3_bucket.rootdomain-bucket.bucket
# redirect all requests to subdomain from root domain
redirect_all_requests_to {
host_name = aws_s3_bucket.subdomain-bucket.bucket
protocol = "https"
}
}
resource "aws_cloudfront_distribution" "subdomain-distribution" {
# (resource arguments)
origin {
# do something about the next 2 lines
domain_name = "${aws_s3_bucket.subdomain-bucket.bucket}.s3.${aws_s3_bucket.subdomain-bucket.region}.amazonaws.com"
origin_id = "${aws_s3_bucket.subdomain-bucket.bucket}.s3.${aws_s3_bucket.subdomain-bucket.region}.amazonaws.com"
s3_origin_config {
origin_access_identity = "origin-access-identity/cloudfront/${aws_cloudfront_origin_access_identity.sub-oai.id}"
}
}
aliases = [
"${var.subdomain}",
]
comment = "subdomain distribution"
default_root_object = "index.html"
enabled = true
http_version = "http2"
is_ipv6_enabled = true
price_class = "PriceClass_100"
wait_for_deployment = false
default_cache_behavior {
allowed_methods = ["GET", "HEAD"]
cached_methods = ["GET", "HEAD"]
forwarded_values {
query_string = false
cookies {
forward = "none"
}
}
compress = true
default_ttl = 0
max_ttl = 0
min_ttl = 0
# do something about line below
target_origin_id = "${var.subdomain}.s3.us-east-1.amazonaws.com"
viewer_protocol_policy = "redirect-to-https"
}
restrictions {
geo_restriction {
locations = []
restriction_type = "none"
}
}
viewer_certificate {
acm_certificate_arn = aws_acm_certificate.acm-cert.arn
cloudfront_default_certificate = false
minimum_protocol_version = "TLSv1.2_2021"
ssl_support_method = "sni-only"
}
}
resource "aws_cloudfront_origin_access_identity" "sub-oai" {
comment = "access-identity-${var.subdomain}.s3.us-east-1.amazonaws.com"
}
### ROOT DOMAIN CF DISTRIBUTION
resource "aws_cloudfront_distribution" "rootdomain-distribution" {
# (resource arguments)
origin {
### do something about the next 2 lines
domain_name = aws_s3_bucket_website_configuration.root-redirect.website_endpoint
origin_id = aws_s3_bucket_website_configuration.root-redirect.website_endpoint
custom_origin_config {
http_port = 80
https_port = 443
origin_keepalive_timeout = 5
origin_protocol_policy = "http-only"
origin_read_timeout = 30
origin_ssl_protocols = [
"TLSv1.2",
]
}
}
aliases = [
"${var.rootdomain}",
]
comment = "root distribution"
enabled = true
http_version = "http2"
is_ipv6_enabled = true
price_class = "PriceClass_100"
wait_for_deployment = false
default_cache_behavior {
allowed_methods = ["GET", "HEAD"]
cached_methods = ["GET", "HEAD"]
forwarded_values {
query_string = false
cookies {
forward = "none"
}
}
compress = true
default_ttl = 0
max_ttl = 0
min_ttl = 0
target_origin_id = aws_s3_bucket_website_configuration.root-redirect.website_endpoint
viewer_protocol_policy = "redirect-to-https"
}
restrictions {
geo_restriction {
locations = []
restriction_type = "none"
}
}
viewer_certificate {
acm_certificate_arn = aws_acm_certificate.acm-cert.arn
cloudfront_default_certificate = false
minimum_protocol_version = "TLSv1.2_2021"
ssl_support_method = "sni-only"
}
depends_on = [aws_s3_bucket.rootdomain-bucket, aws_s3_bucket_website_configuration.root-redirect]
}
# Create A records for cloudfront distributions in Route53
resource "aws_route53_record" "www" {
name = "${var.subdomain}"
type = "A"
zone_id = var.zone_ID
alias {
evaluate_target_health = false
name = aws_cloudfront_distribution.subdomain-distribution.domain_name
zone_id = aws_cloudfront_distribution.subdomain-distribution.hosted_zone_id
}
}
resource "aws_route53_record" "root" {
name = "${var.rootdomain}"
type = "A"
zone_id = var.zone_ID
alias {
evaluate_target_health = false
name = aws_cloudfront_distribution.rootdomain-distribution.domain_name
zone_id = aws_cloudfront_distribution.rootdomain-distribution.hosted_zone_id
}
}
# Create DynamoDB to store site visitor count
resource "aws_dynamodb_table" "visitorCountStore" {
name = var.dynamodb_table
billing_mode = "PAY_PER_REQUEST"
hash_key = "site"
attribute {
name = "site"
type = "S"
}
}
# Initialize visitor count by setting current count to zero
resource "aws_dynamodb_table_item" "initiateCount" {
table_name = aws_dynamodb_table.visitorCountStore.name
hash_key = aws_dynamodb_table.visitorCountStore.hash_key
item = <<ITEM
{
"site": {"S": "CRC"},
"visitor_count": {"N": "0"}
}
ITEM
}
# Create lambda function to update table count and give it required permissions
resource "aws_iam_role" "lambda-iam-role" {
name = "lambda-iam"
assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Action": "sts:AssumeRole",
"Principal": {
"Service": "lambda.amazonaws.com"
},
"Effect": "Allow",
"Sid": ""
}
]
}
EOF
}
resource "aws_iam_policy" "lambda_policy" {
policy = jsonencode({
"Version": "2012-10-17",
"Statement": [
{
"Action": [
"dynamodb:GetItem",
"dynamodb:PutItem",
"dynamodb:UpdateItem",
],
"Effect": "Allow",
"Resource": "${aws_dynamodb_table.visitorCountStore.arn}"
},
{
"Effect": "Allow",
"Action": [
"logs:CreateLogGroup",
"logs:CreateLogStream",
"logs:PutLogEvents",
],
"Resource": "*"
}
]
})
}
resource "aws_iam_role_policy_attachment" "lambda_policy_att" {
role = "${aws_iam_role.lambda-iam-role.name}"
policy_arn = aws_iam_policy.lambda_policy.arn
}
resource "aws_lambda_function" "visitCounter" {
# If the file is not in the current working directory you will need to include a
# path.module in the filename.
filename = "lambda.zip"
function_name = var.lambda_name
role = aws_iam_role.lambda-iam-role.arn
handler = "lambda_function.lambda_handler"
source_code_hash = data.archive_file.lambda-zip.output_base64sha256
runtime = "python3.9"
depends_on = [
aws_dynamodb_table.visitorCountStore
]
}
data "archive_file" "lambda-zip" {
type = "zip"
#source_file = "{path.module}/../../lambda/my-function/index.js" #python file location
source_dir = "${path.module}/lambda/"
output_path = "lambda.zip"
}
### Create API Gateway resource to invoke backend lambda function
resource "aws_api_gateway_rest_api" "invokeLambdaAPI" {
name = var.apigw_name
description = "Invokes Lambda function to update visitor count in DynamoDB and return current value"
endpoint_configuration {
types = ["REGIONAL"]
}
}
# Allow API Gateway to invoke lambda function
resource "aws_lambda_permission" "APIGWAccess" {
statement_id = "AllowAPIGatewayToInvokeLambdaFunction"
action = "lambda:InvokeFunction"
function_name = aws_lambda_function.visitCounter.function_name
principal = "apigateway.amazonaws.com"
source_arn = "${aws_api_gateway_rest_api.invokeLambdaAPI.execution_arn}/*/*/*"
depends_on = [
aws_lambda_function.visitCounter
]
}
# Creating an API resource 'count'
resource "aws_api_gateway_resource" "count" {
rest_api_id = aws_api_gateway_rest_api.invokeLambdaAPI.id
parent_id = aws_api_gateway_rest_api.invokeLambdaAPI.root_resource_id
path_part = "count"
}
// GET method
resource "aws_api_gateway_method" "get" {
rest_api_id = aws_api_gateway_rest_api.invokeLambdaAPI.id
resource_id = aws_api_gateway_resource.count.id
http_method = "GET"
authorization = "NONE"
}
// Integrate API with Lambda fucncion visitCounter
resource "aws_api_gateway_integration" "integration-get" {
rest_api_id = aws_api_gateway_rest_api.invokeLambdaAPI.id
resource_id = aws_api_gateway_resource.count.id
http_method = aws_api_gateway_method.get.http_method
integration_http_method = "POST"
type = "AWS_PROXY"
uri = "${aws_lambda_function.visitCounter.invoke_arn}"
}
// Deploy API
resource "aws_api_gateway_deployment" "deployment" {
rest_api_id = aws_api_gateway_rest_api.invokeLambdaAPI.id
depends_on = [
aws_api_gateway_integration.integration-get
]
lifecycle {
create_before_destroy = true
}
}
// Stage name of deployment
resource "aws_api_gateway_stage" "prod" {
deployment_id = aws_api_gateway_deployment.deployment.id
rest_api_id = aws_api_gateway_rest_api.invokeLambdaAPI.id
stage_name = var.stage_name
access_log_settings {
destination_arn = aws_cloudwatch_log_group.api-logs.arn
format = jsonencode({
requestId = "$context.requestId"
sourceIp = "$context.identity.sourceIp"
requestTime = "$context.requestTime"
protocol = "$context.protocol"
httpMethod = "$context.httpMethod"
resourcePath = "$context.resourcePath"
routeKey = "$context.routeKey"
status = "$context.status"
responseLength = "$context.responseLength"
integrationErrorMessage = "$context.integrationErrorMessage"
}
)
}
depends_on = [aws_cloudwatch_log_group.api-logs]
}
resource "aws_cloudwatch_log_group" "api-logs" {
name = "API-Gateway-Execution-Logs_${aws_api_gateway_rest_api.invokeLambdaAPI.id}/${var.stage_name}"
retention_in_days = 7
}