-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathterraform.tf
263 lines (227 loc) · 6.57 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
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
variable "region" {
type = string
default = "eu-west-1"
}
provider "aws" {
region = var.region
}
variable "gateway_log_level" {
# Allowed values:
# - OFF
# - INFO
# - ERROR
default = "INFO"
}
variable "lambda_log_debug" {
default = "false"
}
variable "gateway_data_trace_enabled" {
default = false
}
data "aws_caller_identity" "current" {
}
locals {
app_name = "dyndns"
account_id = data.aws_caller_identity.current.account_id
stack_name = terraform.workspace
stack_fullname = "${local.app_name}_${local.stack_name}"
object_name_prefix = "${local.stack_fullname}_"
object_name_format = "${local.object_name_prefix}%s"
api_name = format(local.object_name_format, "api")
api_description = "DynDNS2 API for AWS Route53"
lambda_zip_file = "zip/dyndns_lambda.zip"
lambda_function_name = format(local.object_name_format, "api_lambda")
ssm_param_username = "/${local.app_name}/${local.stack_name}/username"
ssm_param_password = "/${local.app_name}/${local.stack_name}/password"
}
# This is global. For the second or subsequent stack in an account, you will need to run
# > terraform import aws_api_gateway_account.gateway aws_api_gateway_account
# Alternatively you can delete this resource and configure the API gateway role manually
resource "aws_api_gateway_account" "gateway" {
cloudwatch_role_arn = aws_iam_role.aws_gw_role.arn
}
# This is global. For the second or subsequent stack in an account, you will need to run
# > terraform import aws_iam_role.aws_gw_role aws_gw_role
# Alternatively you can delete this resource and configure the API gateway role manually
resource "aws_iam_role" "aws_gw_role" {
name = "aws_gw_role"
description = "Allows API Gateway to call AWS resources on your behalf."
assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "",
"Effect": "Allow",
"Principal": {
"Service": "apigateway.amazonaws.com"
},
"Action": "sts:AssumeRole"
}
]
}
EOF
}
resource "aws_iam_policy_attachment" "aws_gw_role_cloudwatchlog" {
name = "aws_gw_role_cloudwatchlog"
roles = [aws_iam_role.aws_gw_role.name]
policy_arn = "arn:aws:iam::aws:policy/service-role/AmazonAPIGatewayPushToCloudWatchLogs"
}
resource "aws_iam_role" "lambda_exec_role" {
name = format(local.object_name_format, "lambda_exec_role")
path = "/"
description = "Lambda execution role for ${local.lambda_function_name}"
assume_role_policy = <<POLICY
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Service": "lambda.amazonaws.com"
},
"Action": "sts:AssumeRole"
}
]
}
POLICY
}
# This is the policy for the lambda execution role, which controls what actions
# the lambda can perform
data "aws_iam_policy_document" "lambda_exec_policy_doc" {
# Allow access to read/modify Route53
statement {
actions = [
"route53:GetHostedZone",
"route53:ChangeResourceRecordSets",
"route53:ListResourceRecordSets",
"route53:GetHostedZoneLimit",
]
resources = [
"arn:aws:route53:::hostedzone/*",
]
}
statement {
actions = [
"route53:ListHostedZones",
"route53:GetHostedZoneCount",
"route53:ListHostedZonesByName",
]
resources = [
"*",
]
}
# Cloudwatch access
statement {
actions = [
"logs:CreateLogGroup",
]
resources = [
"arn:aws:logs:${var.region}:${local.account_id}:*",
]
}
statement {
actions = [
"logs:CreateLogStream",
"logs:PutLogEvents",
]
resources = [
"arn:aws:logs:${var.region}:${local.account_id}:log-group:/aws/lambda/${aws_lambda_function.api_lambda.function_name}:*",
]
}
# SSM Parameters access
statement {
actions = [
"ssm:DescribeParameters",
]
resources = [
"*",
]
}
statement {
actions = [
"ssm:GetParameters",
]
resources = [
"arn:aws:ssm:${var.region}:${local.account_id}:parameter/${local.app_name}/${local.stack_name}/*",
]
}
# KMS access
statement {
actions = [
"kms:Decrypt",
]
resources = [
"*",
]
}
}
resource "aws_iam_role_policy" "lambda_exec" {
name_prefix = "${local.stack_fullname}-"
role = aws_iam_role.lambda_exec_role.id
policy = data.aws_iam_policy_document.lambda_exec_policy_doc.json
}
resource "aws_lambda_function" "api_lambda" {
filename = local.lambda_zip_file
function_name = local.lambda_function_name
role = aws_iam_role.lambda_exec_role.arn
handler = "index.handler"
source_code_hash = filebase64sha256(local.lambda_zip_file)
runtime = "nodejs16.x"
timeout = 15
publish = true
environment {
variables = {
stack = local.stack_name
}
}
}
resource "aws_lambda_permission" "allow_api_gateway" {
function_name = aws_lambda_function.api_lambda.function_name
statement_id = "AllowExecutionFromApiGateway"
action = "lambda:InvokeFunction"
principal = "apigateway.amazonaws.com"
source_arn = "arn:aws:execute-api:${var.region}:${local.account_id}:${aws_api_gateway_rest_api.rest_api.id}/*/*/*"
}
data "template_file" "swagger_api_def" {
template = file("${path.module}/doc/api-swagger-template.yaml")
vars = {
api_name = local.api_name
lambda_uri = aws_lambda_function.api_lambda.invoke_arn
}
}
resource "aws_api_gateway_rest_api" "rest_api" {
name = local.api_name
description = local.api_description
body = data.template_file.swagger_api_def.rendered
}
resource "aws_api_gateway_deployment" "rest_api_deployment" {
rest_api_id = aws_api_gateway_rest_api.rest_api.id
stage_name = "live"
variables = {
"stack" = local.stack_name
"log_debug" = var.lambda_log_debug
"username_param" = local.ssm_param_username
"password_param" = local.ssm_param_password
}
}
output "rest_api_base_url" {
value = aws_api_gateway_deployment.rest_api_deployment.invoke_url
}
# Enable logging for all API gateway methods
resource "aws_api_gateway_method_settings" "api_gateway_settings" {
rest_api_id = aws_api_gateway_rest_api.rest_api.id
stage_name = aws_api_gateway_deployment.rest_api_deployment.stage_name
method_path = "*/*"
settings {
metrics_enabled = true
logging_level = var.gateway_log_level
data_trace_enabled = var.gateway_data_trace_enabled
}
}
output "username_param" {
value = local.ssm_param_username
}
output "password_param" {
value = local.ssm_param_password
}