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
| Name | Description | Type | Default | Required |
|---|---|---|---|---|
| vpc_id | The VPC. | string | n/a | yes |
| subnet_id | Public subnet (internet gateway route) for the instance and its static ENI. | string | n/a | yes |
| route_table_ids | Private route tables that get 0.0.0.0/0 (and 64:ff9b::/96 with nat64) to the static ENI. | list(string) | n/a | yes |
| ami_id | A natkit AMI of the instance type’s architecture, from the same release as the module. | string | n/a | yes |
| name | Name and natkit tag of every resource; with conntrack_alarm_actions, the Name dimension of the metrics. | string | "natkit" | no |
| instance_type | 0.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_credits | Credit 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. | string | null | no |
| eip | Allocate an EIP and have the instance take it. Otherwise it egresses from its own public IP, which changes on replacement. | bool | true | no |
| nat64 | Route 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. | bool | false | no |
| ipv6_cidr | The VPC’s IPv6 CIDR, a default client with nat64. | string | null | no |
| client_cidrs | Sources 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) | null | no |
| untracked | Open 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. | bool | false | no |
| tcp_established_timeout | EC2’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. | number | null | no |
| settings | natkit’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_name | SSM parameter with a CloudWatch agent JSON config: enables the agent. Needs 1 GiB of memory: a micro or larger. | string | null | no |
| conntrack_alarm_actions | Alarm 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) | null | no |
| instance_profile | Name 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. | string | null | no |
| console_password_param | SSM parameter with root’s crypt hash (openssl passwd -6) for the EC2 Serial Console. | string | null | no |
| tags | Extra tags for every resource. | map(string) | {} | no |
Outputs
| Name | Description |
|---|---|
| eni_id | The static ENI: the private route tables’ target. |
| public_ip | The EIP, with eip = true. |
| security_group_id | The static ENI’s: what the clients’ traffic must pass. |
| public_security_group_id | The instance’s primary (public) ENI’s. |
| autoscaling_group_name | The Auto Scaling group of one. |
| conntrack_alarm_name | The alarm, with conntrack_alarm_actions. |
| role_name | The instance role this module created; null with instance_profile. |
Security groups
| ENI | Inbound | Outbound |
|---|---|---|
| Primary (public) | nothing: replies to the NAT’s own connections only | all |
| Static (the route tables’ target) | client_cidrs, or all with untracked | all |
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