-
Notifications
You must be signed in to change notification settings - Fork 0
/
ec2.tf
213 lines (194 loc) · 7.89 KB
/
ec2.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
resource "aws_key_pair" "boundary" {
key_name = "${var.prefix}-${random_pet.test.id}"
public_key = tls_private_key.boundary.public_key_openssh
}
data "aws_ami" "ubuntu" {
most_recent = true
filter {
name = "name"
values = ["ubuntu/images/hvm-ssd/ubuntu-focal-20.04-amd64-server-*"]
}
filter {
name = "virtualization-type"
values = ["hvm"]
}
owners = ["099720109477"] # Canonical
}
resource "aws_instance" "worker" {
ami = data.aws_ami.ubuntu.id
instance_type = "t3.micro"
iam_instance_profile = aws_iam_instance_profile.boundary.name
subnet_id = local.public_subs[1]
key_name = aws_key_pair.boundary.key_name
vpc_security_group_ids = [aws_security_group.boundary.id]
associate_public_ip_address = true
connection {
type = "ssh"
user = "ubuntu"
private_key = tls_private_key.boundary.private_key_pem
host = self.public_ip
bastion_host = aws_instance.controller.public_ip
}
provisioner "file" {
on_failure = continue
content = templatefile("${path.module}/install/worker.hcl.tpl", {
controller_public_ip = aws_instance.controller.public_ip
controller_private_ip = aws_instance.controller.private_ip
name_suffix = ""
region = var.region
public_ip = self.public_ip
private_ip = self.private_ip
tls_disabled = var.tls_disabled
tls_key_path = var.tls_key_path
tls_cert_path = var.tls_cert_path
kms_type = var.kms_type
kms_worker_auth_key_id = aws_kms_key.worker_auth.id
})
destination = "~/boundary-worker.hcl"
}
provisioner "file" {
on_failure = continue
source = "${path.module}/install/install.sh"
destination = "~/install.sh"
}
provisioner "remote-exec" {
on_failure = continue
inline = [
"sudo mkdir -p /etc/pki/tls/boundary",
"echo '${tls_private_key.boundary.private_key_pem}' | sudo tee ${var.tls_key_path}",
"echo '${tls_self_signed_cert.boundary.cert_pem}' | sudo tee ${var.tls_cert_path}",
]
}
provisioner "remote-exec" {
on_failure = continue
inline = [
"sudo apt -y update",
"sudo apt -y install unzip",
"wget https://releases.hashicorp.com/boundary/0.7.1/boundary_0.7.1_linux_amd64.zip",
"unzip boundary_0.7.1_linux_amd64.zip -d ~/boundary",
"sudo mv ~/boundary/boundary /usr/local/bin/boundary",
"sudo chmod 0755 /usr/local/bin/boundary",
"sudo mv ~/boundary-worker.hcl /etc/boundary-worker.hcl",
"sudo chmod 0755 ~/install.sh",
"sudo ~/./install.sh worker",
]
}
depends_on = [aws_instance.controller]
}
resource "aws_instance" "controller" {
ami = data.aws_ami.ubuntu.id
instance_type = "t3.micro"
iam_instance_profile = aws_iam_instance_profile.boundary.name
subnet_id = local.public_subs[1]
key_name = aws_key_pair.boundary.key_name
vpc_security_group_ids = [aws_security_group.boundary.id]
associate_public_ip_address = true
connection {
type = "ssh"
user = "ubuntu"
private_key = tls_private_key.boundary.private_key_pem
host = self.public_ip
}
provisioner "file" {
on_failure = continue
source = "${path.module}/install/install.sh"
destination = "~/install.sh"
}
provisioner "file" {
on_failure = continue
source = "${path.module}/install/northwind-database.sql"
destination = "~/northwind-database.sql"
}
provisioner "file" {
on_failure = continue
source = "${path.module}/install/northwind-roles.sql"
destination = "~/northwind-roles.sql"
}
# aws_db_instance.boundary.endpoint
provisioner "file" {
on_failure = continue
content = templatefile("${path.module}/install/controller.hcl.tpl", {
name_suffix = ""
db_endpoint = "postgresql://${var.psql_user}:${var.psql_pw}@localhost/boundary?sslmode=disable"
private_ip = self.private_ip
tls_disabled = var.tls_disabled
tls_key_path = var.tls_key_path
tls_cert_path = var.tls_cert_path
kms_type = var.kms_type
kms_worker_auth_key_id = aws_kms_key.worker_auth.id
kms_recovery_key_id = aws_kms_key.recovery.id
kms_root_key_id = aws_kms_key.root.id
})
destination = "~/boundary-controller.hcl"
}
provisioner "remote-exec" {
on_failure = continue
inline = [
"sudo mkdir -p /etc/pki/tls/boundary",
"echo '${tls_private_key.boundary.private_key_pem}' | sudo tee ${var.tls_key_path}",
"echo '${tls_self_signed_cert.boundary.cert_pem}' | sudo tee ${var.tls_cert_path}",
]
}
provisioner "remote-exec" {
on_failure = continue
inline = [
"sudo apt -y update",
"sudo apt -y install postgresql-client",
#setup docker
"sudo apt-get install ca-certificates curl gnupg lsb-release",
"curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg",
"echo \"deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable\" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null",
"sudo apt-get -y update",
"sudo apt-get -y install docker-ce docker-ce-cli containerd.io",
"sudo docker network create bnet",
#setup psql container
"sudo docker create --name boundary_psql --network bnet -it -p 5432:5432 -e POSTGRES_PASSWORD=${var.psql_pw} -e POSTGRES_USER=${var.psql_user} postgres",
"sudo docker start boundary_psql",
"sleep 5",
"psql \"postgresql://${var.psql_user}:${var.psql_pw}@localhost/postgres\" -c 'create database boundary';",
#populate northwinds database
"psql \"postgresql://${var.psql_user}:${var.psql_pw}@localhost/postgres\" -c 'create database northwind';",
"psql \"postgresql://${var.psql_user}:${var.psql_pw}@localhost/northwind\" -f ~/northwind-database.sql --quiet",
"psql \"postgresql://${var.psql_user}:${var.psql_pw}@localhost/northwind\" -f ~/northwind-roles.sql --quiet",
#install boundary
"sudo apt -y install unzip",
"wget https://releases.hashicorp.com/boundary/0.7.1/boundary_0.7.1_linux_amd64.zip",
"unzip boundary_0.7.1_linux_amd64.zip -d ~/boundary",
"sudo mv ~/boundary/boundary /usr/local/bin/boundary",
"sudo chmod 0755 /usr/local/bin/boundary",
"sudo mv ~/boundary-controller.hcl /etc/boundary-controller.hcl",
"sudo chmod 0755 ~/install.sh",
"sudo ~/./install.sh controller",
]
}
}
resource "aws_instance" "tfc_agent" {
ami = data.aws_ami.ubuntu.id
instance_type = "t3.micro"
iam_instance_profile = aws_iam_instance_profile.boundary.name
subnet_id = local.private_subs[1]
key_name = aws_key_pair.boundary.key_name
vpc_security_group_ids = [aws_security_group.tfc_agent.id]
associate_public_ip_address = true
connection {
type = "ssh"
user = "ubuntu"
private_key = tls_private_key.boundary.private_key_pem
host = self.private_ip
bastion_host = aws_instance.controller.public_ip
}
provisioner "remote-exec" {
on_failure = continue
inline = [
"sudo apt -y update",
"sudo apt-get -y update",
"sudo apt-get -y install unzip",
"sudo apt-get -y install tmux",
"wget https://releases.hashicorp.com/tfc-agent/1.0.2/tfc-agent_1.0.2_linux_amd64.zip",
"unzip tfc-agent_1.0.2_linux_amd64.zip",
"export TFC_AGENT_TOKEN=${var.tfc_agent_token}",
"export TFC_AGENT_NAME=atarc_aws_enclave_01",
"tmux new -d ./tfc-agent"
]
}
}