Deploying to AWS with Terraform and Nix

Tags
TerraformNix
Updated at
Jan 5, 2022 1:46 PM
Published at
May 23, 2020

Let's say that you want to deploy this NixOS configuration onto AWS:

configuration.nix

{ ... }:
{
  # Put your NixOS configuration here. Eg:
  services.nginx.enable = true;
}

The first thing to do is to create another NixOS configuration that includes the amazon-image config and your main config. This is what ultimately is going to end-up on the VM:

aws-deploy.nix

{ modulesPath, ... }:
{
  imports = [
    "${modulesPath}/virtualisation/amazon-image.nix"
    # path to your config
    ./configuration.nix
  ];
}

TODO: Setup CI here and Eval NixOS to pre-fill the cache.

With that in hand, we can now write a bit of Terraform code:

Downsides

  • No auto-scaling: only a single VM gets configured
  • No auto-healing: if the VM goes down, it takes another terraform apply to re-deploy the system configuration.

Upsides

  • Simple setup.
  • Direct feedback on deployment.
  • It's easy to migrate this auto-scaling in the future.

TODO

  • Use CI + Cachix to pre-build the NixOS machine.
  • Write a terraform aws_image_nixos_custom module for auto-scaling scenarios.
  • Secret management → use SSM
  • Better SSH key management?