-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathterraform.tf
234 lines (193 loc) · 5.65 KB
/
terraform.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
terraform {
backend "s3" {
bucket = "twc-boston-us-east-1"
key = "terraform/website.tfstate"
region = "us-east-1"
}
required_version = "~> 1.0"
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 4.0"
}
}
}
provider "aws" {
region = "us-east-1"
default_tags {
tags = local.tags
}
}
locals {
app = "website"
domain_name = "boston.techworkerscoalition.org"
repo = "https://github.com/techworkersco/twc-site-boston"
tags = {
App = local.app
Repo = local.repo
}
}
###########
# DNS #
###########
resource "aws_acm_certificate" "cert" {
domain_name = local.domain_name
subject_alternative_names = ["*.${local.domain_name}"]
validation_method = "DNS"
}
resource "aws_route53_zone" "zone" {
name = local.domain_name
}
resource "aws_route53_record" "a" {
name = aws_apigatewayv2_domain_name.api.domain_name
type = "A"
zone_id = aws_route53_zone.zone.id
alias {
evaluate_target_health = true
name = aws_apigatewayv2_domain_name.api.domain_name_configuration.0.target_domain_name
zone_id = aws_apigatewayv2_domain_name.api.domain_name_configuration.0.hosted_zone_id
}
}
###################
# API GATEWAY #
###################
resource "aws_apigatewayv2_api" "api" {
description = "Boston TWC website"
name = "website"
protocol_type = "HTTP"
}
resource "aws_apigatewayv2_api_mapping" "api" {
api_id = aws_apigatewayv2_api.api.id
domain_name = aws_apigatewayv2_domain_name.api.id
stage = aws_apigatewayv2_stage.api.id
}
resource "aws_apigatewayv2_domain_name" "api" {
domain_name = local.domain_name
domain_name_configuration {
certificate_arn = aws_acm_certificate.cert.arn
endpoint_type = "REGIONAL"
security_policy = "TLS_1_2"
}
}
resource "aws_apigatewayv2_integration" "api" {
api_id = aws_apigatewayv2_api.api.id
description = "Boston TWC website"
integration_method = "POST"
integration_type = "AWS_PROXY"
integration_uri = aws_lambda_function.lambda.invoke_arn
}
resource "aws_apigatewayv2_route" "api" {
for_each = toset(["GET /", "GET /{proxy+}"])
api_id = aws_apigatewayv2_api.api.id
route_key = each.key
authorization_type = "NONE"
target = "integrations/${aws_apigatewayv2_integration.api.id}"
}
resource "aws_apigatewayv2_stage" "api" {
api_id = aws_apigatewayv2_api.api.id
auto_deploy = true
name = "$default"
access_log_settings {
destination_arn = aws_cloudwatch_log_group.api.arn
format = jsonencode({
httpMethod = "$context.httpMethod"
ip = "$context.identity.sourceIp"
protocol = "$context.protocol"
requestId = "$context.requestId"
requestTime = "$context.requestTime"
responseLength = "$context.responseLength"
routeKey = "$context.routeKey"
status = "$context.status"
})
}
lifecycle {
ignore_changes = [deployment_id]
}
}
############
# LOGS #
############
resource "aws_cloudwatch_log_group" "api" {
name = "/aws/apigatewayv2/${aws_apigatewayv2_api.api.name}"
retention_in_days = 30
}
resource "aws_cloudwatch_log_group" "lambda" {
name = "/aws/lambda/${aws_lambda_function.lambda.function_name}"
retention_in_days = 30
}
##############
# LAMBDA #
##############
resource "aws_iam_role" "lambda" {
name = "${local.app}-lambda"
managed_policy_arns = ["arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"]
assume_role_policy = jsonencode({
Version = "2012-10-17"
Statement = [{
Sid = "AssumeLambda"
Action = "sts:AssumeRole"
Effect = "Allow"
Principal = { Service = "lambda.amazonaws.com" }
}]
})
}
resource "aws_lambda_function" "lambda" {
description = "Boston TWC Website"
filename = "${path.module}/package.zip"
function_name = "website"
handler = "index.handler"
memory_size = 2048
role = aws_iam_role.lambda.arn
runtime = "nodejs18.x"
source_code_hash = filebase64sha256("${path.module}/package.zip")
timeout = 30
environment {
variables = {
GOOGLE_API_KEY = var.GOOGLE_API_KEY
GOOGLE_CALENDAR_ID = var.GOOGLE_CALENDAR_ID
}
}
}
resource "aws_lambda_permission" "lambda" {
for_each = toset(["GET/", "GET/*"])
action = "lambda:InvokeFunction"
function_name = aws_lambda_function.lambda.arn
principal = "apigateway.amazonaws.com"
source_arn = "${aws_apigatewayv2_api.api.execution_arn}/${aws_apigatewayv2_stage.api.name}/${each.value}"
}
##########
# S3 #
##########
resource "aws_s3_bucket" "bucket" {
bucket = "twc-boston-us-east-1"
}
resource "aws_s3_bucket_public_access_block" "bucket" {
bucket = aws_s3_bucket.bucket.id
block_public_acls = true
block_public_policy = true
ignore_public_acls = true
restrict_public_buckets = true
}
#################
# VARIABLES #
#################
variable "GOOGLE_API_KEY" {
description = "Google API key"
type = string
}
variable "GOOGLE_CALENDAR_ID" {
description = "Google Calendar ID"
type = string
default = "[email protected]"
}
###############
# OUTPUTS #
###############
output "url" {
description = "Boston TWC website URL"
value = "https://boston.techworkerscoalition.org/"
}
output "name_servers" {
description = "boston.techworkerscoalition.org nameservers"
value = [for x in aws_route53_zone.zone.name_servers : x]
}