Amazon Elastic Container Service (ECS) is a powerful tool for running, managing, and scaling containerized applications. This guide provides a step-by-step walkthrough of setting up an ECS cluster, creating task definitions, and deploying services with a load balancer for seamless traffic distribution. By the end, you’ll have a working containerized application accessible online.

NAT Gateway Configuration: Setting up Internet Connectivity for Private Subnet Instances

A NAT Gateway is essential to allow instances in a private subnet to connect to the internet. Here’s how to set it up:

  1. Create a NAT Gateway: Navigate to the VPC console, select “NAT Gateways,” and create a new NAT Gateway in a public subnet. Attach an Elastic IP to ensure internet access.
  2. Update Route Tables: Add a route to the private subnet’s route table, directing internet-bound traffic (0.0.0.0/0) to the NAT Gateway.

This configuration ensures secure internet access for instances without exposing them directly.

Creating an ECS Cluster: Step-by-Step Guide to Creating an EC2 Instance-Based Cluster

An ECS cluster is a logical grouping of EC2 instances that act as your compute resources for running containers.

  1. Launch the ECS Cluster:
    • Open the ECS console and select “Clusters.”
    • Choose the EC2 Linux + Networking template and provide cluster details.
    • Configure the networking of the EC2 instances and attach the appropriate IAM role for ECS tasks.
  2. Connect to the Cluster: After creation, the EC2 instances in the cluster will automatically register with ECS.

Creating a Load Balancer (ALB): How to Set Up an ALB for Traffic Distribution

A load balancer ensures seamless traffic distribution to your application.

  1. Set Up the ALB:
    • Navigate to the EC2 console and create an Application Load Balancer.
    • Choose internet-facing as the type and select the appropriate subnets.
    • Configure a target group for the ALB and link it to the ECS service.
  2. Assign Security Groups: Ensure the ALB has a security group allowing HTTP/HTTPS traffic.

Creating a Task Definition: Container Image and Network Configuration

Task definitions are the blueprint for your ECS tasks.

  1. Create the Task Definition:
    • Navigate to the ECS console and create a new task definition.
    • Choose the EC2 launch type and specify the container image, memory, and CPU requirements.
  2. Network Configuration: Set up port mappings to expose container ports to the ALB.

Creating an ECS Service: Creating a Service Based on Task Definitions and Connecting to a Load Balancer

Services allow you to run and maintain a specified number of tasks based on your task definition.

  1. Create a Service:
    • In the ECS console, create a service and choose your cluster and task definition.
    • Enable the load balancer and link it to the previously created ALB.
  2. Scaling Configuration: Define the desired and maximum number of tasks for your service.

Accessing and Verifying Docker on EC2 Instances

Access your ECS instances to verify Docker and containers.

  1. Connect via Session Manager: Use AWS Systems Manager Session Manager to access the EC2 instance without SSH keys.
  2. Verify Docker:
    • Run docker ps to check running containers.
    • Inspect logs and container statuses for troubleshooting.

Accessing and Modifying the Webpage

Access the application and verify content modification.

  1. Access via ALB DNS: Use the DNS name of your ALB to open the webpage in your browser.
  2. Modify Content:
    • Update container files or deploy a new version of the task definition.
    • Verify changes by refreshing the webpage.

Conclusion

Following these steps, you’ve successfully set up an Amazon ECS cluster, configured a load balancer, deployed a containerized application, and verified functionality. ECS simplifies the management of containerized workloads, providing a scalable and secure foundation for your applications.

References

Getting Started with Amazon ECS

Deploy Java microservices on Amazon ECS using Amazon ECR and load balancing.