aboutsummaryrefslogtreecommitdiff
path: root/terraform/aws/backend/main.tf
blob: 57e9348c99906f2b54a1ee2735dadb8666e654f9 (plain)
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
terraform {
  backend "http" {}
}

variable "aws_access_key" {
  type = "string"
}

variable "aws_secret_key" {
  type = "string"
}

variable "aws_region" {
  type = "string"
}

variable "aws_vpc_id" {
  type = "string"
}

variable "aws_route_53_zone_id" {
  type = "string"
}

variable "ssh_public_key" {
  type = "string"
}

variable "guix_substitute_servers" {
  type = "map"
  default = {
    "https://berlin.guixsd.org"    = <<EOF
(entry
 (public-key
  (ecc
   (curve Ed25519)
   (q #8D156F295D24B0D9A86FA5741A840FF2D24F60F7B6C4134814AD55625971B394#)
  )
 )
 (tag
  (guix import)
 )
)
EOF
    "http://beid.cbaines.net"      = <<EOF
(entry
 (public-key
  (ecc
   (curve Ed25519)
   (q #50349E83123851A65A569304A12E512B698223A81E10BEEC5A3E56EDAEE5DAC1#)
  )
 )
 (tag
  (guix import)
 )
)
EOF
  }
}

variable "mini_environment_admin_guix_public_key" {
  type = "string"
}

variable "mini_environment_admin_public_ip_address" {
  type = "string"
}

variable "backend_slug" {
  type = "string"
}

locals {
  guix_daemon_substitute_servers = "${join(" ", keys(var.guix_substitute_servers))}"
}

provider "aws" {
  access_key = "${var.aws_access_key}"
  secret_key = "${var.aws_secret_key}"
  region  = "${var.aws_region}"
}

data "aws_route53_zone" "main" {
  zone_id = "${var.aws_route_53_zone_id}"
}

data "template_file" "guix_daemon_service" {
  template = "${file("${path.module}/guix-daemon.service.tpl")}"

  vars {
    substitute_servers = "${local.guix_daemon_substitute_servers}"
  }
}

data "aws_availability_zones" "available" {}


resource "aws_default_subnet" "main" {
  count = "${length(data.aws_availability_zones.available.names)}"
  availability_zone = "${data.aws_availability_zones.available.names[count.index]}"
}

resource "aws_efs_file_system" "main" {
  creation_token = "govuk_mini_environment_admin/${var.backend_slug}"

  tags {
    Name = "govuk_mini_environment_admin/${var.backend_slug}"
  }
}

resource "aws_efs_mount_target" "main" {
  count = "${length(data.aws_availability_zones.available.names)}"
  file_system_id = "${aws_efs_file_system.main.id}"
  subnet_id      = "${aws_default_subnet.main.*.id[count.index]}"

  security_groups = ["${aws_security_group.efs_mount_target.id}"]
}

resource "aws_key_pair" "deployer" {
  key_name = "govuk_mini_environment_admin/${var.backend_slug}/deployer"
  public_key = "${var.ssh_public_key}"
}

resource "aws_security_group" "public_webserver" {
  name        = "govuk_mini_environment_admin/${var.backend_slug}/public_webserver"
  description = "For instances running public facing web servers"
  vpc_id      = "${var.aws_vpc_id}"

  ingress {
    from_port       = 0
    to_port         = 80
    protocol        = "tcp"
    cidr_blocks = ["0.0.0.0/0"]
  }

  ingress {
    from_port       = 0
    to_port         = 443
    protocol        = "tcp"
    cidr_blocks = ["0.0.0.0/0"]
  }

  ingress {
    from_port       = 0
    to_port         = 8080
    protocol        = "tcp"
    cidr_blocks = ["0.0.0.0/0"]
  }

  ingress {
    from_port       = 0
    to_port         = 8443
    protocol        = "tcp"
    cidr_blocks = ["0.0.0.0/0"]
  }
}

resource "aws_security_group" "ssh_access_from_mini_environment_admin" {
  name        = "govuk_mini_environment_admin/${var.backend_slug}/ssh_access_from_mini_environment_admin"
  description = "For instances that need SSH access for Terraform and Guix builds"
  vpc_id      = "${var.aws_vpc_id}"

  ingress {
    from_port = 0
    to_port = 22
    protocol = "tcp"
    cidr_blocks = ["${var.mini_environment_admin_public_ip_address}/32"]
  }
}

resource "aws_security_group" "guix_client" {
  name        = "govuk_mini_environment_admin/${var.backend_slug}/guix_client"
  description = "For instances with access to the guix_daemon instance"
  vpc_id      = "${var.aws_vpc_id}"

  egress {
    from_port = 0
    to_port = 0
    protocol = "-1"
    cidr_blocks = ["0.0.0.0/0"]
  }
}

resource "aws_security_group" "guix_daemon" {
  name        = "govuk_mini_environment_admin/${var.backend_slug}/guix_daemon"
  description = "For the guix_daemon instance."
  vpc_id      = "${var.aws_vpc_id}"

  ingress {
    from_port       = 44146
    to_port         = 44146
    protocol        = "tcp"
    security_groups = ["${aws_security_group.guix_client.id}"]
  }

  egress {
    from_port = 0
    to_port = 0
    protocol = "-1"
    cidr_blocks = ["0.0.0.0/0"]
  }
}

resource "aws_security_group" "efs_mount_target" {
  name        = "govuk_mini_environment_admin/${var.backend_slug}/efs_mount_target"
  description = "For the EFS File System mount targets"
  vpc_id      = "${var.aws_vpc_id}"

  ingress {
    from_port       = 0
    to_port         = 0
    protocol        = "-1"
    security_groups = [
      "${aws_security_group.guix_client.id}",
      "${aws_security_group.guix_daemon.id}",
    ]
  }
}

resource "aws_spot_instance_request" "main" {
  ami             = "ami-8fd760f6"
  instance_type   = "t2.large"
  key_name        = "${aws_key_pair.deployer.key_name}"
  security_groups = [
    "${aws_security_group.guix_daemon.name}",
    "${aws_security_group.ssh_access_from_mini_environment_admin.name}"

  ]

  wait_for_fulfillment = true
  spot_price = "0.05"

  depends_on = ["aws_efs_mount_target.main"]

  provisioner "file" {
    content = "${data.template_file.guix_daemon_service.rendered}"
    destination = "/home/ubuntu/guix-daemon.service"

    connection {
      type     = "ssh"
      user     = "ubuntu"
    }
  }

  provisioner "file" {
    content = "(acl\n ${join("\n", values(var.guix_substitute_servers))}\n${var.mini_environment_admin_guix_public_key}\n)\n"
    destination = "/home/ubuntu/acl"

    connection {
      type     = "ssh"
      user     = "ubuntu"
    }
  }

  provisioner "remote-exec" {
    inline = [
      "sudo apt-get update",
      "sudo apt-get update",
      "sudo apt-get -y install nfs-common cachefilesd nscd",
      "sudo tune2fs -o user_xattr /dev/xvda1",
      "sudo sed 's/#RUN/RUN/' -i /etc/default/cachefilesd",
      "sudo mkdir -p /mnt/efs",
      "echo \"${aws_efs_file_system.main.dns_name}:/ /mnt/efs nfs4 nfsvers=4.1,rsize=1048576,wsize=1048576,fsc,hard,timeo=600,retrans=2 0 0\" | sudo tee -a /etc/fstab",
      "sudo mount -a",
      <<EOF
if [ ! -d "/mnt/efs/gnu" ]; then
  cd /mnt/efs
  sudo wget https://alpha.gnu.org/gnu/guix/guix-binary-0.14.0.x86_64-linux.tar.xz
  sudo tar --warning=no-timestamp -xf guix-binary-0.14.0.x86_64-linux.tar.xz
  cd -
fi
EOF
      ,
      "sudo mkdir -p /gnu/store /var/guix",
      "echo \"${aws_efs_file_system.main.dns_name}:/var/guix /var/guix nfs4 nfsvers=4.1,rsize=1048576,wsize=1048576,hard,timeo=600,retrans=2 0 0\" | sudo tee -a /etc/fstab",
      "echo \"${aws_efs_file_system.main.dns_name}:/gnu/store /gnu/store nfs4 nfsvers=4.1,rsize=1048576,wsize=1048576,fsc,hard,timeo=600,retrans=2 0 0\" | sudo tee -a /etc/fstab",
      "sudo mount -a",
      "sudo mv /home/ubuntu/guix-daemon.service /etc/systemd/system/guix-daemon.service",
      "sudo mkdir /etc/guix",
      "sudo mv /home/ubuntu/acl /etc/guix/acl",
      "sudo groupadd --system guixbuild",
      <<EOF
for i in `seq -w 1 17`;
do
  sudo useradd -g guixbuild -G guixbuild           \
          -d /var/empty -s `which nologin`    \
          -c "Guix build user $i" --system    \
          guixbuilder$i;
done
EOF
      ,
      "sudo systemctl daemon-reload",
      "sudo systemctl enable guix-daemon.service",
      "sudo systemctl start guix-daemon.service",
      <<EOF
if [ ! -d "/var/guix/profiles/per-user/ubuntu" ]; then
  /var/guix/profiles/per-user/root/guix-profile/bin/guix package -i guile guix
else
  ln -s /var/guix/profiles/per-user/ubuntu/guix-profile ~/.guix-profile
fi
EOF
      ,
      # This is needed for things like guix copy to work
      "echo 'GUIX_PROFILE=/home/ubuntu/.guix-profile; source /home/ubuntu/.guix-profile/etc/profile' | cat - .bashrc > temp && mv temp .bashrc"
    ]

    connection {
      type     = "ssh"
      user     = "ubuntu"
    }
  }
}

resource "aws_route53_record" "main" {
  zone_id = "${data.aws_route53_zone.main.zone_id}"
  name    = "guix-daemon"
  type    = "A"
  ttl     = "60"
  records = ["${aws_spot_instance_request.main.public_ip}"]
}

# Outputs

output "deployer_key_pair_name" {
  value = "${aws_key_pair.deployer.key_name}"
}

output "guix_client_security_group_name" {
  value = "${aws_security_group.guix_client.name}"
}

output "public_webserver_security_group_name" {
  value = "${aws_security_group.public_webserver.name}"
}

output "ssh_access_from_mini_environment_admin_security_group_name" {
  value = "${aws_security_group.ssh_access_from_mini_environment_admin.name}"
}

output "guix_daemon_private_dns" {
  value = "${aws_spot_instance_request.main.private_dns}"
}

output "efs_file_system_dns_name" {
  value = "${aws_efs_file_system.main.dns_name}"
}