-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.tf
executable file
·221 lines (184 loc) · 5.12 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
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 5.65.0"
}
random = {
source = "hashicorp/random"
version = "~> 3.6.2"
}
archive = {
source = "hashicorp/archive"
version = "~> 2.5.0"
}
}
required_version = ">= 1.2.0"
}
provider "aws" {
region = var.aws_region
}
resource "random_pet" "lambda_bucket_name" {
prefix = "bgg3dprints"
length = 4
}
resource "random_pet" "website_bucket_name" {
prefix = "bgg3dprints-web"
length = 4
}
resource "aws_s3_bucket" "lambda_bucket" {
bucket = random_pet.lambda_bucket_name.id
}
#.deploy/terraform/static-site/iam.tf
data "aws_iam_policy_document" "website_policy" {
statement {
actions = [
"s3:GetObject"
]
principals {
identifiers = ["*"]
type = "*"
}
resources = [
"${aws_s3_bucket.website_bucket.arn}/*"
]
}
}
resource "aws_s3_bucket" "website_bucket" {
bucket = "${var.app_name}.${var.domain_name}"
}
resource "aws_s3_bucket_policy" "website_bucket_policy" {
bucket = aws_s3_bucket.website_bucket.id
policy = data.aws_iam_policy_document.website_policy.json
}
resource "aws_s3_bucket_website_configuration" "website_bucket_configuration" {
bucket = aws_s3_bucket.website_bucket.id
index_document {
suffix = "index.html"
}
error_document {
key = "error.html"
}
routing_rule {
condition {
key_prefix_equals = "search/"
}
redirect {
replace_key_prefix_with = "result/"
host_name = "${aws_lambda_function_url.test_latest.url_id}.lambda-url.${var.aws_region}.on.aws"
#host_name = <url_id>.lambda-url.<region>.on.aws
#host_name = "iil7g7seqoihgoayfd5hr2pkx40ikost.lambda-url.us-east-1.on.aws"
protocol = "https"
}
}
}
resource "aws_s3_bucket_public_access_block" "website_public_access_block" {
bucket = aws_s3_bucket.website_bucket.id
block_public_acls = false
block_public_policy = false
ignore_public_acls = false
restrict_public_buckets = false
}
# [
# {
# "Condition": {
# "KeyPrefixEquals": "search/"
# },
# "Redirect": {
# "HostName": "iil7g7seqoihgayfd5hr2pkx40ikost.lambda-url.us-east-1.on.aws",
# "Protocol": "https",
# "ReplaceKeyPrefixWith": "result/"
# }
# }
# ]
resource "aws_s3_object" "object1" {
for_each = fileset("dist/", "*")
bucket = aws_s3_bucket.website_bucket.id
key = each.value
content_type = "text/html"
source = "dist/${each.value}"
#acl = "public-read"
etag = filemd5("dist/${each.value}")
}
# Upload zip to S3
data "archive_file" "bgg3dprints" {
type = "zip"
source_dir = "${path.module}/app"
output_path = "${path.module}/app.zip"
}
data "archive_file" "bgg3dprints_layer" {
type = "zip"
source_dir = "${path.module}/bgg3dprints-layer"
output_path = "${path.module}/bgg3dprints-layer.zip"
}
resource "aws_lambda_layer_version" "lambda_layer" {
filename = "${path.module}/bgg3dprints-layer.zip"
layer_name = "bgg3dprints_layer"
compatible_runtimes = ["python3.10"]
}
resource "aws_s3_object" "bgg3dprints" {
bucket = aws_s3_bucket.lambda_bucket.id
key = "app.zip"
source = data.archive_file.bgg3dprints.output_path
etag = filemd5(data.archive_file.bgg3dprints.output_path)
}
# Create Lamdba function
resource "aws_lambda_function" "bgg3dprints" {
function_name = "bgg3dprints"
s3_bucket = aws_s3_bucket.lambda_bucket.id
s3_key = aws_s3_object.bgg3dprints.key
runtime = "python3.10"
handler = "lambda_function.lambda_handler"
timeout = 300
source_code_hash = data.archive_file.bgg3dprints.output_base64sha256
layers = [aws_lambda_layer_version.lambda_layer.arn]
role = aws_iam_role.lambda_exec.arn
}
resource "aws_cloudwatch_log_group" "bgg3dprints" {
name = "/aws/lambda/${aws_lambda_function.bgg3dprints.function_name}"
retention_in_days = 30
}
resource "aws_iam_role" "lambda_exec" {
name = "serverless_lambda"
assume_role_policy = jsonencode({
Version = "2012-10-17"
Statement = [{
Action = "sts:AssumeRole"
Effect = "Allow"
Sid = ""
Principal = {
Service = "lambda.amazonaws.com"
}
}
]
})
}
resource "aws_iam_role_policy_attachment" "lambda_policy" {
role = aws_iam_role.lambda_exec.name
policy_arn = "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
}
resource "aws_lambda_function_url" "test_latest" {
function_name = aws_lambda_function.bgg3dprints.function_name
authorization_type = "NONE"
cors {
allow_credentials = false
allow_origins = ["*"]
allow_methods = ["*"]
allow_headers = ["date", "keep-alive"]
expose_headers = ["keep-alive", "date"]
max_age = 86400
}
}
resource "aws_route53_zone" "primary" {
name = var.domain_name
}
resource "aws_route53_record" "web" {
zone_id = aws_route53_zone.primary.zone_id
name = var.app_name
type = "A"
alias {
name = aws_s3_bucket_website_configuration.website_bucket_configuration.website_domain
zone_id = aws_s3_bucket.website_bucket.hosted_zone_id
evaluate_target_health = false
}
}