Guide to building a production web service on AWS — from ALB to autoscaling
☁️ AWS Complete Guide
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.
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…
Component
Role
Placement
Route 53
DNS — connects the domain to the ALB with an Alias
Managed relational DB with automatic Multi-AZ failover
Private Subnet
S3 + CloudFront
Static asset storage + global CDN delivery
-
Secrets Manager
Secure management and runtime injection of the DB password and API keys
-
CloudWatch
Log collection, metrics, alarms
-
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.
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.
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 443resource "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 Groupresource "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/*"] } }}
Component
Description
Listener
The port/protocol on which the ALB waits for requests (80, 443)
Target Group
The set of targets that receive the actual traffic (ECS Task IPs, EC2, Lambda)
Health Check
Checks periodically whether the targets are healthy and removes unhealthy ones from traffic
Listener Rule
Routes to different Target Groups according to path and host header conditions
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 }}
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.
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.
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"}
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.
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"}
s3:GetObject, secretsmanager:GetSecretValue (called directly from code), 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.
Values that need periodic rotation, such as DB passwords
API keys and settings that rarely change
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.
Health check failures → fewer targets to take traffic
ECS CPUUtilization / MemoryUtilization
Task resource utilization
The basis for scaling decisions and capacity planning
RDS DatabaseConnections / FreeStorageSpace
Number of DB connections, remaining disk space
Prevents downtime from connection pool exhaustion and a full disk
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.
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.
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)
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 point
Question to ask
Practical guideline
Boundaries
Which parts of the AWS code are most likely to change?
Separate input/output, configuration, external integrations, and the core rules.
State
Where is state created and where does it go away?
Make the owner and lifecycle of state visible in the code.
Failure
What 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.
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 axis
How to verify
Definition of done
Correctness
Automate the normal/failure cases.
The core scenarios pass reproducibly.
Regression prevention
When fixing a bug, leave the same case behind as a test.
The same failure is not deployed again.
Operability
Check 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.