🎓 A1Learner
🔍
0 of 50 lessons completed

Capstone: A Three-Tier App

A three-tier app is a web server, a storage layer, and the network between them. This capstone builds all three as modules.

The layout

The root calls four modules. Each module owns one tier.

capstone/
  main.tf
  main.tftest.hcl
  modules/
    network/main.tf
    iam/main.tf
    web/main.tf
    storage/main.tf

The network tier

A VPC holds the app. A subnet holds the web server.

resource "aws_vpc" "main" {
  cidr_block = "10.0.0.0/16"
}

resource "aws_subnet" "web" {
  vpc_id     = aws_vpc.main.id
  cidr_block = "10.0.1.0/24"
}

The web tier

One small server runs in the subnet. It carries an IAM role, not a key.

resource "aws_instance" "web" {
  ami                  = "ami-12345678"
  instance_type        = "t2.micro"
  subnet_id            = var.subnet_id
  iam_instance_profile = var.instance_profile
}

The storage tier

A bucket holds the app data. It is a module output, so the root can show its name.

resource "aws_s3_bucket" "app" {
  bucket = "capstone-app-bucket-${terraform.workspace}"
}

Dev and prod workspaces

Each workspace gets its own state in the remote backend. The config stays the same.

terraform workspace new dev
terraform apply -auto-approve
terraform workspace new prod
terraform apply -auto-approve

Test, then destroy

The test runs against a mocked provider. The destroy takes everything down when the practice ends.

terraform test
terraform destroy -auto-approve

Example

# Start the emulator first: moto_server -p 5000
# Create the backend bucket first, outside Terraform:
# aws --endpoint-url http://localhost:5000 s3api create-bucket --bucket capstone-state --region us-east-1

# modules/network/main.tf
resource "aws_vpc" "main" {
  cidr_block = "10.0.0.0/16"
}

resource "aws_subnet" "web" {
  vpc_id     = aws_vpc.main.id
  cidr_block = "10.0.1.0/24"
}

output "vpc_id" {
  value = aws_vpc.main.id
}

output "subnet_id" {
  value = aws_subnet.web.id
}

# modules/iam/main.tf
resource "aws_iam_role" "web" {
  name = "capstone-web-role-${terraform.workspace}"
  assume_role_policy = jsonencode({
    Version = "2012-10-17"
    Statement = [{
      Action    = "sts:AssumeRole"
      Effect    = "Allow"
      Principal = { Service = "ec2.amazonaws.com" }
    }]
  })
}

resource "aws_iam_instance_profile" "web" {
  name = "capstone-web-profile-${terraform.workspace}"
  role = aws_iam_role.web.name
}

output "instance_profile" {
  value = aws_iam_instance_profile.web.name
}

# modules/web/main.tf
variable "subnet_id" {}
variable "instance_profile" {}
variable "vpc_id" {}

resource "aws_security_group" "web" {
  name   = "capstone-web-sg"
  vpc_id = var.vpc_id

  ingress {
    from_port   = 80
    to_port     = 80
    protocol    = "tcp"
    cidr_blocks = ["0.0.0.0/0"]
  }
}

resource "aws_instance" "web" {
  ami                  = "ami-12345678"
  instance_type        = "t2.micro"
  subnet_id            = var.subnet_id
  iam_instance_profile = var.instance_profile
  vpc_security_group_ids = [aws_security_group.web.id]
}

output "instance_id" {
  value = aws_instance.web.id
}

# modules/storage/main.tf
resource "aws_s3_bucket" "app" {
  bucket = "capstone-app-bucket-${terraform.workspace}"
}

output "bucket_name" {
  value = aws_s3_bucket.app.bucket
}

# main.tf (root)
terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 6.0"
    }
  }

  backend "s3" {
    bucket                      = "capstone-state"
    key                         = "capstone/terraform.tfstate"
    region                      = "us-east-1"
    use_path_style = true
    skip_credentials_validation = true
    skip_metadata_api_check     = true
    skip_requesting_account_id  = true
    endpoints = {
      s3 = "http://localhost:5000"
    }
  }
}

provider "aws" {
  region                      = "us-east-1"
  s3_use_path_style                      = true
  skip_credentials_validation = true
  skip_metadata_api_check     = true
  skip_requesting_account_id  = true
  endpoints {
    ec2 = "http://localhost:5000"
    s3  = "http://localhost:5000"
    iam = "http://localhost:5000"
    sts = "http://localhost:5000"
  }
}

module "network" {
  source = "./modules/network"
}

module "iam" {
  source = "./modules/iam"
}

module "web" {
  source           = "./modules/web"
  subnet_id        = module.network.subnet_id
  instance_profile = module.iam.instance_profile
  vpc_id           = module.network.vpc_id
}

module "storage" {
  source = "./modules/storage"
}

output "instance_id" {
  value = module.web.instance_id
}

output "bucket_name" {
  value = module.storage.bucket_name
}

# main.tftest.hcl
mock_provider "aws" {}

run "capstone_outputs_exist" {
  command = plan

  assert {
    condition     = module.storage.bucket_name != ""
    error_message = "The storage module produced no bucket name."
  }
}

# terraform init
# terraform workspace new dev
# terraform apply -auto-approve
# terraform test
# terraform workspace new prod
# terraform apply -auto-approve
# terraform destroy -auto-approve
# terraform workspace select dev
# terraform destroy -auto-approve

Key points

  • The network tier is a VPC with a subnet.
  • The web tier is an EC2 server in that subnet.
  • The storage tier is an S3 bucket.
  • IAM gives the server a role instead of a key.
  • A remote S3 backend holds the state for the whole app.
  • Dev and prod are workspaces on the same config, and a test checks the shape before apply.
  • The whole capstone runs in the emulator on this machine. It creates nothing in a real account.

Four small modules form three tiers: network, web, and storage, with IAM giving the server its role. The state lives in a remote S3 backend, and dev and prod are separate workspaces. The test checks the shape, and a closing destroy takes the three tiers down.

Try it Yourself

Edit the code, then press Run.

Running is simulated · Rust Playground

Exercise

How is the three-tier app organised in this capstone?