Skip to content
OpenTofu module

OpenTofu module

tofu/nat sets up the HA layout as a module:

  • an Auto Scaling group of one, with a launch lifecycle hook: a replacement is in service when natkit says it’s ready, not before;
  • a static ENI and an EIP;
  • the private route tables’ routes;
  • a scoped instance role.
module "nat" {
  source          = "github.com/ulm0/natkit//tofu/nat?ref=v0.1.8"
  vpc_id          = aws_vpc.main.id
  subnet_id       = aws_subnet.public.id
  route_table_ids = [aws_route_table.private.id]
  ami_id          = "ami-..." # natkit-v0.1.8-arm64 (public in us-east-1, owner 208689161537): the same release as the module
  instance_type   = "t4g.micro"
  settings        = { nf_conntrack_max = "262144" }
}

With the community VPC module, see With the VPC module. Once applied, check that it’s up. Upgrading says how a new release rolls out and what each one changes.

Inputs

NameDescriptionTypeDefaultRequired
vpc_idThe VPC.stringn/ayes
subnet_idPublic subnet (internet gateway route) for the instance and its static ENI.stringn/ayes
route_table_idsPrivate route tables that get 0.0.0.0/0 (and 64:ff9b::/96 with nat64) to the static ENI.list(string)n/ayes
ami_idA natkit AMI of the instance type’s architecture, from the same release as the module.stringn/ayes
nameName and natkit tag of every resource; with conntrack_alarm_actions, the Name dimension of the metrics.string"natkit"no
instance_type0.5 GiB of memory is enough (t4g.nano, t3.nano); the CloudWatch agent (cwagent_cfg_param_name) needs 1 GiB, and the module refuses a *.nano with it. See instance size.string"t4g.micro"no
cpu_creditsCredit mode of a burstable (T-family) instance_type: unlimited or standard. Default: EC2’s, the account’s default for the family (unlimited on T4g, T3 and T3a unless changed). standard caps the cost: an instance out of credits is held to its baseline. natkit hasn’t been measured in that mode. See CPU credits.stringnullno
eipAllocate an EIP and have the instance take it. Otherwise it egresses from its own public IP, which changes on replacement.booltrueno
nat64Route 64:ff9b::/96 to the ENI and give it an IPv6 address (the subnet needs an IPv6 CIDR). Pair with Route 53 Resolver DNS64: see NAT64.boolfalseno
ipv6_cidrThe VPC’s IPv6 CIDR, a default client with nat64.stringnullno
client_cidrsSources that may route through the NAT, IPv4 and IPv6, in canonical form. Enforced by the static ENI’s security group (unless untracked) and by natkit’s nftables (client_cidrs). Default: the VPC’s IPv4 CIDRs, plus ipv6_cidr.list(string)nullno
untrackedOpen the static ENI’s security group to all traffic, so EC2 doesn’t track the flows through it: about twice the connections before EC2’s per-instance allowance runs out. client_cidrs is then enforced by natkit’s nftables only. See Performance.boolfalseno
tcp_established_timeoutEC2’s idle timeout, in seconds (60 to 432000), for established TCP connections it tracks on both ENIs. Default: EC2’s (432000, or 350 on Nitro v6 instances). 350 matches NAT Gateway and frees the allowance from abandoned connections sooner. Going back to null leaves the static ENI at its last value: set EC2’s default explicitly instead. See Performance.numbernullno
settingsnatkit’s tuning user-data keys: ip_local_port_range, nf_conntrack_max, nf_conntrack_buckets, nf_conntrack_tcp_timeout_established, tcp_keepalive_time, tcp_max_syn_backlog. Any other key is refused at plan time.map(any){}no
cwagent_cfg_param_nameSSM parameter with a CloudWatch agent JSON config: enables the agent. Needs 1 GiB of memory: a micro or larger.stringnullno
conntrack_alarm_actionsAlarm when EC2 drops packets because the instance’s connection-tracking allowance ran out, and when the NAT stops reporting, with these actions (SNS topic ARNs; [] for an alarm without any). natkit publishes the ENA driver’s allowance counters itself (metrics), without the CloudWatch agent. CloudWatch bills the five metrics, the requests and the alarm: see Costs.list(string)nullno
instance_profileName of an existing instance profile to use instead of the module’s role, where IAM is managed elsewhere. Its role needs the instance role’s permissions.stringnullno
console_password_paramSSM parameter with root’s crypt hash (openssl passwd -6) for the EC2 Serial Console.stringnullno
tagsExtra tags for every resource.map(string){}no

Outputs

NameDescription
eni_idThe static ENI: the private route tables’ target.
public_ipThe EIP, with eip = true.
security_group_idThe static ENI’s: what the clients’ traffic must pass.
public_security_group_idThe instance’s primary (public) ENI’s.
autoscaling_group_nameThe Auto Scaling group of one.
conntrack_alarm_nameThe alarm, with conntrack_alarm_actions.
role_nameThe instance role this module created; null with instance_profile.

Security groups

ENIInboundOutbound
Primary (public)nothing: replies to the NAT’s own connections onlyall
Static (the route tables’ target)client_cidrs, or all with untrackedall

The static ENI has to admit the clients: to EC2, a packet a client sends to the internet through the NAT is inbound traffic on that ENI.

Last updated on