-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
174 lines (148 loc) · 5.79 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
locals {
mime_types = {
"html" = "text/html",
"css" = "text/css",
"js" = "application/javascript",
"png" = "image/png",
"jpg" = "image/jpeg",
"svg" = "image/svg+xml",
// Add other file extensions and MIME types as needed
}
}
# S3 Bucket Module Configuration
module "resume_bucket" {
source = "terraform-aws-modules/s3-bucket/aws"
bucket = "cloudresume-don"
# Versioning to be enabled once VC is setup.
versioning = {
enabled = true
mfa_delete = false
}
}
# resource "aws_s3_object" "s3_resume_object" {
# for_each = fileset("${path.module}/s3_resume_bucket_files", "*")
# bucket = "cloudresume-don" # Specify your S3 bucket name
# key = each.value # The key (path) in the bucket
# source = "${path.module}/s3_resume_bucket_files/${each.value}" # Path to the local file
# etag = filemd5("${path.module}/s3_resume_bucket_files/${each.value}")
# }
resource "aws_s3_object" "s3_resume_object" {
for_each = fileset("${path.module}/s3_resume_bucket_files", "**/*")
bucket = "cloudresume-don"
key = each.value
source = "${path.module}/s3_resume_bucket_files/${each.value}"
etag = filemd5("${path.module}/s3_resume_bucket_files/${each.value}")
content_type = lookup(local.mime_types, split(".", each.value)[1], "binary/octet-stream")
}
# CloudFront Distribution Module Configuration
module "cloudfront_resume" {
source = "terraform-aws-modules/cloudfront/aws"
aliases = ["*.donangeles.com", "donangeles.com"]
comment = null
default_root_object = "index.html"
enabled = true
http_version = "http2and3"
is_ipv6_enabled = true
price_class = "PriceClass_All"
retain_on_delete = false
wait_for_deployment = true
web_acl_id = null
# Origin configuration pointing to the S3 bucket
origin = {
content = {
domain_name = "cloudresume-don.s3.us-east-1.amazonaws.com"
origin_access_control_id = "E2RU6J1N6S39CV"
origin_id = "cloudresume-don.s3-website-us-east-1.amazonaws.com"
}
}
# Default cache behavior settings
default_cache_behavior = {
allowed_methods = ["GET", "HEAD"]
cache_policy_id = "658327ea-f89d-4fab-a63d-7e88639e58f6"
cached_methods = ["GET", "HEAD"]
target_origin_id = "cloudresume-don.s3-website-us-east-1.amazonaws.com"
viewer_protocol_policy = "redirect-to-https"
compress = true
use_forwarded_values = false
}
# Viewer certificate configuration
viewer_certificate = {
acm_certificate_arn = "arn:aws:acm:us-east-1:408810908626:certificate/90e8be72-67ba-4297-9d7f-31d095a46fbd"
cloudfront_default_certificate = false
iam_certificate_id = null
minimum_protocol_version = "TLSv1.2_2021"
ssl_support_method = "sni-only"
}
}
# DynamoDB Table Module Configuration
module "ddb_resume_table" {
source = "terraform-aws-modules/dynamodb-table/aws"
name = "visitCounter"
# Setting timeouts and tags explicitly to override module defaults
timeouts = {}
tags = null
}
# API Gateway Module Configuration
module "apigw_resume" {
source = "terraform-aws-modules/apigateway-v2/aws"
name = "visitorCounterAPI"
# CORS configuration for the API
cors_configuration = {
allow_headers = ["'content-type,x-amz-date,authorization,x-api-key,x-amz-security-token'"]
allow_methods = ["POST", "OPTIONS", "GET"]
allow_origins = ["https://www.donangeles.com", "https://donangeles.com"]
}
create_api_domain_name = false
create_default_stage = false
create_routes_and_integrations = true
integrations = {
"POST /countVisit" = {
lambda_arn = aws_lambda_function.lambda_update_count.arn
connection_type = "INTERNET"
integration_type = "AWS_PROXY"
payload_format_version = "2.0"
timeout_milliseconds = 30000
}
"OPTIONS /countVisit" = {
lambda_arn = aws_lambda_function.lambda_update_count.arn
connection_type = "INTERNET"
integration_type = "AWS_PROXY"
payload_format_version = "2.0"
timeout_milliseconds = 30000
}
}
}
# AWS Lambda Function Resource Configuration
resource "aws_lambda_function" "lambda_update_count" {
function_name = "countVisit"
handler = "lambda_function.lambda_handler"
runtime = "python3.9"
role = "arn:aws:iam::408810908626:role/lambda-ddb-visitcount"
memory_size = 128
timeout = 3
package_type = "Zip"
#source_code_hash = "GpCmZ2hqv/4gy/RvyxrfWF2vq4r1wM6oo+k52y4OcrA="
filename = data.archive_file.update_count_zip.output_path #"${path.module}/lambda_functions/update_count.zip"
source_code_hash = data.archive_file.update_count_zip.output_base64sha256 #filebase64sha256("${path.module}/lambda_functions/update_count.zip")
# Additional configuration details
architectures = ["x86_64"]
reserved_concurrent_executions = -1
ephemeral_storage {
size = 512
}
tracing_config {
mode = "PassThrough"
}
}
data "archive_file" "update_count_zip" {
type = "zip"
source_dir = "${path.module}/lambda_functions/update_count"
output_path = "${path.module}/lambda_functions/builds/update_count.zip"
}
output "lambda_function_source_code_hash" {
description = "The base64-encoded SHA256 hash of the Lambda function's source code"
value = aws_lambda_function.lambda_update_count.source_code_hash
#data.archive_file.update_count_zip.output_base64sha256
#filebase64sha256("${path.module}/lambda_functions/update_count.zip")
#aws_lambda_function.lambda_update_count.source_code_hash
}