This guide demonstrates two approaches to deploy a Node.js todo application on AWS ECS Fargate: using AWS CLI and Terraform. Both methods will help you containerize and deploy your application in a serverless infrastructure.
Prerequisites
AWS Account with configured credentials
Docker installed
Node.js development environment
Terraform installed (for IaC approach)
GitHub account
Method 1: Using AWS CLI
Step 1: Getting the Application
First, clone the Node.js todo application from GitHub:
The repository contains a simple todo application with the necessary Docker configuration files and AWS task definitions.
Step 2: Building the Docker Image
Navigate to the project directory and build the Docker image using the provided Dockerfile:
The build process successfully completed, creating our application image using Node.js 12.2.0-alpine as the base image.
Step 3: Setting up Amazon ECR
Create a new repository in Amazon ECR to store our Docker images:
After creating the repository, configure AWS CLI authentication:
Step 4: Creating the ECS Cluster
Set up an ECS cluster to manage our containers:
The cluster is created with the following specifications:
Cluster name: todo-app-cluster
Networking mode: awsvpc
Launch type: FARGATE
Step 5: Configuring Security and IAM Roles
Create necessary IAM roles and security groups:
Trust policy configuration for ECS tasks:
Task definition configuration showing networking and permissions:
Step 6: Deploying the Application
Register the task definition with ECS:
Create and configure the ECS service:
Monitor the deployment status:
Step 7: Application Verification
After successful deployment, the application is accessible via the assigned public IP:
Service Configuration Details
The service is configured with:
CPU: 256 units
Memory: 512MB
Port mapping: 8000:8000
Network mode: awsvpc
Platform version: LATEST
Launch type: FARGATE
Monitoring and Management
You can monitor the service status through ECS console:
Method 2: Deploying with Terraform (Infrastructure as Code)
Step 1: Create Docker Image
# Build the Docker image
docker build -t todo-app .
Output will show successful build steps and final image creation.
Step 2: Create ECR Repository
Go to AWS Console → Amazon ECR
Click "Create repository"
Configure settings:
Repository name: todo-app
Tag mutability: Mutable
Encryption: AES-256
Click "Create repository"
Step 3: Push Image to ECR
# Tag the image with ECR repository URL
docker tag todo-app:latest [account-id].dkr.ecr.us-east-1.amazonaws.com/todo-app:latest
# Login to ECR
aws ecr get-login-password --region us-east-1 | docker login --username AWS --password-stdin [account-id].dkr.ecr.us-east-1.amazonaws.com
# Push image to ECR
docker push [account-id].dkr.ecr.us-east-1.amazonaws.com/todo-app:latest
Step 4: Setup Terraform Configuration
# Create terraform directory
mkdir terraform
cd terraform
# Create main.tf file
touch main.tf
Step 5: Add Infrastructure Configuration
Create main.tf
with the following content:
# ECS Cluster
resource "aws_ecs_cluster" "main" {
name = "todo-app-cluster"
setting {
name = "containerInsights"
value = "disabled"
}
}
# ECS Service
resource "aws_ecs_service" "app" {
name = "todo-app-service"
cluster = aws_ecs_cluster.main.id
launch_type = "FARGATE"
scheduling_strategy = "REPLICA"
desired_count = 1
deployment_maximum_percent = 200
deployment_minimum_healthy_percent = 100
network_configuration {
assign_public_ip = true
security_groups = [aws_security_group.ecs_tasks.id]
subnets = aws_subnet.public[*].id
}
task_definition = aws_ecs_task_definition.app.arn
}
# Task Definition
resource "aws_ecs_task_definition" "app" {
family = "todo-app"
requires_compatibilities = ["FARGATE"]
network_mode = "awsvpc"
cpu = 256
memory = 512
container_definitions = jsonencode([
{
name = "todo-app"
image = "${aws_ecr_repository.app.repository_url}:latest"
portMappings = [
{
containerPort = 8000
hostPort = 8000
protocol = "tcp"
}
]
}
])
}
Step 6: Deploy Infrastructure
# Initialize Terraform
terraform init
# Preview changes
terraform plan
# Apply configuration
terraform apply
Enter "yes" when prompted to confirm deployment.
Step 7: Verify Deployment
Check ECR Repository:
Navigate to Amazon ECR
Verify "todo-app" repository exists
Confirm image has been pushed successfully
Verify ECS Cluster:
Go to ECS console
Check "todo-app-cluster" is active
Confirm "todo-app-service" is present
Confirm Service Status:
Check service details
Verify 1/1 tasks are running
Monitor service events
Access Application:
Get public IP from task details
Open browser and navigate to:
http://<public-ip>:8000
Verify todo application is accessible and functional
Common Issues and Troubleshooting
Method 1 (AWS CLI) Issues:
ECR Authentication Failures
Token expiration (login valid for 12 hours only)
Incorrect region or account ID
Solution: Re-authenticate using
aws ecr get-login-password
Task Definition Registration
Invalid JSON format
Missing required parameters
Solution: Validate JSON and check AWS ECS documentation
Service Deployment
Insufficient permissions
Network configuration issues
Solution: Check IAM roles and VPC settings
Method 2 (Terraform) Issues:
Terraform State Issues
State lock problems
Corrupted state file
Solution: Use remote state storage (S3) with proper locking
Resource Dependencies
Resources created in wrong order
Missing dependencies
Solution: Use
depends_on
and check resource references
Configuration Syntax
HCL syntax errors
Invalid resource attributes
Solution: Use
terraform fmt
and validate configurations
General Issues for Both Methods:
Network Connectivity
Security group misconfiguration
VPC subnet issues
Solution: Verify security group rules and subnet configurations
Container Health
Application crashes
Memory/CPU constraints
Solution: Check container logs and adjust resource allocations
Image Pull Failures
Repository permissions
Image tag issues
Solution: Verify ECR permissions and image tags
Conclusion
Both deployment methods offer distinct advantages:
AWS CLI Method
Quick deployment for simple scenarios
Direct control over resources
Easier for beginners to understand
Good for one-time deployments
Terraform Method
Infrastructure as Code benefits
Version control of infrastructure
Reproducible deployments
Better for team collaboration
Key Takeaways
Infrastructure Management
Both methods effectively deploy ECS workloads
Terraform provides better infrastructure governance
AWS CLI offers simpler direct management
Development Workflow
Container-based deployment streamlines process
Infrastructure as Code improves repeatability
Automation reduces human error
Best Practices
Use proper version control
Implement monitoring and logging
Follow security best practices
Regular backups and disaster recovery planning
Resource Optimization
Right-size containers and tasks
Implement auto-scaling when needed
Monitor and optimize costs
Security Considerations
Proper IAM role configuration
Network security with security groups
Encryption for sensitive data
Maintenance
Regular updates and patches
Monitoring and alerting
Documentation and knowledge sharing