Deploying a Docker Image on Amazon ECS

In this article, we’ll guide you through deploying a Docker image on Amazon Elastic Container Service (ECS) using AWS Fargate. We’ll cover creating a task definition, setting up a cluster, and launching a service.

Docker Image: https://hub.docker.com/r/cloudsecnetwork/csn-nginx-demo

Step 1: Create a Task Definition

A task definition is like a blueprint for your application. It defines various parameters for the Docker container, including the image to use, CPU, and memory requirements.

  1. Navigate to ECS in the AWS Management Console.
  2. Create a new task definition:
    • Click on Task Definitions > Create new Task Definition.
    • Choose Fargate as the launch type.
  3. Configure the task definition:
    • Task Definition Name: Give it a unique name, such as csn-nginx-demo-task.
    • Task Role: Leave this empty for now. Task roles are used to grant permissions to the tasks themselves.
    • Task Execution Role: Select Create new role. This role allows ECS to pull the container image and write logs to CloudWatch.
    • Task Size: Set CPU to 0.5 vCPU and memory to 1 GB. These values determine the amount of resources allocated to your task.
  4. Add Container:
    • Container Name: csn-web-app.
    • Image: cloudsecnetwork/csn-nginx-demo.
    • Port Mappings: Set container port to 80 and protocol to HTTP. This tells ECS which port the container listens on for traffic.
    • Leave other parameters as default.

Step 2: Create an ECS Cluster

A cluster is a logical grouping of tasks or services. With AWS Fargate, you don’t need to manage EC2 instances; AWS handles the infrastructure.

  1. Navigate to Clusters in the ECS console.
  2. Create Cluster:
    • Cluster Name: Enter a unique name, such as csn-demo-cluster.
    • Choose AWS Fargate under the Infrastructure section.
    • Optionally, enable Container Insights for monitoring and logging.

Step 3: Create a Service

A service allows you to run and maintain a specified number of instances of a task definition simultaneously in an ECS cluster.

  1. Navigate to the newly created cluster.
  2. Create Service:
    • Launch Type: Select Fargate.
    • Task Definition: Select the task definition created earlier.
    • Service Name: Give it a unique name, such as csn-web-service.
    • Number of Tasks: Set to 1 (or more if needed, but consider cost implications).
  3. Configure Network:
    • Cluster VPC: Select the default VPC or a custom one.
    • Subnets: Choose available subnets.
    • Security Groups: Use default or customize as needed. Ensure port 80 is open to allow HTTP traffic.
  4. Set Up Load Balancer:
    • Load Balancing: Choose Application Load Balancer.
    • Configure a new load balancer, listener, and target group if not already set up.
  5. Optional Security Group Configuration:
    • Ensure the security group used has inbound rules that allow traffic on port 80.

Step 4: Security Group Rules (Optional)

If the default security group is used, ensure that the inbound rules allow HTTP traffic:

  • Port: 80
  • Source: 0.0.0.0/0 (allowing traffic from any IP address)

This configuration ensures that your application is accessible over the web.

Conclusion

By following these steps, you have successfully deployed a Docker image on Amazon ECS using AWS Fargate. This setup allows you to scale your application effortlessly while benefiting from AWS’s managed infrastructure.

Additional Considerations

  • Cost Management: Be mindful of the costs associated with running multiple task instances.
  • Security: Regularly review and update security group rules to ensure your application remains secure.
  • Scaling: ECS services can be configured to automatically scale based on demand, providing flexibility and resilience.

Tagged in :