Skip to content

About

AWS Network (VPC) with OpenTofu

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

AWS Network (VPC)

A VPC with public and private subnets across up to three availability zones, an internet gateway, route tables and an S3 gateway endpoint. A NAT gateway is optional and off by default.

Network only: no compute, no database. It is the plain building block for workloads that you deploy afterwards. OpenTofu demo template. Standalone, no dependency on other repos. Compatible with Terraform.

Architecture

flowchart TB
  internet([Internet]) <--> igw[Internet gateway]
  subgraph vpc[VPC 10.0.0.0/16]
    subgraph pub[Public subnets, one per AZ]
      nat[NAT gateway, optional]
    end
    subgraph priv[Private subnets, one per AZ]
      workload[Your workloads]
    end
  end
  igw <--> pub
  priv -->|0.0.0.0/0, only if NAT is on| nat
  priv -->|gateway endpoint| s3[(S3)]
  vpc -->|flow logs| cw[(CloudWatch Logs)]
Loading
  • VPC with az_count public and az_count private subnets (default 2 each, /24), public subnets first
  • Internet gateway and one public route table with a default route to it
  • One route table per private subnet. Without the NAT gateway the private subnets have no route to the internet
  • NAT gateway (optional): one shared gateway, or one per AZ with nat_gateway_per_az
  • S3 gateway endpoint on the private route tables (no hourly cost)
  • VPC flow logs (all traffic) to CloudWatch Logs
  • Default security group without any rules, default network ACL unchanged

Security

  • Subnets do not assign public IPs automatically
  • The default security group has no rules, so it allows no traffic
  • Private subnets have no route to the internet unless the NAT gateway is on
  • Flow logs on by default, retention configurable
  • vpc_cidr must be a private range (RFC 1918) between /16 and /24, and the module checks that the subnets fit

Accepted checkov findings (documented inline in the code):

Check Reason
CKV_AWS_338, CKV_AWS_158 Log retention is configurable, AWS-managed encryption for logs
CKV_AWS_394 Only the first az_count zones are used

The Trivy scan (trivy config) accepts findings with #trivy:ignore comments above the resource. The reason is written in the same line.

Usage

Configure AWS credentials, for example with aws configure sso or environment variables.

cp terraform.tfvars.example terraform.tfvars
tofu init
tofu plan
tofu apply

Using the network

Place workloads in private_subnet_ids. Use public_subnet_ids for resources that must be reachable from the internet, such as load balancers. Public subnets only have a route to the internet gateway. A public IP address still has to be assigned explicitly. Turn on enable_nat_gateway when private workloads need outbound internet access.

Inputs

Name Description Default
name_prefix Prefix for all resource names "demo"
region AWS region "eu-central-1"
advanced_settings Object with the settings below, all optional {}

advanced_settings attributes (shown as "Advanced Settings" in the StackGuardian form):

Name Description Default
vpc_cidr CIDR block of the VPC. Private range (RFC 1918), prefix /16 to /24 "10.0.0.0/16"
az_count Number of availability zones, 1 to 3. Each gets a public and a private subnet 2
subnet_newbits Bits added to the VPC prefix for each subnet, 1 to 12. 8 gives /24 subnets in a /16. The subnets must fit: 2 x az_count blocks, and the subnet prefix may not be longer than /28 8
enable_nat_gateway NAT gateway for the private subnets false
nat_gateway_per_az One NAT gateway per AZ instead of one shared. Needs enable_nat_gateway false
enable_flow_logs VPC flow logs to CloudWatch Logs true
flow_log_retention_days Retention of the flow logs in CloudWatch 90
tags Additional tags for all resources {}

Outputs

Name Description
vpc_id VPC ID
vpc_cidr CIDR block of the VPC
public_subnet_ids IDs of the public subnets
private_subnet_ids IDs of the private subnets
public_subnet_cidrs CIDR blocks of the public subnets
private_subnet_cidrs CIDR blocks of the private subnets
availability_zones Availability zones of the subnets, in subnet order
internet_gateway_id ID of the internet gateway
nat_gateway_ids IDs of the NAT gateways, empty when off
private_route_table_ids IDs of the private route tables

Cost

The VPC, subnets, route tables, internet gateway and the S3 gateway endpoint have no hourly cost. Each NAT gateway (and its Elastic IP) is billed per hour while it exists, plus data processing, even when idle, so it is off by default. With nat_gateway_per_az you pay for one per AZ. Flow logs are billed by CloudWatch Logs for ingestion and storage.

Destroy after the demo:

tofu destroy

Tests

The GitLab pipeline (.gitlab-ci.yml) lints every push. Merge requests into development or main also run validation, the checkov and Trivy scans and the unit tests. A tag runs all of these plus the integration tests, which deploy real resources and destroy them again, and then creates a release. Integration tests run only when the CI/CD variable AWS_ROLE_ARN is set: an IAM role trusted for GitLab OIDC with the audience sts.amazonaws.com. AWS_REGION is optional.

In GitLab, the unit-tests job also publishes its results as a JUnit test report, which you find in the Tests tab of the pipeline and of the merge request. The script ci/tofu-json-to-junit.py converts the output of tofu test -json to JUnit XML.

The GitHub Actions workflow (.github/workflows/ci.yml) runs the same jobs: every push, pull requests into development or main, and tags. Set AWS_ROLE_ARN (and optionally AWS_REGION) as repository variables; the IAM role then needs a trust for GitHub OIDC (token.actions.githubusercontent.com).

tofu test -test-directory=tests/unit

Test cases: tests/README.md

Development

pre-commit install
pre-commit run --all-files

About

AWS Network (VPC) with OpenTofu

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Used by

Contributors

Languages