-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
286 lines (259 loc) · 7.15 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
# Copyright © 2024 Joseph Wright <joseph@cloudboss.co>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
locals {
aws_account_id = data.aws_caller_identity.me.account_id
aws_region = data.aws_region.here.name
iam_policy_statement_kms = (
var.kms_key_id == null
? []
: [
{
Action = ["kms:Decrypt"]
Effect = "Allow"
Resource = [one(data.aws_kms_key.it[*].arn)]
},
]
)
iam_policy_statement_ssm = [
{
Action = [
"ssm:GetParameter",
"ssm:GetParametersByPath",
]
Effect = "Allow"
Resource = ["${local.ssm_arn_prefix}${var.tailscale.authkey_ssm_path}"]
},
]
iam_policy_statements = concat(
local.iam_policy_statement_kms,
local.iam_policy_statement_ssm,
)
init_scripts = (
var.enable_nat_gateway
? [
<<-EOF
#!/bin/sh
/sbin/iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
EOF
]
: []
)
lambda_vpc_config = (
var.lambda_configure_vpc
? {
id = var.vpc_id
subnet_ids = var.subnet_ids
}
: null
)
routes = toset(
var.tailscale.route_vpc_cidr
? concat([data.aws_vpc.it.cidr_block], var.tailscale.routes)
: var.tailscale.routes
)
security_group_ids = concat(
[aws_security_group.it.id],
var.extra_security_group_ids,
)
ssm_arn_prefix = "arn:aws:ssm:${local.aws_region}:${local.aws_account_id}:parameter"
}
data "aws_caller_identity" "me" {}
data "aws_region" "here" {}
data "aws_kms_key" "it" {
count = var.kms_key_id == null ? 0 : 1
key_id = var.kms_key_id
}
data "aws_vpc" "it" {
id = var.vpc_id
}
resource "aws_security_group" "it" {
name = var.stack_key
tags = var.tags
vpc_id = var.vpc_id
}
module "enable_router_lambda" {
source = "cloudboss/asg-enable-router/aws"
version = "0.1.1"
autoscaling_group_name = var.stack_key
iam_permissions_boundary = var.iam.permissions_boundary
name = "${var.stack_key}-enable-router"
tags = var.tags
vpc_config = local.lambda_vpc_config
}
module "security_group_rules" {
source = "cloudboss/security-group-rules/aws"
version = "0.1.0"
rules = [
{
cidr_ipv4 = "0.0.0.0/0"
from_port = 41641
ip_protocol = "udp"
to_port = 41641
type = "ingress"
},
{
cidr_ipv4 = "0.0.0.0/0"
from_port = 3478
ip_protocol = "udp"
to_port = 3478
type = "egress"
},
{
cidr_ipv4 = "0.0.0.0/0"
from_port = 443
ip_protocol = "tcp"
to_port = 443
type = "egress"
},
{
cidr_ipv4 = "0.0.0.0/0"
from_port = 443
ip_protocol = "udp"
to_port = 443
type = "egress"
},
{
cidr_ipv4 = data.aws_vpc.it.cidr_block
ip_protocol = "-1"
type = "ingress"
},
{
cidr_ipv4 = data.aws_vpc.it.cidr_block
ip_protocol = "-1"
type = "egress"
},
]
security_group_id = aws_security_group.it.id
tags = var.tags
}
module "iam_role" {
source = "cloudboss/iam-role/aws"
version = "0.1.0"
trust_policy_statements = [
{
Action = "sts:AssumeRole"
Effect = "Allow"
Principal = {
Service = "ec2.amazonaws.com"
}
},
]
create_instance_profile = true
name = var.stack_key
permissions_boundary = var.iam.permissions_boundary
policy_arns = var.iam.extra_policy_arns
policy_statements = local.iam_policy_statements
tags = var.tags
}
module "user_data" {
source = "cloudboss/easyto-user-data/aws"
version = "0.1.0"
env = [
{
name = "TS_ACCEPT_DNS"
value = tostring(var.tailscale.accept_dns)
},
{
name = "TS_EXTRA_ARGS"
value = join(" ", var.tailscale.extra_args)
},
{
name = "TS_ROUTES"
value = join(",", local.routes)
},
{
name = "TS_STATE_DIR"
value = var.tailscale.state_dir
},
{
name = "TS_TAILSCALED_EXTRA_ARGS"
value = join(" ", var.tailscale.tailscaled_extra_args)
},
{
name = "TS_USERSPACE"
value = tostring(var.tailscale.userspace)
},
]
env-from = [
{
ssm = {
name = "TS_AUTHKEY"
path = var.tailscale.authkey_ssm_path
}
}
]
init-scripts = local.init_scripts
sysctls = [
{
name = "net.ipv4.ip_forward"
value = "1"
},
{
name = "net.ipv6.conf.all.forwarding"
value = "1"
},
]
}
module "asg" {
source = "cloudboss/asg/aws"
version = "0.1.0"
ami = var.ami
block_device_mappings = [
{
device_name = var.volume.name
ebs = {
iops = var.volume.iops
volume_size = var.volume.size
volume_type = var.volume.type
}
},
]
instance_initiated_shutdown_behavior = "terminate"
instance_refresh = try(var.autoscaling.instance_refresh, {
strategy = "Rolling"
})
instance_type = try(var.autoscaling.instance_type, null)
instances_desired = try(var.autoscaling.instances_desired, 1)
instances_max = try(var.autoscaling.instances_max, 1)
instances_min = try(var.autoscaling.instances_min, 1)
iam_instance_profile = module.iam_role.instance_profile.arn
max_instance_lifetime = try(var.autoscaling.max_instance_lifetime, null)
mixed_instances_distribution = try(var.autoscaling.mixed_instances_distribution, {})
mixed_instances_overrides = try(var.autoscaling.mixed_instances_overrides, [])
name = var.stack_key
network_interfaces = try(var.autoscaling.network_interfaces, [
{
associate_public_ip_address = true
},
])
security_group_ids = local.security_group_ids
ssh_key = var.ssh_key
subnet_ids = var.subnet_ids
suspended_processes = try(var.autoscaling.suspended_processes, null)
tags = {
default = var.tags
}
termination_policies = try(var.autoscaling.termination_policies, null)
user_data = {
value = module.user_data.value
}
vpc_id = var.vpc_id
depends_on = [module.enable_router_lambda]
}