-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmain.tf
147 lines (120 loc) · 3.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
#
# K3S resources
#
resource "random_password" "token" {
length = 32
special = false
}
resource "random_password" "db" {
length = 32
special = false
}
#
# Control plane Lb
#
module "controlplane_lb" {
source = "./modules/controlplane-lb"
name = var.name
vpc_id = var.vpc_id
subnets = var.subnets
# AWS CCM Tagging
tags = merge({
"kubernetes.io/cluster/${var.name}" = "owned"
}, var.tags)
}
#
# Database
#
module "db" {
source = "./modules/database"
name = var.name
vpc_id = var.vpc_id
subnets = var.subnets
password = random_password.db.result
ca_cert_identifier = var.rds_ca_cert_identifier
tags = var.tags
}
#
# Cluster Shared Security Group
#
resource "aws_security_group" "cluster" {
name = "${var.name}-k3s-cluster-sg"
vpc_id = var.vpc_id
description = "Shared cluster k3s server/agent security group"
tags = merge({
"shared" = "true",
}, var.tags)
}
# TODO: Trim these down
resource "aws_security_group_rule" "all_self_ingress" {
description = "Allow all ingress traffic between cluster nodes"
from_port = 0
to_port = 0
protocol = "-1"
security_group_id = aws_security_group.cluster.id
type = "ingress"
self = true
}
resource "aws_security_group_rule" "all_self_egress" {
description = "Allow all egress traffic"
from_port = 0
to_port = 0
protocol = "-1"
security_group_id = aws_security_group.cluster.id
type = "egress"
cidr_blocks = ["0.0.0.0/0"]
}
#
# Shared Security Groups
#
resource "aws_security_group" "shared_server" {
name = "${var.name}-k3s-shared-server-sg"
vpc_id = var.vpc_id
description = "Shared k3s server security group"
tags = merge({
"shared" = "true",
}, var.tags)
}
resource "aws_security_group_rule" "controlplane_ingress" {
description = "All traffic between nodes"
from_port = module.controlplane_lb.port
to_port = module.controlplane_lb.port
protocol = "tcp"
security_group_id = aws_security_group.shared_server.id
type = "ingress"
cidr_blocks = [data.aws_vpc.this.cidr_block]
}
resource "aws_security_group_rule" "server_db_ingress" {
description = "Allow servers to connect to DB"
from_port = module.db.port
to_port = module.db.port
protocol = "tcp"
security_group_id = module.db.sg
type = "ingress"
source_security_group_id = aws_security_group.shared_server.id
}
resource "aws_security_group" "shared_agent" {
name = "${var.name}-k3s-shared-agent-sg"
vpc_id = var.vpc_id
description = "Shared k3s agent security group"
tags = merge({
"shared" = "true",
}, var.tags)
}
#
# State Storage
#
module "state" {
source = "./modules/state-store"
count = var.state_bucket == null ? 1 : 0
name = var.name
}
resource "aws_s3_bucket_object" "state" {
bucket = var.state_bucket == null ? module.state[0].bucket : var.state_bucket
key = "state.env"
content_type = "text/plain"
content = <<-EOT
TOKEN=${random_password.token.result}
DATASTORE_ENDPOINT=${module.db.datastore_endpoint}
EOT
}