From 5e19296e503bf56df468841f3483505204d31454 Mon Sep 17 00:00:00 2001 From: Sergey Gulin Date: Wed, 19 Apr 2023 01:21:05 +0300 Subject: [PATCH] [Chore] Refactor aws instances and hcloud servers Problem: We have added the mkAWS and mkHcloud to terranix-simple, so now we can reduce amount of boilerplate when defining aws instances and hcloud servers. Solution: Use mkAWS and mkHcloud to refactor aws instances and hcloud servers. --- terraform/alhena.nix | 2 +- terraform/castor.nix | 2 +- terraform/common.nix | 39 ++++++++++----------------------------- terraform/jishui.nix | 2 +- terraform/mebsuta.nix | 2 +- terraform/tejat-prior.nix | 2 +- terraform/wasat.nix | 2 +- 7 files changed, 16 insertions(+), 35 deletions(-) diff --git a/terraform/alhena.nix b/terraform/alhena.nix index 62eaa44..3f4717a 100644 --- a/terraform/alhena.nix +++ b/terraform/alhena.nix @@ -1,6 +1,6 @@ { pkgs, lib, ... }: let - inherit (import ./common.nix) mkAWS; + inherit (import ./common.nix pkgs) mkAWS; inherit (pkgs.lib) mkAddressRecords; in { resource.aws_instance.alhena = mkAWS { diff --git a/terraform/castor.nix b/terraform/castor.nix index aec4706..d1d5f67 100644 --- a/terraform/castor.nix +++ b/terraform/castor.nix @@ -1,6 +1,6 @@ { pkgs, lib, ... }: let - inherit (import ./common.nix) mkAWS; + inherit (import ./common.nix pkgs) mkAWS; inherit (pkgs.lib) mkAddressRecords; in { resource.aws_instance.castor = mkAWS { diff --git a/terraform/common.nix b/terraform/common.nix index b70612f..3e34ff6 100644 --- a/terraform/common.nix +++ b/terraform/common.nix @@ -1,35 +1,16 @@ -{ - mkAWS = let - security-group = [ - "\${aws_security_group.egress_all.id}" - "\${aws_security_group.http.id}" - "\${aws_security_group.ssh.id}" - "\${aws_security_group.wireguard.id}" - ]; in {key_name, volume_size, tags, vpc_security_group_ids ? security-group}: { - # Networking - availability_zone = "\${module.vpc.azs[2]}"; - subnet_id = "\${module.vpc.public_subnets[2]}"; - associate_public_ip_address = true; - inherit key_name tags vpc_security_group_ids; +{ pkgs, ... }: +let + inherit (pkgs.lib) mkAWS mkHcloud; +in { + mkAWS = {key_name, volume_size, tags, vpc_security_group_ids ? null}@args: mkAWS ({ + availability_zone = "\${module.vpc.azs[2]}"; + subnet_id = "\${module.vpc.public_subnets[2]}"; + instance_type = "t3a.micro"; + } // args); - # Instance parameters - instance_type = "t3a.micro"; - monitoring = true; - - # Disk type, size, and contents - lifecycle.ignore_changes = [ "ami" ]; - ami = "\${data.aws_ami.nixos.id}"; - root_block_device = { - volume_type = "gp2"; - inherit volume_size; - }; - }; - - mkHcloud = {name, ssh_keys}: { + mkHcloud = {name, ssh_keys}: mkHcloud { inherit name ssh_keys; - image = "ubuntu-20.04"; server_type = "cx11"; - lifecycle.ignore_changes = [ "user_data" ]; # Install NixOS 20.09 user_data = '' #cloud-config diff --git a/terraform/jishui.nix b/terraform/jishui.nix index 21cd672..11b989c 100644 --- a/terraform/jishui.nix +++ b/terraform/jishui.nix @@ -1,6 +1,6 @@ { pkgs, lib, ... }: let - inherit (import ./common.nix) mkAWS; + inherit (import ./common.nix pkgs) mkAWS; inherit (pkgs.lib) mkAddressRecords; in { resource.aws_instance.jishui = mkAWS { diff --git a/terraform/mebsuta.nix b/terraform/mebsuta.nix index d13611c..0467e31 100644 --- a/terraform/mebsuta.nix +++ b/terraform/mebsuta.nix @@ -1,6 +1,6 @@ { pkgs, lib, ... }: let - inherit (import ./common.nix) mkHcloud; + inherit (import ./common.nix pkgs) mkHcloud; inherit (pkgs.lib) mkAddressRecords; in { resource.hcloud_server.mebsuta = mkHcloud { diff --git a/terraform/tejat-prior.nix b/terraform/tejat-prior.nix index 4bcffc1..9e91315 100644 --- a/terraform/tejat-prior.nix +++ b/terraform/tejat-prior.nix @@ -1,6 +1,6 @@ { pkgs, lib, ... }: let - inherit (import ./common.nix) mkAWS; + inherit (import ./common.nix pkgs) mkAWS; inherit (pkgs.lib) mkAddressRecords; in { resource.aws_instance.tejat-prior = mkAWS { diff --git a/terraform/wasat.nix b/terraform/wasat.nix index 84c1ff4..a814372 100644 --- a/terraform/wasat.nix +++ b/terraform/wasat.nix @@ -1,6 +1,6 @@ { pkgs, lib, ... }: let - inherit (import ./common.nix) mkHcloud; + inherit (import ./common.nix pkgs) mkHcloud; inherit (pkgs.lib) mkAddressRecords; in { resource.hcloud_server.wasat = mkHcloud {