diff --git a/instance/cloud-config.yml b/instance/cloud-config.yml index a0f4e93..436632b 100644 --- a/instance/cloud-config.yml +++ b/instance/cloud-config.yml @@ -7,6 +7,9 @@ packages: - awscli runcmd: - set -e + - while [ ! -b $(readlink -f ${ebs_device_path}) ]; do echo "waiting for EBS attachment..">&2; sleep 5; done + - test -z "$(blkid ${ebs_device_path})" && mkfs -t ext4 -L factorio ${ebs_device_path} + - mount -t ext4 -o noatime -L factorio /opt - adduser --disabled-login --gecos GECOS --no-create-home --home /opt/factorio factorio - curl -L "https://www.factorio.com/get-download/${factorio_version}/headless/linux64" -o /opt/factorio.tar.gz - tar -C /opt -x -f /opt/factorio.tar.gz diff --git a/instance/main.tf b/instance/main.tf index 260cf4a..de1cf48 100644 --- a/instance/main.tf +++ b/instance/main.tf @@ -48,6 +48,7 @@ data "template_file" "cloud_config" { template = file("./cloud-config.yml") vars = { aws_region = var.region + ebs_device_path = var.ebs_device_path factorio_version = var.factorio_version } } @@ -141,11 +142,24 @@ ENV } } +resource "aws_ebs_volume" "factorio_data" { + size = 5 + availability_zone = aws_instance.factorio.availability_zone +} + +resource "aws_volume_attachment" "factorio_mount" { + # NVMe devices are always exposed as /dev/nvmen1 + device_name = "/dev/sdf" + volume_id = aws_ebs_volume.factorio_data.id + instance_id = aws_instance.factorio.id +} + resource "null_resource" "provision" { triggers = { - instance_id = aws_instance.factorio.id - instance_ip = aws_instance.factorio.public_ip - private_key = tls_private_key.ssh.private_key_pem + instance_id = aws_instance.factorio.id + instance_ip = aws_instance.factorio.public_ip + private_key = tls_private_key.ssh.private_key_pem + data_volume_attachment = aws_volume_attachment.factorio_mount.volume_id } # Restore save games from S3 and start headless server. @@ -163,6 +177,10 @@ resource "null_resource" "provision" { inline = [ "sudo systemctl stop factorio-headless.service", "sudo systemctl start factorio-backup.service", + # Workaround for volume attachment timeout on destroy: + # - https://github.com/terraform-providers/terraform-provider-aws/issues/1017 + # - https://github.com/terraform-providers/terraform-provider-aws/issues/4770 + "sudo umount /opt || true" ] } diff --git a/instance/terraform.tfvars b/instance/terraform.tfvars index 4ced226..5973530 100644 --- a/instance/terraform.tfvars +++ b/instance/terraform.tfvars @@ -5,6 +5,8 @@ factorio_version = "latest" # Factorio server instance: NVMe instance #instance_type = "c5.large" +# Internal device name specific to NVMe +#ebs_device_path = "/dev/nvme1n1" # S3 bucket for save games. Use bucket name from state module. bucket_name = "factorio-123" diff --git a/instance/variables.tf b/instance/variables.tf index 4a95997..207a64b 100644 --- a/instance/variables.tf +++ b/instance/variables.tf @@ -23,6 +23,12 @@ variable "instance_type" { description = "AWS instance type to use for the Factorio server." } +variable "ebs_device_path" { + type = string + default = "/dev/xvdf" + description = "Internal EBS device name to use for Factorio data." +} + variable "bucket_name" { type = string description = "S3 bucket to use for save game backups."