@@ -24,25 +24,74 @@ terraform {
24
24
}
25
25
26
26
provider "aws" {
27
- region = " us-west-2 "
27
+ region = " us-east-1 "
28
28
shared_credentials_files = [" ../secrets/plaintext/aws_credentials" ]
29
29
}
30
30
31
+ locals {
32
+ # The availability zone to create the EC2 instance and EBS volumes in. The
33
+ # AWS instance and EBS volumes need to be in the same AZ.
34
+ az = " us-east-1e"
35
+
36
+ # This is a volume that gets mounted on /nix.
37
+ #
38
+ # The AWS docs recommended a device name like "/dev/sdf":
39
+ # https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/device_naming.html#available-ec2-device-names
40
+ # But when actually trying to use "/dev/sdf", the actual device gets created
41
+ # with a name like "/dev/xvdf", so we just use that here.
42
+ nix_volume_name = " /dev/xvdf"
43
+ }
44
+
31
45
resource "aws_instance" "binplz_server" {
32
46
ami = aws_ami. binplz_ami . id
33
47
instance_type = " t2.micro"
34
48
vpc_security_group_ids = [aws_security_group . my_security_group . id ]
35
49
user_data_replace_on_change = true
50
+ availability_zone = local. az
36
51
37
52
# We could also use a file provisioner here, but I've found that to be a bit more fragile since it requires SSH access.
38
53
user_data = << EOF
39
54
#!/run/current-system/sw/bin/bash
55
+
40
56
PATH=/run/current-system/sw/bin
41
57
echo "${ file (" ../secrets/plaintext/nixbuild.pem" )} " > /root/nixbuild.pem
42
58
chmod 0600 /root/nixbuild.pem
59
+
60
+ # format the /nix volume if it is not already formatted.
61
+ NIX_VOLUME_FS_TYPE="$(file -s '${ local . nix_volume_name } ' | awk '{print $2}')"
62
+
63
+ # If no FS, then this output contains "data"
64
+ if [ "$NIX_VOLUME_FS_TYPE" = "data" ]; then then
65
+ mkfs.ext4 '${ local . nix_volume_name } '
66
+ fi
67
+
68
+ mkdir -p /mnt/nix
69
+ mount '${ local . nix_volume_name } ' /mnt/to-be-nix
70
+ cp -rp /nix/* /mnt/nix/
71
+ umount /mnt/nix
72
+
73
+ mount '${ local . nix_volume_name } ' /nix
74
+
43
75
EOF
44
76
}
45
77
78
+ resource "aws_volume_attachment" "nix_volume_attachement" {
79
+ device_name = local. nix_volume_name
80
+ volume_id = aws_ebs_volume. nix_volume . id
81
+ instance_id = aws_instance. binplz_server . id
82
+ }
83
+
84
+ resource "aws_ebs_volume" "nix_volume" {
85
+ # An EBS volume must be created in a specific AZ.
86
+ availability_zone = local. az
87
+ size = 100 # 100GB disk
88
+ type = " gp3"
89
+
90
+ tags = {
91
+ Name = " /nix directory"
92
+ }
93
+ }
94
+
46
95
output "public_ip_addr" {
47
96
value = aws_eip. binplz_eip . public_ip
48
97
}
@@ -53,7 +102,8 @@ resource "aws_eip" "binplz_eip" {
53
102
54
103
resource "null_resource" "dns_update" {
55
104
triggers = {
56
- # Note that after deploying binplz at least once, we will likely never re-provision this Elastic IP, so it is very unlikely to ever change.
105
+ # Note that after deploying binplz at least once, we will likely never
106
+ # re-provision this Elastic IP, so it is very unlikely to ever change.
57
107
ip_change = aws_eip.binplz_eip.public_ip
58
108
}
59
109
provisioner "local-exec" {
0 commit comments