Skip to main content
AIDevOps
  • Learn
  • Learning Paths
  • Practice
  • Open Source
  • Books
  • Engineering

    AI DevOpsThe full map of building and operating AI servicesLLMOpsLLM deployment · evaluation · observabilityHands-on ProjectsBuild AI Agent projects

    Knowledge

    DocsTechnical documentationBlogEngineering articlesPloggerDevelopment log feed

    Validate

    Certification3-level skills certification · coming soon
AI Models
LlamaTutorial|MistralTutorial|GemmaTutorial|DeepSeekTutorial|QwenTutorial
☁️ Cloud
Cloud Intro & RoadmapAWSGCPAzureNCPCloudflare
🤖 AI Applied Development
Applied AI Intro & RoadmapHugging FaceLangChainLlamaIndexLLMOps|LangGraphMCPMulti-AgentAgent Evaluation
🧠 AI Core
AI Intro & RoadmapML FundamentalsLLM Fundamentals|Python AIC++|PyTorchTensorFlowJAX
🧠 AI Agent Development
Finance AI AgentLLM API ServerStock Investing AgentAIOps AI AgentEducation AI AgentCoding AI Agent
🌱 Spring Cloud
Spring Intro & RoadmapSpring Cloud GatewaySpring BootJava|Spring AISpring SecuritySpring BatchSpring JPA
🐳 DevOps
DevOps Intro & RoadmapLinuxDockerCI/CD|Kubernetes BasicsK8s AdvancedPrometheusGrafana
🧱 Infrastructure
Infrastructure Intro & RoadmapNginxRedis
🎨 Frontend
Frontend Intro & RoadmapJavaScriptTypeScript|ReactNext.js|VueNuxt|Electron
📱 Mobile
Mobile Intro & RoadmapKotlinAndroidFlutter
⚙️ Backend
Backend Intro & RoadmapPython BasicsFastAPIDjangoFlask|CGoGinNode.js
💾 Database
DB Intro & RoadmapCore SQLOracleMySQLPostgreSQL|MongoDBVector DB
🧪 Testing
k6JMeternGrinder
AIDevOps

Engineering AI. From Code to Production.
An engineering learning platform for building and operating AI and AI Agents

Learn

  • All Guides
  • Learning Paths
  • AI Tutorial
  • Practice
  • Books

Resources

  • AI DevOps
  • LLMOps
  • Hands-on Projects
  • Docs
  • Blog
  • Plogger
  • Open Source
  • Certification (coming soon)

Start Here

  • AI Core Roadmap
  • AI Applied Development Roadmap
  • Spring Cloud Roadmap
  • DevOps Roadmap
  • Infrastructure Roadmap

 

  • Cloud Roadmap
  • Frontend Roadmap
  • Mobile Roadmap
  • Backend Roadmap
  • Database Roadmap
© 2026 AIDevOps. All rights reserved.
Terms of ServicePrivacy PolicySitemaptestforge.kr
  1. Home
  2. Learn
  3. Cloud
  4. AWS
Guide to building a production web service on AWS — from ALB to autoscaling

☁️ AWS Complete Guide

Visitors

Build a real web service on AWS from start to finish. Multi-AZ VPC networking, ALB load balancing and HTTPS termination, connecting a domain with ACM and Route 53, ECS Fargate autoscaling, RDS, S3+CloudFront, IAM and Secrets security, CloudWatch monitoring, CI/CD, and structuring a Terraform project — a production-grade architecture laid out in order.

  • Intermediate
  • Updated Oct 1, 2026
  • ~16 min read
  • 17 sections
  • 18 code examples

Learning Paths that include this guide

This guide is one step in the paths below. Study it together with the steps before and after.

  • Cloud Native Engineer →
  • Production AI Engineer →
ALB load balancing & HTTPS terminationECS Fargate autoscalingRDS and Secrets security designUnified IaC with Terraform

Contents

0 / 20
  1. How to use this guide
  2. Architecture diagrams
  3. The whole architecture at a glance
  4. VPC network design (Multi-AZ)
  5. Setting up the ALB
  6. ACM certificates & Route 53 domains
  7. ECS Fargate deployment & ALB integration
  8. Autoscaling
  9. Setting up the RDS database
  10. S3 static assets & CloudFront CDN
  11. IAM roles & security policies
  12. Secrets Manager & environment variables
  13. CloudWatch monitoring & alarms
  14. CI/CD (ECR + GitHub Actions)
  15. Structuring the Terraform project
  16. Production checklist
  17. AWS design
  18. Operating standards
  19. Verification strategy
  20. Frequently asked questions
Contents 20 sections
  1. How to use this guide
  2. Architecture diagrams
  3. The whole architecture at a glance
  4. VPC network design (Multi-AZ)
  5. Setting up the ALB
  6. ACM certificates & Route 53 domains
  7. ECS Fargate deployment & ALB integration
  8. Autoscaling
  9. Setting up the RDS database
  10. S3 static assets & CloudFront CDN
  11. IAM roles & security policies
  12. Secrets Manager & environment variables
  13. CloudWatch monitoring & alarms
  14. CI/CD (ECR + GitHub Actions)
  15. Structuring the Terraform project
  16. Production checklist
  17. AWS design
  18. Operating standards
  19. Verification strategy
  20. Frequently asked questions

How to use this guide

How to read it

Understanding AWS as a real-world workflow

Build a real web service on AWS from start to finish. Multi-AZ VPC networking, ALB load balancing and HTTPS termination, connecting a domain with ACM and Route 53, ECS Fargate autoscaling, RDS, S3+CloudFront, IAM and Secrets security, CloudWatch monitoring, CI/CD, and structuring a Terraform project — a production-grade architecture laid out in order. Rather than listing concepts, this guide is organized so you can follow the content in the order you have to make decisions on a real project.

Key perspective

Infrastructure / operations

Rather than memorizing install commands, understand the order in which traffic, runtime, observability, and incident response connect.

ALB load balancing & HTTPS terminationECS Fargate autoscalingRDS and Secrets security designUnified IaC with Terraform

Architecture diagrams

To make what you read stick, it helps to capture the flow as a picture first. The two diagrams below are reference maps you can keep coming back to while learning AWS.

Learning flow

Rendering diagram…

Architecture view

Rendering diagram…

The whole architecture at a glance

When you first open AWS, the big picture matters more than individual commands. This section starts with the problems the concepts ahead were created to solve.

A real web service is not finished just because one container is running. Only when you can connect a domain, terminate HTTPS, spread traffic across several instances, isolate the database safely, and detect failures can you call it "production". This guide builds the architecture below one component at a time, in the order you would actually create it.
Rendering diagram…
ComponentRolePlacement
Route 53DNS — connects the domain to the ALB with an Alias-
ACMFree TLS certificates with automatic renewal-
ALBL7 load balancing, HTTPS termination, path/host-based routingPublic Subnet (Multi-AZ)
ECS FargateServerless container executionPrivate Subnet (Multi-AZ)
RDSManaged relational DB with automatic Multi-AZ failoverPrivate Subnet
S3 + CloudFrontStatic asset storage + global CDN delivery-
Secrets ManagerSecure management and runtime injection of the DB password and API keys-
CloudWatchLog collection, metrics, alarms-

Tip

This order (network → ALB → domain/certificate → compute → data → security → monitoring → deployment automation) is also the order in which dependencies do not get tangled when you build infrastructure from scratch — resources in later sections reference resources created in earlier ones.

VPC network design (Multi-AZ)

Here you look at VPC network design (Multi-AZ) alongside real code. Rather than copying the example as-is, read it with an eye on the inputs, the outputs, and the parts most likely to change.

A production service must be spread across at least 2 Availability Zones (AZs), because the remaining AZ has to be able to keep taking traffic when one AZ fails. Place the ALB and NAT Gateway in public subnets, and the ECS Tasks and RDS in private subnets that cannot be reached directly from the internet.
network.tfHCL
resource "aws_vpc" "main" {
  cidr_block           = "10.0.0.0/16"
  enable_dns_hostnames = true
  tags = { Name = "prod-vpc" }
}

resource "aws_internet_gateway" "main" {
  vpc_id = aws_vpc.main.id
}

resource "aws_subnet" "public" {
  for_each = { a = { cidr = "10.0.1.0/24", az = "us-east-1a" },
               b = { cidr = "10.0.2.0/24", az = "us-east-1c" } }
  vpc_id                  = aws_vpc.main.id
  cidr_block              = each.value.cidr
  availability_zone       = each.value.az
  map_public_ip_on_launch = true
  tags = { Name = "public-${each.key}" }
}

resource "aws_subnet" "private" {
  for_each = { a = { cidr = "10.0.11.0/24", az = "us-east-1a" },
               b = { cidr = "10.0.12.0/24", az = "us-east-1c" } }
  vpc_id            = aws_vpc.main.id
  cidr_block        = each.value.cidr
  availability_zone = each.value.az
  tags = { Name = "private-${each.key}" }
}

resource "aws_eip" "nat" {
  domain = "vpc"
}

resource "aws_nat_gateway" "main" {
  allocation_id = aws_eip.nat.id
  subnet_id     = aws_subnet.public["a"].id
}

resource "aws_route_table" "public" {
  vpc_id = aws_vpc.main.id
  route {
    cidr_block = "0.0.0.0/0"
    gateway_id = aws_internet_gateway.main.id
  }
}

resource "aws_route_table" "private" {
  vpc_id = aws_vpc.main.id
  route {
    cidr_block     = "0.0.0.0/0"
    nat_gateway_id = aws_nat_gateway.main.id
  }
}
SubnetExample CIDRResources placedOutbound route
Public Subnet A (AZ-a)10.0.1.0/24ALB, NAT GatewayInternet Gateway
Public Subnet B (AZ-b)10.0.2.0/24ALBInternet Gateway
Private Subnet A (AZ-a)10.0.11.0/24ECS Task, RDS(Primary)NAT Gateway
Private Subnet B (AZ-b)10.0.12.0/24ECS Task, RDS(Standby)NAT Gateway

Tip

  • A Security Group is stateful, so once inbound is allowed the response traffic is allowed automatically. A NACL, by contrast, is stateless, so inbound and outbound rules have to be stated separately — a SG alone is enough for most services, and NACLs should be used only when an extra line of defense is needed at the subnet level.
  • Creating a separate NAT Gateway per AZ raises availability but raises the bill just as much. To save cost you can keep one NAT Gateway shared by both private subnets, as in the example above, but the trade-off is that outbound internet access is cut everywhere if that AZ goes down.

Setting up the ALB

Here you look at Setting up the ALB alongside real code. Rather than copying the example as-is, read it with an eye on the inputs, the outputs, and the parts most likely to change.

An ALB (Application Load Balancer) works at OSI layer 7 (the application layer) and distributes traffic by looking at the path and host header of HTTP/HTTPS requests. It must be created across at least 2 public subnets in different AZs.
alb.tfHCL
resource "aws_security_group" "alb" {
  name   = "alb-sg"
  vpc_id = aws_vpc.main.id

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

resource "aws_lb" "main" {
  name               = "my-app-alb"
  internal           = false
  load_balancer_type = "application"
  security_groups    = [aws_security_group.alb.id]
  subnets            = [for s in aws_subnet.public : s.id]
}

resource "aws_lb_target_group" "app" {
  name        = "my-app-tg"
  port        = 8080
  protocol    = "HTTP"
  vpc_id      = aws_vpc.main.id
  target_type = "ip"   # must be ip for the Fargate awsvpc network mode

  health_check {
    path                = "/health"
    healthy_threshold   = 2
    unhealthy_threshold = 3
    interval            = 30
    timeout             = 5
    matcher             = "200"
  }
}
listeners.tfHCL
resource "aws_lb_listener" "https" {
  load_balancer_arn = aws_lb.main.arn
  port              = 443
  protocol          = "HTTPS"
  ssl_policy        = "ELBSecurityPolicy-TLS13-1-2-2021-06"
  certificate_arn   = aws_acm_certificate.cert.arn

  default_action {
    type             = "forward"
    target_group_arn = aws_lb_target_group.app.arn
  }
}

# Port 80 does nothing but redirect to 443
resource "aws_lb_listener" "http_redirect" {
  load_balancer_arn = aws_lb.main.arn
  port              = 80
  protocol          = "HTTP"

  default_action {
    type = "redirect"
    redirect {
      port        = "443"
      protocol    = "HTTPS"
      status_code = "HTTP_301"
    }
  }
}

# Path-based routing example: /api/* goes to a separate API Target Group
resource "aws_lb_listener_rule" "api" {
  listener_arn = aws_lb_listener.https.arn
  priority     = 10

  action {
    type             = "forward"
    target_group_arn = aws_lb_target_group.api.arn
  }
  condition {
    path_pattern { values = ["/api/*"] }
  }
}
ComponentDescription
ListenerThe port/protocol on which the ALB waits for requests (80, 443)
Target GroupThe set of targets that receive the actual traffic (ECS Task IPs, EC2, Lambda)
Health CheckChecks periodically whether the targets are healthy and removes unhealthy ones from traffic
Listener RuleRoutes to different Target Groups according to path and host header conditions

Tip

  • Chain the security groups so the ECS Task SG allows traffic to the container port only from the ALB SG. This blocks direct access that bypasses the ALB at the root, and the "ALB → Task" traffic flow is clear from the security group rules alone.
  • The health check path (/health) must return 200 with no authentication. If it gets caught by authentication middleware and returns 401/403, the whole Target Group is judged Unhealthy, which easily leads to an incident where the entire service looks dead.
  • The ALB has cross-zone load balancing enabled by default, so even when Tasks are concentrated in one AZ, traffic is spread evenly across the targets in every AZ.

ACM certificates & Route 53 domains

Here you look at ACM certificates & Route 53 domains alongside real code. Rather than copying the example as-is, read it with an eye on the inputs, the outputs, and the parts most likely to change.

Get a free TLS certificate from ACM (AWS Certificate Manager), attach it to the HTTPS listener created above, and connect the domain directly to the ALB with a Route 53 Alias record. Unlike a CNAME, an Alias record can also be used on the root domain (example.com), and lookups are free of charge.
acm-route53.tfHCL
resource "aws_acm_certificate" "cert" {
  domain_name               = "example.com"
  subject_alternative_names = ["*.example.com"]
  validation_method         = "DNS"

  lifecycle { create_before_destroy = true }
}

resource "aws_route53_record" "cert_validation" {
  for_each = {
    for dvo in aws_acm_certificate.cert.domain_validation_options : dvo.domain_name => {
      name  = dvo.resource_record_name
      type  = dvo.resource_record_type
      value = dvo.resource_record_value
    }
  }
  zone_id = aws_route53_zone.main.zone_id
  name    = each.value.name
  type    = each.value.type
  records = [each.value.value]
  ttl     = 60
}

resource "aws_acm_certificate_validation" "cert" {
  certificate_arn         = aws_acm_certificate.cert.arn
  validation_record_fqdns = [for r in aws_route53_record.cert_validation : r.fqdn]
}

resource "aws_route53_record" "alb_alias" {
  zone_id = aws_route53_zone.main.zone_id
  name    = "example.com"
  type    = "A"

  alias {
    name                   = aws_lb.main.dns_name
    zone_id                = aws_lb.main.zone_id
    evaluate_target_health = true
  }
}

Tip

  • A certificate used on the ALB only has to be in the same region as the ALB, but a certificate for CloudFront must be issued in the us-east-1 (N. Virginia) region — mixing up the region and being unable to attach the certificate to CloudFront is a common problem.
  • With DNS validation, the certificate renews automatically for as long as the validation record remains. Email validation needs manual approval before expiry, so use DNS validation unless you have a specific reason not to.

ECS Fargate deployment & ALB integration

Here you look at ECS Fargate deployment & ALB integration alongside real code. Rather than copying the example as-is, read it with an eye on the inputs, the outputs, and the parts most likely to change.

Specify the container image, CPU/memory, and log settings in the Task Definition, and connect to the Target Group created earlier with the load_balancer block of the ECS Service. The roles a container uses are split in two (Execution Role and Task Role); the difference is covered in detail in the IAM section later.
task-definition.jsonJSON
{
  "family": "my-app-task",
  "networkMode": "awsvpc",
  "requiresCompatibilities": ["FARGATE"],
  "cpu": "512",
  "memory": "1024",
  "executionRoleArn": "arn:aws:iam::123456789012:role/ecsTaskExecutionRole",
  "taskRoleArn": "arn:aws:iam::123456789012:role/ecsTaskRole",
  "containerDefinitions": [
    {
      "name": "app",
      "image": "123456789012.dkr.ecr.us-east-1.amazonaws.com/my-app:latest",
      "portMappings": [{ "containerPort": 8080 }],
      "logConfiguration": {
        "logDriver": "awslogs",
        "options": {
          "awslogs-group": "/ecs/my-app",
          "awslogs-region": "us-east-1",
          "awslogs-stream-prefix": "ecs"
        }
      }
    }
  ]
}
ecs-service.tfHCL
resource "aws_ecs_cluster" "main" {
  name = "my-app-cluster"
}

resource "aws_security_group" "ecs_tasks" {
  name   = "ecs-tasks-sg"
  vpc_id = aws_vpc.main.id

  ingress {
    from_port       = 8080
    to_port         = 8080
    protocol        = "tcp"
    security_groups = [aws_security_group.alb.id]   # allowed from the ALB only
  }
  egress {
    from_port   = 0
    to_port     = 0
    protocol    = "-1"
    cidr_blocks = ["0.0.0.0/0"]
  }
}

resource "aws_ecs_service" "app" {
  name            = "my-app-service"
  cluster         = aws_ecs_cluster.main.id
  task_definition = aws_ecs_task_definition.app.arn
  desired_count   = 2
  launch_type     = "FARGATE"

  network_configuration {
    subnets          = [for s in aws_subnet.private : s.id]
    security_groups  = [aws_security_group.ecs_tasks.id]
    assign_public_ip = false
  }

  load_balancer {
    target_group_arn = aws_lb_target_group.app.arn
    container_name   = "app"
    container_port   = 8080
  }

  deployment_circuit_breaker {
    enable   = true
    rollback = true
  }

  depends_on = [aws_lb_listener.https]
}

Tip

With deployment_circuit_breaker on, ECS rolls back to the previous version automatically when a new deployment keeps failing as Unhealthy. Always keep desired_count at 2 or more to guarantee zero downtime during deployments as well.

Autoscaling

Here you look at Autoscaling alongside real code. Rather than copying the example as-is, read it with an eye on the inputs, the outputs, and the parts most likely to change.

Application Auto Scaling raises or lowers the running count (desired count) of the ECS service automatically with traffic. Choose between CPU utilization and ALB request count as the metric, whichever fits the nature of the service.
autoscaling.tfHCL
resource "aws_appautoscaling_target" "ecs" {
  max_capacity       = 10
  min_capacity       = 2
  resource_id        = "service/${aws_ecs_cluster.main.name}/${aws_ecs_service.app.name}"
  scalable_dimension = "ecs:service:DesiredCount"
  service_namespace  = "ecs"
}

resource "aws_appautoscaling_policy" "request_count" {
  name               = "alb-request-count-tracking"
  policy_type        = "TargetTrackingScaling"
  resource_id        = aws_appautoscaling_target.ecs.resource_id
  scalable_dimension = aws_appautoscaling_target.ecs.scalable_dimension
  service_namespace  = aws_appautoscaling_target.ecs.service_namespace

  target_tracking_scaling_policy_configuration {
    predefined_metric_specification {
      predefined_metric_type = "ALBRequestCountPerTarget"
      resource_label = "${aws_lb.main.arn_suffix}/${aws_lb_target_group.app.arn_suffix}"
    }
    target_value       = 1000
    scale_in_cooldown  = 300
    scale_out_cooldown = 60
  }
}

Tip

  • ALBRequestCountPerTarget (request-count based) reacts to traffic bursts faster than the CPU-based metric — it suits API servers that wait on I/O a lot and use little CPU.
  • Keep the scale-out cooldown short (e.g. 60 seconds) and the scale-in cooldown long (e.g. 300 seconds). The other way round causes flapping, where a brief traffic spike is followed straight away by a scale-in.

Setting up the RDS database

Here you look at Setting up the RDS database alongside real code. Rather than copying the example as-is, read it with an eye on the inputs, the outputs, and the parts most likely to change.

Always place RDS only in private subnets, and use a security group to allow access only from the ECS Task security group. With Multi-AZ enabled, it fails over automatically to the Standby when the Primary fails (taking 60-120 seconds on average).
rds.tfHCL
resource "aws_db_subnet_group" "main" {
  name       = "main-db-subnet-group"
  subnet_ids = [for s in aws_subnet.private : s.id]
}

resource "aws_security_group" "rds" {
  name   = "rds-sg"
  vpc_id = aws_vpc.main.id

  ingress {
    from_port       = 5432
    to_port         = 5432
    protocol        = "tcp"
    security_groups = [aws_security_group.ecs_tasks.id]   # access from the ECS Tasks only
  }
}

resource "aws_db_instance" "main" {
  identifier                   = "my-app-db"
  engine                       = "postgres"
  engine_version               = "16.4"
  instance_class               = "db.t4g.medium"
  allocated_storage            = 20
  storage_encrypted            = true
  multi_az                     = true
  db_subnet_group_name         = aws_db_subnet_group.main.name
  vpc_security_group_ids       = [aws_security_group.rds.id]
  username                     = "appuser"
  manage_master_user_password  = true   # the password is generated and stored in Secrets Manager automatically
  backup_retention_period      = 7
  deletion_protection          = true
  skip_final_snapshot          = false
  final_snapshot_identifier    = "my-app-db-final-snapshot"
}

Tip

  • With manage_master_user_password = true, RDS generates the master password automatically and stores it in Secrets Manager — there is no longer any need to write the password in application code or Terraform variables.
  • In production, always set deletion_protection = true and skip_final_snapshot = false. Leaving out these two values really does lead, quite often, to an accidental terraform destroy deleting the production DB without a trace.

S3 static assets & CloudFront CDN

Here you look at S3 static assets & CloudFront CDN alongside real code. Rather than copying the example as-is, read it with an eye on the inputs, the outputs, and the parts most likely to change.

Static assets such as images, JS, and CSS are far faster and cheaper to store in S3 and deliver with caching worldwide through CloudFront than to serve directly from the ECS containers.
s3-cloudfront.tfHCL
resource "aws_s3_bucket" "static" {
  bucket = "my-app-static-assets"
}

resource "aws_s3_bucket_public_access_block" "static" {
  bucket                  = aws_s3_bucket.static.id
  block_public_acls       = true
  block_public_policy     = true
  ignore_public_acls      = true
  restrict_public_buckets = true
}

resource "aws_cloudfront_origin_access_control" "oac" {
  name                              = "my-app-oac"
  origin_access_control_origin_type = "s3"
  signing_behavior                  = "always"
  signing_protocol                  = "sigv4"
}

resource "aws_cloudfront_distribution" "cdn" {
  enabled             = true
  default_root_object = "index.html"

  origin {
    domain_name              = aws_s3_bucket.static.bucket_regional_domain_name
    origin_id                = "s3-static"
    origin_access_control_id = aws_cloudfront_origin_access_control.oac.id
  }

  default_cache_behavior {
    target_origin_id       = "s3-static"
    viewer_protocol_policy = "redirect-to-https"
    allowed_methods        = ["GET", "HEAD"]
    cached_methods         = ["GET", "HEAD"]
    forwarded_values {
      query_string = false
      cookies { forward = "none" }
    }
  }

  restrictions {
    geo_restriction { restriction_type = "none" }
  }

  viewer_certificate {
    cloudfront_default_certificate = true
  }
}

Tip

  • The current recommended approach is to block public access to the S3 bucket completely and let CloudFront in only through OAC (Origin Access Control) — the OAI (Origin Access Identity) used in the past is no longer recommended.
  • Changes to a CloudFront distribution can take about 5-15 minutes to propagate to edges worldwide. Wait a little before panicking that something you just deployed has not taken effect.

IAM roles & security policies

Here you look at IAM roles & security policies alongside real code. Rather than copying the example as-is, read it with an eye on the inputs, the outputs, and the parts most likely to change.

In ECS it is easy to confuse the Execution Role and the Task Role. The Execution Role is the role with which the ECS agent pulls images from ECR and sends logs to CloudWatch, and the Task Role is the role the application code running inside the container uses when calling other AWS services such as S3 and Secrets Manager.
iam-execution-role.tfHCL
resource "aws_iam_role" "ecs_execution" {
  name = "ecsTaskExecutionRole"
  assume_role_policy = jsonencode({
    Version = "2012-10-17"
    Statement = [{
      Effect    = "Allow"
      Principal = { Service = "ecs-tasks.amazonaws.com" }
      Action    = "sts:AssumeRole"
    }]
  })
}

resource "aws_iam_role_policy_attachment" "ecs_execution" {
  role       = aws_iam_role.ecs_execution.name
  policy_arn = "arn:aws:iam::aws:policy/service-role/AmazonECSTaskExecutionRolePolicy"
}
iam-task-role-policy.jsonJSON
{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Action": ["s3:GetObject"],
      "Resource": "arn:aws:s3:::my-app-static-assets/*"
    }
  ]
}
RoleUsed byTypical permissions
Execution RoleThe ECS agent (infrastructure level)ecr:GetDownloadUrlForLayer, logs:CreateLogStream, secretsmanager:GetSecretValue (for injection)
Task RoleApplication code inside the containers3:GetObject, secretsmanager:GetSecretValue (called directly from code), and so on

Tip

  • Never put an Access Key directly in code or an environment variable file. With a Task Role the container gets temporary credentials automatically, and the credentials are rotated automatically.
  • A policy that leaves Resource open as "*" violates the principle of least privilege. Always narrow the scope to specific ARNs (buckets, tables, and so on).

Secrets Manager & environment variable management

Here you look at Secrets Manager & environment variable management alongside real code. Rather than copying the example as-is, read it with an eye on the inputs, the outputs, and the parts most likely to change.

Do not hardcode sensitive information such as the DB password and external API keys in the image or an environment variable file; store it in Secrets Manager (or Parameter Store SecureString if rotation is not needed) and inject it at runtime through the secrets field of the Task Definition.
secrets.tfHCL
resource "aws_secretsmanager_secret" "app_secrets" {
  name = "my-app/prod/secrets"
}

resource "aws_secretsmanager_secret_version" "app_secrets" {
  secret_id = aws_secretsmanager_secret.app_secrets.id
  secret_string = jsonencode({
    OPENAI_API_KEY = "sk-..."
  })
}
task-definition-secrets.jsonJSON
{
  "containerDefinitions": [{
    "name": "app",
    "environment": [
      { "name": "NODE_ENV", "value": "production" }
    ],
    "secrets": [
      {
        "name": "OPENAI_API_KEY",
        "valueFrom": "arn:aws:secretsmanager:us-east-1:123456789012:secret:my-app/prod/secrets:OPENAI_API_KEY::"
      }
    ]
  }]
}
ComparisonSecrets ManagerParameter Store (SecureString)
CostBilled monthly per secretThe Standard tier is free
Automatic rotationSupported through Lambda integrationNot supported (manual updates)
Suitable forValues that need periodic rotation, such as DB passwordsAPI keys and settings that rarely change

Tip

Put non-sensitive values (NODE_ENV, region, and so on) in the environment field and sensitive values in the secrets field, separately — the ECS agent injects the secrets field only at runtime with the Execution Role permissions, and the actual values never appear in the task definition JSON itself.

CloudWatch monitoring & alarms

Here you look at CloudWatch monitoring & alarms alongside real code. Rather than copying the example as-is, read it with an eye on the inputs, the outputs, and the parts most likely to change.

Collect logs in CloudWatch and set things up so anomalies notify you immediately through alarms.
monitoring.tfHCL
resource "aws_cloudwatch_log_group" "app" {
  name              = "/ecs/my-app"
  retention_in_days = 30
}

resource "aws_sns_topic" "alerts" {
  name = "prod-alerts"
}

resource "aws_sns_topic_subscription" "email" {
  topic_arn = aws_sns_topic.alerts.arn
  protocol  = "email"
  endpoint  = "oncall@example.com"
}

resource "aws_cloudwatch_metric_alarm" "alb_5xx" {
  alarm_name          = "alb-target-5xx-spike"
  comparison_operator = "GreaterThanThreshold"
  evaluation_periods   = 2
  metric_name          = "HTTPCode_Target_5XX_Count"
  namespace            = "AWS/ApplicationELB"
  period               = 60
  statistic            = "Sum"
  threshold            = 10
  alarm_actions        = [aws_sns_topic.alerts.arn]
  dimensions = {
    LoadBalancer = aws_lb.main.arn_suffix
  }
}
MetricMeaningWhy it matters
ALB HTTPCode_Target_5XX_CountThe number of times the backend returned 5xxThe most direct signal of an application failure
ALB UnHealthyHostCountThe number of targets judged UnhealthyHealth check failures → fewer targets to take traffic
ECS CPUUtilization / MemoryUtilizationTask resource utilizationThe basis for scaling decisions and capacity planning
RDS DatabaseConnections / FreeStorageSpaceNumber of DB connections, remaining disk spacePrevents downtime from connection pool exhaustion and a full disk

Tip

  • Without retention_in_days on a log group, logs are kept forever and the cost keeps piling up — limiting operational logs to around 30-90 days is recommended.
  • At the very least, make sure these two alarms — "ALB 5xx surge" and "UnHealthyHostCount occurs" — notify you immediately through SNS; they are the signals that detect most failures first.

CI/CD (ECR + GitHub Actions)

Here you look at CI/CD (ECR + GitHub Actions) alongside real code. Rather than copying the example as-is, read it with an eye on the inputs, the outputs, and the parts most likely to change.

Automate the flow of pushing the container image to ECR and updating the ECS service to the new image with GitHub Actions. Pinning the image tag to the commit SHA means you can always trace exactly which build ECS is running, and a step that waits for the deployment to stabilize after the service update is needed to detect failed deployments automatically.
.github/workflows/deploy.ymlYAML
name: Deploy to ECS

on:
  push:
    branches: [main]

permissions:
  id-token: write   # issue temporary credentials through OIDC
  contents: read

jobs:
  deploy:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - name: Configure AWS credentials (OIDC)
        uses: aws-actions/configure-aws-credentials@v4
        with:
          role-to-assume: arn:aws:iam::123456789012:role/github-actions-deployer
          aws-region: us-east-1

      - name: Login to Amazon ECR
        id: ecr
        uses: aws-actions/amazon-ecr-login@v2

      - name: Build, tag and push image
        env:
          REGISTRY: ${{ steps.ecr.outputs.registry }}
          REPOSITORY: my-app
          IMAGE_TAG: ${{ github.sha }}
        run: |
          docker build -t $REGISTRY/$REPOSITORY:$IMAGE_TAG .
          docker push $REGISTRY/$REPOSITORY:$IMAGE_TAG

      - name: Force new ECS deployment
        run: |
          aws ecs update-service \
            --cluster my-app-cluster \
            --service my-app-service \
            --force-new-deployment

Tip

  • Instead of storing a long-lived Access Key in GitHub Secrets, set things up so GitHub Actions gets temporary credentials through OIDC (OpenID Connect), and the risk of key exposure or leakage disappears altogether — use the role-to-assume method of aws-actions/configure-aws-credentials.
  • Using the commit SHA (github.sha) instead of latest as the image tag makes it clear "which commit is live in production right now", and you can roll back to the previous tag at once when something goes wrong.

Structuring the Terraform project

Here you look at Structuring the Terraform project alongside real code. Rather than copying the example as-is, read it with an eye on the inputs, the outputs, and the parts most likely to change.

When a real team operates the resources covered so far (VPC, ALB, ECS, RDS, ...), they are not all crammed into one file; split them into modules and manage the remote state with S3 + a DynamoDB lock, so several people can work safely at the same time.
Directory structureTEXT
infra/
├── modules/
│   ├── network/       # VPC, subnets, NAT, route tables
│   ├── alb/           # ALB, target groups, listeners, ACM
│   ├── ecs-service/   # ECS service + autoscaling
│   └── rds/           # RDS instance
├── envs/
│   ├── staging/
│   │   ├── main.tf    # module calls + per-environment variable values
│   │   └── backend.tf
│   └── production/
│       ├── main.tf
│       └── backend.tf
envs/production/backend.tfHCL
terraform {
  backend "s3" {
    bucket         = "my-app-terraform-state"
    key            = "production/terraform.tfstate"
    region         = "us-east-1"
    dynamodb_table = "terraform-locks"
    encrypt        = true
  }
}

Tip

  • The state file can contain sensitive information such as the DB password in plain text — always turn on server-side encryption (SSE) and block public access for the S3 bucket that stores the state.
  • Separate the state files and backend keys per environment, such as staging/production, to prevent the incident of an apply run by mistake in staging landing in production.
  • Enforcing "only plan runs on PRs, reviewed by a person; apply only after merging to the main branch" as a CI pipeline rule filters out unexpected changes in advance.

Production checklist

Here you look at Production checklist alongside real code. Rather than copying the example as-is, read it with an eye on the inputs, the outputs, and the parts most likely to change.

✅
Final checks before deployment

• Security groups: expose only the ALB to 0.0.0.0/0, and allow access to ECS and RDS only through internal security group references
• WAF: attach AWS WAF to the ALB and apply the baseline SQL injection, XSS, and rate limiting rules
• ALB access logs: enable storage in S3 so post-incident analysis is possible after a failure or attack
• Multi-AZ: spread the ALB, ECS, and RDS across at least 2 AZs
• Backups & deletion protection: enable RDS automatic backups, deletion_protection = true
• Cost alarms: detect unexpected charges early with AWS Budgets
• Tagging strategy: apply Environment/Service/Owner tags consistently to every resource (essential for cost analysis and automation scripts)

Related guides: Docker guide · Kubernetes guide · Cloudflare guide
waf.tfHCL
resource "aws_wafv2_web_acl" "main" {
  name  = "alb-waf"
  scope = "REGIONAL"

  default_action { allow {} }

  rule {
    name     = "aws-managed-common"
    priority = 1
    override_action { none {} }
    statement {
      managed_rule_group_statement {
        name        = "AWSManagedRulesCommonRuleSet"
        vendor_name = "AWS"
      }
    }
    visibility_config {
      cloudwatch_metrics_enabled = true
      metric_name                = "aws-managed-common"
      sampled_requests_enabled   = true
    }
  }

  visibility_config {
    cloudwatch_metrics_enabled = true
    metric_name                = "alb-waf"
    sampled_requests_enabled   = true
  }
}

resource "aws_wafv2_web_acl_association" "alb" {
  resource_arn = aws_lb.main.arn
  web_acl_arn  = aws_wafv2_web_acl.main.arn
}

AWS practical design

AWS practical design is a point where the options diverge. Using the table to compare what each approach is for and how it differs in operation makes later decisions much easier.

AWS design rests on cost optimization, high availability based on Multi-AZ, IAM least-privilege policies, and VPC network isolation.
Decision pointQuestion to askPractical guideline
BoundariesWhich parts of the AWS code are most likely to change?Separate input/output, configuration, external integrations, and the core rules.
StateWhere is state created and where does it go away?Make the owner and lifecycle of state visible in the code.
FailureWhat does the caller receive on failure?Decide the timeout, fallback, and error contract first.

AWS operating standards

This section covers AWS operating standards from a practical perspective. Instead of memorizing the concept, focus on the situations in which you would reach for it.

Keep up CloudWatch metric observation, EC2 Auto Scaling, IAM credential rotation, and monitoring of resource usage through Cost Explorer.

Tip

  • Multi-AZ VPC
  • IAM Role usage (No AccessKeys)
  • CloudWatch Alarms
  • Terraform State Management

AWS verification strategy

AWS verification strategy is a point where the options diverge. Using the table to compare what each approach is for and how it differs in operation makes later decisions much easier.

Run Terraform plan pre-verification, automatic detection of AWS Config policy violations, IAM Policy Simulator checks, and regression tests against Security Group misconfiguration.
Quality axisHow to verifyDefinition of done
CorrectnessAutomate the normal/failure cases.The core scenarios pass reproducibly.
Regression preventionWhen fixing a bug, leave the same case behind as a test.The same failure is not deployed again.
OperabilityCheck the logs, metrics, and alerts.There is a path for tracing the cause when a problem occurs.

AWS: frequently asked questions

What security settings are essential when starting with AWS?

Enable MFA on the root account and do not use it for everyday work. Create separate users or roles for work with only the permissions they need, and keep access keys out of code and repositories. A budget alert also helps you notice quickly when a leaked key starts generating charges.

When do I use EC2, ECS, EKS, and Lambda?

EC2 means running virtual servers yourself, ECS and EKS are services for running containers, with EKS using Kubernetes, and Lambda is serverless, running a function only when a request arrives. EKS fits if you already use Kubernetes, ECS if you want to run containers simply, and Lambda for short, intermittent work.

What is the difference between an IAM user and an IAM role?

A user is a fixed identity with long-term credentials, and a role is an identity you assume when needed to receive short-lived temporary credentials. For applications running on servers or in containers, the rule is to attach a role rather than embed access keys.

← Previous guideCloud Intro & RoadmapNext guide →GCP