forked from tailscale-dev/examples-infrastructure-as-code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.tf
More file actions
286 lines (239 loc) · 7.11 KB
/
main.tf
File metadata and controls
286 lines (239 loc) · 7.11 KB
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
locals {
name = "example-${basename(path.cwd)}"
aws_tags = {
Name = local.name
}
// Modify these to use your own VPC
vpc_cidr_block = module.vpc.vpc_cidr_block
vpc_id = module.vpc.vpc_id
subnet_ids = module.vpc.private_subnets
security_group_ids = [aws_security_group.tailscale.id]
# EKS cluster configuration
cluster_version = "1.30"
node_instance_type = "t3.medium"
node_capacity_type = "ON_DEMAND"
node_ami_type = "AL2_x86_64"
desired_size = 2
max_size = 4
min_size = 1
# Tailscale configuration
tailscale_oauth_client_id = var.tailscale_oauth_client_id
tailscale_oauth_client_secret = var.tailscale_oauth_client_secret
}
// Remove this to use your own VPC.
module "vpc" {
source = "../internal-modules/aws-vpc"
name = local.name
tags = local.aws_tags
cidr = "10.0.0.0/16"
public_subnets = ["10.0.1.0/24", "10.0.2.0/24"]
private_subnets = ["10.0.10.0/24", "10.0.20.0/24"]
}
# EKS Cluster
resource "aws_eks_cluster" "main" {
name = local.name
role_arn = aws_iam_role.cluster.arn
version = local.cluster_version
vpc_config {
subnet_ids = local.subnet_ids
endpoint_private_access = true
endpoint_public_access = true
public_access_cidrs = ["0.0.0.0/0"]
security_group_ids = [aws_security_group.cluster.id]
}
# Enable logging
enabled_cluster_log_types = ["api", "audit", "authenticator", "controllerManager", "scheduler"]
tags = local.aws_tags
depends_on = [
aws_iam_role_policy_attachment.cluster_AmazonEKSClusterPolicy,
aws_cloudwatch_log_group.eks_cluster,
module.vpc.nat_ids, # remove if using your own VPC
]
}
# EKS Node Group
resource "aws_eks_node_group" "main" {
cluster_name = aws_eks_cluster.main.name
node_group_name = "${local.name}-nodes"
node_role_arn = aws_iam_role.node_group.arn
subnet_ids = local.subnet_ids
capacity_type = local.node_capacity_type
ami_type = local.node_ami_type
instance_types = [local.node_instance_type]
scaling_config {
desired_size = local.desired_size
max_size = local.max_size
min_size = local.min_size
}
update_config {
max_unavailable = 1
}
tags = local.aws_tags
depends_on = [
aws_iam_role_policy_attachment.node_group_AmazonEKSWorkerNodePolicy,
aws_iam_role_policy_attachment.node_group_AmazonEKS_CNI_Policy,
aws_iam_role_policy_attachment.node_group_AmazonEC2ContainerRegistryReadOnly,
]
}
# CloudWatch Log Group for EKS Cluster
resource "aws_cloudwatch_log_group" "eks_cluster" {
name = "/aws/eks/${local.name}/cluster"
retention_in_days = 7
tags = local.aws_tags
}
# Kubernetes namespace for Tailscale operator
resource "kubernetes_namespace" "tailscale_operator" {
metadata {
name = "tailscale"
labels = {
"pod-security.kubernetes.io/enforce" = "privileged"
}
}
depends_on = [aws_eks_node_group.main]
}
# Deploy Tailscale Operator using Helm
resource "helm_release" "tailscale_operator" {
name = "tailscale-operator"
repository = "https://pkgs.tailscale.com/helmcharts"
chart = "tailscale-operator"
version = "1.84.0"
namespace = kubernetes_namespace.tailscale_operator.metadata[0].name
values = [
yamlencode({
operatorConfig = {
image = {
repo = "tailscale/k8s-operator"
tag = "v1.84.0"
}
}
apiServerProxyConfig = {
mode = "true"
tags = "tag:k8s-operator,tag:k8s-api-server"
}
oauth = {
clientId = local.tailscale_oauth_client_id
clientSecret = local.tailscale_oauth_client_secret
hostname = "${local.name}-operator"
tags = "tag:k8s-operator"
}
})
]
set_sensitive {
name = "oauth.clientId"
value = local.tailscale_oauth_client_id
}
set_sensitive {
name = "oauth.clientSecret"
value = local.tailscale_oauth_client_secret
}
depends_on = [
kubernetes_namespace.tailscale_operator,
aws_eks_node_group.main,
]
}
# Security group for EKS cluster
resource "aws_security_group" "cluster" {
name_prefix = "${local.name}-cluster-"
vpc_id = local.vpc_id
tags = merge(
local.aws_tags,
{
Name = "${local.name}-cluster"
}
)
}
resource "aws_security_group_rule" "cluster_egress" {
security_group_id = aws_security_group.cluster.id
type = "egress"
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
ipv6_cidr_blocks = ["::/0"]
}
# Security group for Tailscale traffic
resource "aws_security_group" "tailscale" {
vpc_id = local.vpc_id
name = "${local.name}-tailscale"
tags = merge(
local.aws_tags,
{
Name = "${local.name}-tailscale"
}
)
}
resource "aws_security_group_rule" "tailscale_ingress" {
security_group_id = aws_security_group.tailscale.id
type = "ingress"
from_port = 41641
to_port = 41641
protocol = "udp"
cidr_blocks = ["0.0.0.0/0"]
ipv6_cidr_blocks = ["::/0"]
}
resource "aws_security_group_rule" "tailscale_egress" {
security_group_id = aws_security_group.tailscale.id
type = "egress"
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
ipv6_cidr_blocks = ["::/0"]
}
resource "aws_security_group_rule" "internal_vpc_ingress_ipv4" {
security_group_id = aws_security_group.tailscale.id
type = "ingress"
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = [local.vpc_cidr_block]
}
# IAM Role for EKS Cluster
resource "aws_iam_role" "cluster" {
name = "${local.name}-cluster-role"
assume_role_policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Action = "sts:AssumeRole"
Effect = "Allow"
Principal = {
Service = "eks.amazonaws.com"
}
}
]
})
tags = local.aws_tags
}
resource "aws_iam_role_policy_attachment" "cluster_AmazonEKSClusterPolicy" {
policy_arn = "arn:aws:iam::aws:policy/AmazonEKSClusterPolicy"
role = aws_iam_role.cluster.name
}
# IAM Role for EKS Node Group
resource "aws_iam_role" "node_group" {
name = "${local.name}-node-group-role"
assume_role_policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Action = "sts:AssumeRole"
Effect = "Allow"
Principal = {
Service = "ec2.amazonaws.com"
}
}
]
})
tags = local.aws_tags
}
resource "aws_iam_role_policy_attachment" "node_group_AmazonEKSWorkerNodePolicy" {
policy_arn = "arn:aws:iam::aws:policy/AmazonEKSWorkerNodePolicy"
role = aws_iam_role.node_group.name
}
resource "aws_iam_role_policy_attachment" "node_group_AmazonEKS_CNI_Policy" {
policy_arn = "arn:aws:iam::aws:policy/AmazonEKS_CNI_Policy"
role = aws_iam_role.node_group.name
}
resource "aws_iam_role_policy_attachment" "node_group_AmazonEC2ContainerRegistryReadOnly" {
policy_arn = "arn:aws:iam::aws:policy/AmazonEC2ContainerRegistryReadOnly"
role = aws_iam_role.node_group.name
}