aws-access.md 16 KB

AWS stages 0 and 1: foundation and access

Status: written, tested with mocked providers, never applied. No AWS account, no credential and no Spacelift stack exists for this code. Do not create the Spacelift stacks cah-aws-00-foundation or cah-aws-10-access until the maintainer's check of Spacelift's native AWS integration on the current plan has passed (design, section 10, decision 6). If that check fails, return to the maintainer before any fallback to a static key. .spacelift/config.yml has no AWS entry for the same reason.

This is the AWS twin of stage 0 and stage 1. The shape is the same: a foundation layer, then an access layer that builds two Twingate connectors with no public inbound. The objects underneath differ; the provider pages explain how.

1. What this builds

Stack stacks/aws/00-foundation (cost: nothing):

  • One VPC with one public subnet, an internet gateway and an explicit route table (modules/aws/vpc), default range 10.78.0.0/24. The VPC's default security group is emptied.
  • A baseline security group with no inbound rule and the five default outbound ports (modules/aws/firewall). No stage 1 machine wears it (an instance carries exactly one group); it is the deny-all starting point for later stages.
  • The AWS provider is configured with a region variable (default us-east-1, the region the price table was recorded for) and a Project default tag. It names no account id, profile, key or role. Credentials come from the environment at run time.
  • Outputs: vpc_id, vpc_ip_range, subnet_id, baseline_security_group_id, region.

Stack stacks/aws/10-access (cost: $20.84 a month):

  • One Twingate remote network access-<region>, the group cah-<region>-admins, and one restricted SSH resource per connector (TCP 22 only, UDP denied, no ICMP), the same as the DigitalOcean stack.
  • Two t4g.micro instances, access-<region>-connector-a and -b, each running a Twingate connector (the shared modules/twingate-bootstrap). Each has a public IPv4 address, an 8 GiB encrypted gp3 root volume, standard CPU credits, IMDSv2 required, no key pair and no instance profile.
  • A connector security group (the only group attached to either instance) with no inbound rule from any address. It has one inbound rule whose source is the group itself (TCP 22), mirrored by one outbound rule to the group itself, so each connector can reach the other's SSH as Twingate may route a session through either one (see section 2c of stage 1). Outbound to any address: tcp 443, 80, 53; udp 53, 123; tcp 30000-31000 and udp 1-65535 for Twingate peer-to-peer.
  • The connectors reboot at 04:00 and 05:00 Eastern, so they never reboot together.
  • Outputs: connector_private_ips (keys a and b), connector_security_group_id.

Values pass between the stacks the way they do on DigitalOcean: Spacelift stack dependencies mapped to TF_VAR_* variables, no remote state.

Opt-in metrics (issue #99, AWS half; all off by default, the plan is a no-op). node_exporter and node_exporter_instances install a node exporter bound to each connector's private address; metrics_source_security_group_id (an sg-... id you supply, empty by default) admits the enabled metrics ports (the exporter's, and connector_telemetry.metrics_port with its previous value during a rollout) from that one security group only, as inbound_from_other_groups of the firewall module: never a CIDR, an IPv6 range or a prefix list. Turning the exporter on changes user_data, so it REPLACES the instance (and its public address, there is no Elastic IP) of every connector in node_exporter_instances: roll out ["a"], check SSH through Twingate, then ["a", "b"]. Setting only the source group (or only the telemetry port) changes the security group and replaces no instance. The group must exist in the same VPC and is not created here. It must be a different group from the connector group: naming the connector group itself is refused (a precondition in the firewall module and on the stack output), because it would admit connector-to-connector metrics, which this change does not offer. Changing the source group is a different rule key, so the plan removes the old rule and adds the new one in the same apply and AWS may briefly hold both (or, if the add runs first, a duplicate-rule conflict can fail the apply). Do it in two applies: first set metrics_source_security_group_id = "" and apply (the old rules go), then set the new group and apply (the new rules arrive). Metrics are unscrapable between the two applies; that is expected. This is written and mock-tested only: nothing is applied, no Spacelift stack exists for these AWS stacks, and Spacelift's native AWS integration is still unchecked, so do not assume it works. Details and limits: stage 2, section 2.3. Log shipping is not offered on AWS yet.

What the tests prove (tofu test, mocked providers, no account needed): names and wiring; instance type and root size read from cost.json; two connectors with distinct reboot times; the connector group has exactly the member-only SSH rule and no inbound from an address anywhere; the baseline has zero inbound; with the metrics inputs on, the exact ingress list (member SSH plus the enabled metrics ports from the one source group); the golden user_data hashes at the default; outputs; and that no stack file names an account id, profile, key or role. What they cannot prove is in section 7.

2. Prerequisites

  1. The throwaway AWS account from AWS account preparation: root protected, a billing alert set, nothing else in the account.
  2. The Spacelift AWS integration check passed (the warning at the top). Until then there is no way to run this stack without a long-lived key, which this project does not want.
  3. The Spacelift role needs ssm:GetParameter on Canonical's public path /aws/service/canonical/ubuntu/server/24.04/stable/current/arm64/hvm/ebs-gp3/ami-id, plus the EC2 permissions to create the objects above. Scope it to the one region.
  4. A separate Doppler setup for AWS, per spacelift-secrets.md section 8a: do not reuse stage 1's prd config. That config holds DIGITALOCEAN_TOKEN, and its service token can read the whole config whatever names the hook asks for, so an AWS run holding it could read the DigitalOcean credential. Create the AWS-only config (8a proposes project cah, config dev_aws) holding TWINGATE_API_TOKEN, TWINGATE_NETWORK, ADMIN_SSH_PUBLIC_KEY and ADMIN_USER_IDS (ADMIN_SSH_KEY_NAME is not used on AWS: there is no key pair), its own read-only service token, and its own Spacelift context (8a proposes cah-doppler-aws). Label the AWS stacks with a cloud label such as cah-aws, so the existing cah-doppler context does not auto-attach to them (a stack must never receive two tokens). Attach the push policy to the AWS stacks before any secret context (the policy autoattaches on the cah label, which the AWS stacks no longer carry): attach it manually or extend its autoattach labels. No AWS secret goes in Doppler if the native integration is available.
  5. Confirm 10.78.0.0/24 overlaps no homelab range, or set vpc_ip_range.
  6. Record the check date of Twingate's outbound allowlist article here, as stage 1 does: not yet checked for this stack.

3. Cost

Phase id aws-twin, planned, proposed ceiling $30. Planned $20.84 a month (two t4g.micro at $0.0084 an hour, two public IPv4 addresses at $0.005 an hour, two 8 GiB gp3 volumes at $0.08 per GiB-month), against an approved $21.00 for AWS. The current phase's ceiling (stage-1, $30) applies, and DigitalOcean stage 1 ($12) plus AWS ($21) would be $33, so apply this only while the DigitalOcean access stage is torn down. Data transfer and anything created outside OpenTofu are not priced; a billing alert only notifies. See cost and the AWS cost lesson.

The cost switch is two flags, not one. providers.digitalocean.active is true today and providers.aws.active is false. Turning only AWS on fails the gate. Order of operations:

  1. Destroy DigitalOcean stage 1 and confirm it is gone.
  2. In docs/stages/approved.json set providers.digitalocean.active to false and providers.aws.active to true, in one reviewed change.
  3. Apply AWS. To return to DigitalOcean: destroy AWS, confirm it is gone, then reverse both flags in one change.

The gate for each state, run on copies of the repository with only the flags changed:

digitalocean.active aws.active Result
true false (today) passes: aws inactive, digitalocean $12.00 active
false true (the AWS stage) passes: aws $20.84 against $21.00 active, digitalocean inactive
true true fails: the approved figures of the active providers add up to $33.00, above the shared ceiling $30.00 (the message keeps the wording "shared ceiling"; the ceiling is the current phase's)
false false passes (nothing is attested as applied)

python3 -I scripts/cost_check.py --root . reports aws: planned $20.84/mo, approved $21.00, inactive today. Once a trusted run produces a real plan, run the gate with --plan plan.json --stack stacks/aws/10-access before the first apply; it prices what the plan resolves, which source code can hide.

4. Bootstrap (when the check has passed)

Order on every instance: update, baseline, harden, then applications. The AWS connectors use the same cloud-init template as the DigitalOcean ones (modules/twingate-bootstrap), so the order is the one in the stage 1 guide and the first-hour lesson: update and automatic security updates first, a baseline you take by hand, the hardening, and the connector last. Amazon Linux behaves differently from Ubuntu at launch (see the lesson); this stack uses the Ubuntu image path through the shared template.

  1. Create cah-aws-00-foundation rooted at stacks/aws/00-foundation, labeled cah-aws (not cah), with the push policy attached first, then the AWS integration and the cah-doppler-aws context, and TF_VAR_region set.
  2. Create cah-aws-10-access rooted at stacks/aws/10-access, labeled cah-aws, push policy first, then the same AWS integration and the cah-doppler-aws context attached to this stack too (a stack dependency passes the mapped outputs, not credentials, so the access stack gets none from the foundation stack). It depends on the foundation stack, with this mapping:

| Foundation output | Environment variable | |---|---| | vpc_id | TF_VAR_vpc_id | | subnet_id | TF_VAR_subnet_id | | region | TF_VAR_region |

The Doppler hook must export TF_VAR_twingate_network, TF_VAR_admin_ssh_public_key and TF_VAR_admin_user_ids for this stack, as it does for the DigitalOcean one.

  1. Check that no Twingate site named access-<region> exists in the console (a hand-made one makes the apply fail or leaves a duplicate).
  2. Add the two stacks to .spacelift/config.yml and the CI trigger paths in that same change, then run tracked. Autodeploy stays off; read the plan, then confirm.

5. Verify

  1. Both connectors show online in the Twingate console, under access-<region>.
  2. From a Twingate client: ssh cah-admin@<private ip> works for each address in connector_private_ips.
  3. From outside Twingate, nmap -Pn -p 22,80,443 <connector public ip> shows the ports filtered (scanning your own instance is authorized).
  4. The phase 4 acceptance checks in section 7, recorded with their command output.
  5. python3 -I scripts/cost_check.py --root . still reports the planned AWS figure, and the AWS bill for the first day matches two instances, two public addresses and two 8 GiB volumes.

6. Rollback and teardown

Destroy cah-aws-10-access first, then cah-aws-00-foundation; the VPC is in use by the access stack until then. The connector tokens die with the instances and the Twingate site goes with the stack. Afterwards confirm in the console that no instance, volume or public address remains (an address left allocated keeps billing), then, if DigitalOcean stage 1 is returning, reverse both flags in approved.json in one change: providers.aws.active to false and providers.digitalocean.active to true. To rotate a token or the admin key, replace one connector at a time as in stage 1 section 3; a changed cloud-init replaces the instance (user_data_replace_on_change) and probably changes its private address.

If a connector is offline, use the other one for SSH, then sudo systemctl status twingate-connector and sudo cat /var/log/cah-connector-install.log, as in stage 1 section 6a. There is no AWS key pair and no console password path by design, so a total loss of both connectors is recovered by replacing them in a run.

7. Phase 4 acceptance checklist (the first real apply must record all of these)

Mocked tests show the shape of the code, not AWS's behavior. Keep the command output with the apply notes. Everything below is UNVERIFIED until then.

From the vpc module:

  • The VPC's default security group has no rules (describe-security-group-rules filtered to it).
  • The subnet's route table is the explicit one, with 0.0.0.0/0 to the internet gateway; the subnet does not use the main table.
  • The default network ACL is still allow-all, as documented.

From the firewall module:

  • aws ec2 describe-security-group-rules for the connector group: the seven outbound rules to 0.0.0.0/0, the member rules (TCP 22 in and out, source and destination the group itself), no every-protocol (-1) egress rule, no inbound rule from 0.0.0.0/0 or ::/0; when metrics_source_security_group_id is set, one more inbound rule per enabled metrics port whose source is that group and nothing else. The baseline group: five outbound rules, no inbound. Repeat after any manual console change (drift in a rule added outside the code is not detected).
  • The egress rules exist before the instances boot (the declared ordering is UNVERIFIED).

From the twingate-connector module:

  • Both network interfaces carry exactly the one connector group, not the default group or any other.
  • aws ec2 describe-instances: HttpTokens=required, hop limit 1, no IAM instance profile, no key name, CpuCredits=standard.
  • The root volumes are encrypted gp3, 8 GiB, 3000 IOPS, 125 MiB/s.
  • The Canonical SSM parameter resolves to an available Ubuntu 24.04 arm64 image in the region and zone, matching t4g.micro.
  • Both public IPv4 addresses are assigned and the route works (the connector reaches Twingate).
  • cloud-init status --wait (through Twingate) completed and the connector is healthy in the console.
  • The bill model matches the manifest: two instances, two public IPv4 addresses, two 8 GiB gp3 roots, standard credits, nothing else.

From these stacks:

  • A peer-to-peer session through connector a to connector b's SSH resource works (the member rules do their job), and the reverse.
  • Twingate's outbound allowlist check date recorded (section 2, item 6).
  • Cross-cloud read denial (manual, from spacelift-secrets.md section 8a): from an AWS stack run, ask for DIGITALOCEAN_TOKEN through the AWS context and confirm the hook fails with could not read DIGITALOCEAN_TOKEN from Doppler. Also confirm the cah-doppler context is not attached to either AWS stack, and the push policy is attached to both.
  • tofu plan shows no Project tag drift on any resource (default tags interacting with module resources is untested against AWS).