Private AWS architecture as code
Start with a small private application
Imagine an online catalog that must be reachable over HTTPS but must not expose its containers or database directly to the internet. Terraform describes the required AWS objects, links their identifiers and tracks lifecycle ownership. The design below is a fictional IPv4 architecture in two Availability Zones, not an instruction to deploy. No AWS account or provider is needed for the offline work.
Internet client → HTTPS → public ALB (zones A and B) ↓ TCP 8080 private ECS tasks (A and B) ↓ TCP 5432 isolated RDS subnet group (A and B)
Tasks → approved egress path → image registry and logsTasks → S3 gateway endpoint → private bucket (IAM-controlled)Database → no default internet route in this designThe load balancer is the public entry point; the application and database use private addresses. An internet-facing ALB requires appropriately routed public subnets in at least two Availability Zones in the ordinary regional design. The database subnet group spans two zones, but that alone does not enable a Multi-AZ database deployment. Select actual zone names and service availability only in an authorized account.
Plan subnet addresses before creating resources
A VPC is a network boundary. A subnet occupies an address range within the VPC and belongs to one Availability Zone. For this example, reserve a /16 and allocate six separate /24s. The following complete local configuration computes the address plan without an AWS provider. Terraform 1.14.8 is the inherited provisional baseline; expected results are not Terraform execution evidence.
terraform { required_version = "= 1.14.8"}locals { vpc_cidr = "10.40.0.0/16" subnets = { edge_a = cidrsubnet(local.vpc_cidr, 8, 0) edge_b = cidrsubnet(local.vpc_cidr, 8, 1) app_a = cidrsubnet(local.vpc_cidr, 8, 10) app_b = cidrsubnet(local.vpc_cidr, 8, 11) db_a = cidrsubnet(local.vpc_cidr, 8, 20) db_b = cidrsubnet(local.vpc_cidr, 8, 21) }}output "subnet_plan" { value = local.subnets}The output maps edge_a to 10.40.0.0/24, edge_b to 10.40.1.0/24, app_a to 10.40.10.0/24, app_b to 10.40.11.0/24, db_a to 10.40.20.0/24 and db_b to 10.40.21.0/24. Distinct indices make non-overlapping /24s within this /16. Real capacity planning must account for AWS-reserved addresses, load balancer headroom, scaling and other networks.
Separate network routing from address assignment
A public subnet has a route to an internet gateway. In this design, edge subnets have that route; application subnets route approved outbound internet traffic through an appropriate NAT path or use service endpoints; database subnets have no default internet route. A gateway endpoint can provide an S3 path without NAT for that traffic, but authorization still depends on IAM and endpoint/bucket policies. Private addressing is not sufficient access control. The next fragment uses the AWS provider (inherited illustrative 6.55.0 pin); it creates only a VPC and two subnets, not the route tables, associations, gateways or rest of the design. It is intentionally not runnable here.
# Partial AWS provider illustration, not a deployable root module.resource "aws_vpc" "catalog" { cidr_block = "10.40.0.0/16" enable_dns_support = true enable_dns_hostnames = true}variable "zones" { type = object({ a = string, b = string })}resource "aws_subnet" "app" { for_each = { a = 10, b = 11 } vpc_id = aws_vpc.catalog.id availability_zone = var.zones[each.key] cidr_block = cidrsubnet(aws_vpc.catalog.cidr_block, 8, each.value) map_public_ip_on_launch = false}The explicit false value disables default public IPv4 assignment for launches in these subnets; it does not prevent every mechanism that could attach a public address. Review the ECS network setting assign_public_ip, routes, security groups and all explicit associations separately.
Compose small modules with one owner each
A network module should expose network identifiers; an identity module should expose scoped role ARNs; a security module should own rules; an edge module should own the listener and target group; database and application modules should consume identifiers. Pass dependencies through inputs rather than searching by loose names or creating a VPC in every module. The following is an interface sketch with hypothetical local module paths; no implementation is supplied and the code cannot be initialized as written.
# Illustrative module interfaces only; implementation not supplied.module "network" { source = "./modules/network" vpc_cidr = "10.40.0.0/16"}module "identity" { source = "./modules/identity"}module "security" { source = "./modules/security" vpc_id = module.network.vpc_id}module "database" { source = "./modules/database" subnet_ids = module.network.db_subnet_ids security_group_id = module.security.db_sg_id}module "edge" { source = "./modules/edge" vpc_id = module.network.vpc_id subnet_ids = module.network.edge_subnet_ids security_group_id = module.security.edge_sg_id}module "application" { source = "./modules/application" subnet_ids = module.network.app_subnet_ids security_group_id = module.security.app_sg_id task_role_arn = module.identity.task_role_arn execution_role_arn = module.identity.execution_role_arn target_group_arn = module.edge.target_group_arn database_endpoint = module.database.endpoint}References establish dependency edges. They do not prove application readiness: an ECS deployment can still fail health checks even if the target group exists. If ECS needs a listener association before service creation, model that real dependency explicitly in the edge/application contract. A shared externally owned VPC could instead be passed in through an approved lookup, using explicit ID, account and region. Do not have two Terraform states create or manage the same VPC.
Define the permitted traffic and identity boundaries
The ALB listener accepts intended HTTPS clients; the application group accepts TCP 8080 from the ALB group; the database group accepts TCP 5432 from the application group only. Security groups are stateful, but still review outbound permissions, network ACLs, routes and TLS. The following partial fragment illustrates only two ingress rules, not complete security groups or outbound policy. Do not mix separate rule resources with conflicting inline rules.
# Partial illustration: assumes three existing security groups.resource "aws_vpc_security_group_ingress_rule" "app_from_edge" { security_group_id = aws_security_group.app.id referenced_security_group_id = aws_security_group.edge.id ip_protocol = "tcp" from_port = 8080 to_port = 8080}resource "aws_vpc_security_group_ingress_rule" "db_from_app" { security_group_id = aws_security_group.db.id referenced_security_group_id = aws_security_group.app.id ip_protocol = "tcp" from_port = 5432 to_port = 5432}For ECS, the execution role supports platform operations such as approved image pulls and log delivery; the task role authorizes application API calls. Keep the two roles scoped to their intended jobs. Store application data in a private S3 bucket with public-access controls, encryption and scoped permissions. Use a reviewed secrets mechanism for database authentication; never put credentials in the module outputs or example plans. Configure log retention, health alarms and application-level monitoring. A private database requires backups and restore testing as well as network controls.
Test design intent without claiming cloud success
In a future approved test environment, the Terraform native mocking framework can substitute provider behavior to test configuration decisions. This staged test assumes the network fragment and a reviewed provider schema; it does not make an AWS request when correctly mocked, but it has not been executed.
# Staged test for the network fragment only, not executed.mock_provider "aws" {}run "private_application_subnets" { command = plan variables { zones = { a = "eu-west-1a", b = "eu-west-1b" } } assert { condition = length(aws_subnet.app) == 2 && alltrue([ for subnet in aws_subnet.app : !subnet.map_public_ip_on_launch ]) error_message = "Expected two application subnets without default public IPv4 assignment." }}The assertion checks two configured values only. It cannot prove the routes, DNS, access policy, public IP absence on actual tasks, availability or cost. The offline exercise instead verifies CIDR ranges and reviews a synthetic reachability matrix. Before any real deployment, test permitted and denied connections and workload health in an authorized disposable environment.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Private AWS architecture as code — knowledge check
Review private AWS architecture, cost and change safety.
Which module should own the VPC in the proposed design?
Which route is characteristic of the public edge subnets in this design?
Which inbound database rule matches the intended topology?
What does a two-zone RDS subnet group establish by itself?
What does the staged mock assertion verify?