@@ -24,25 +24,74 @@ terraform {
2424}
2525
2626provider "aws" {
27- region = " us-west-2 "
27+ region = " us-east-1 "
2828 shared_credentials_files = [" ../secrets/plaintext/aws_credentials" ]
2929}
3030
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+
3145resource "aws_instance" "binplz_server" {
3246 ami = aws_ami. binplz_ami . id
3347 instance_type = " t2.micro"
3448 vpc_security_group_ids = [aws_security_group . my_security_group . id ]
3549 user_data_replace_on_change = true
50+ availability_zone = local. az
3651
3752 # We could also use a file provisioner here, but I've found that to be a bit more fragile since it requires SSH access.
3853 user_data = << EOF
3954#!/run/current-system/sw/bin/bash
55+
4056PATH=/run/current-system/sw/bin
4157echo "${ file (" ../secrets/plaintext/nixbuild.pem" )} " > /root/nixbuild.pem
4258chmod 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+
4375EOF
4476}
4577
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+
4695output "public_ip_addr" {
4796 value = aws_eip. binplz_eip . public_ip
4897}
@@ -53,7 +102,8 @@ resource "aws_eip" "binplz_eip" {
53102
54103resource "null_resource" "dns_update" {
55104 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.
57107 ip_change = aws_eip.binplz_eip.public_ip
58108 }
59109 provisioner "local-exec" {
0 commit comments