Home  Aws   Amazon elas ...

Amazon Elastic Container Service (ECS) example

Amazon Elastic Container Service (ECS) is a fully managed container orchestration service provided by AWS. It allows you to run and manage Docker containers on a cluster of EC2 instances or with AWS Fargate, a serverless compute engine for containers.

Here’s a step-by-step example to run a container using ECS:

Prerequisites

  1. AWS Account: Ensure you have an active AWS account.
  2. AWS CLI: Install and configure the AWS CLI on your local machine.
  3. Docker: Install Docker on your local machine to build container images.

Steps to Run a Container Using ECS

1. Create a Docker Image

  1. Dockerfile: Create a simple Dockerfile for a sample application. For this example, we’ll use a simple Node.js application.

    # Use the official Node.js image from the Docker Hub
    FROM node:14
    
    # Create and change to the app directory
    WORKDIR /usr/src/app
    
    # Copy the application code to the container
    COPY . .
    
    # Install dependencies
    RUN npm install
    
    # Expose the port the app runs on
    EXPOSE 8080
    
    # Run the app
    CMD ["node", "app.js"]
    
  2. Application Code: Create a simple app.js file.

    const http = require('http');
    
    const hostname = '0.0.0.0';
    const port = 8080;
    
    const server = http.createServer((req, res) => {
      res.statusCode = 200;
      res.setHeader('Content-Type', 'text/plain');
      res.end('Hello World\n');
    });
    
    server.listen(port, hostname, () => {
      console.log(`Server running at http://${hostname}:${port}/`);
    });
    
  3. Build Docker Image:

    docker build -t my-node-app .
    
  4. Push Docker Image to Amazon ECR:

    • Create an ECR repository:

      aws ecr create-repository --repository-name my-node-app
      
    • Authenticate Docker to your Amazon ECR registry:

      aws ecr get-login-password --region <your-region> | docker login --username AWS --password-stdin <account-id>.dkr.ecr.<region>.amazonaws.com
      
    • Tag the Docker image:

      docker tag my-node-app:latest <account-id>.dkr.ecr.<region>.amazonaws.com/my-node-app:latest
      
    • Push the Docker image:

      docker push <account-id>.dkr.ecr.<region>.amazonaws.com/my-node-app:latest
      

2. Set Up ECS

  1. Create an ECS Cluster:

    aws ecs create-cluster --cluster-name my-cluster
    
  2. Create a Task Definition:

    {
      "family": "my-task-def",
      "containerDefinitions": [
        {
          "name": "my-node-app",
          "image": "<account-id>.dkr.ecr.<region>.amazonaws.com/my-node-app:latest",
          "essential": true,
          "portMappings": [
            {
              "containerPort": 8080,
              "hostPort": 8080
            }
          ]
        }
      ],
      "requiresCompatibilities": [
        "EC2"
      ],
      "networkMode": "bridge",
      "memory": "512",
      "cpu": "256"
    }
    

    Save this as task-def.json and register it with ECS:

    aws ecs register-task-definition --cli-input-json file://task-def.json
    
  3. Run the Task:

    aws ecs run-task --cluster my-cluster --task-definition my-task-def
    

3. Set Up Networking

  1. Create an EC2 Instance: Ensure the instance is part of the ECS cluster and has Docker and ECS agent installed.
  2. Security Groups: Open port 8080 in the security group associated with the ECS instances.

4. Verify the Deployment

Using AWS Fargate

To use AWS Fargate instead of EC2 instances:

  1. Modify Task Definition:

    {
      "family": "my-task-def",
      "containerDefinitions": [
        {
          "name": "my-node-app",
          "image": "<account-id>.dkr.ecr.<region>.amazonaws.com/my-node-app:latest",
          "essential": true,
          "portMappings": [
            {
              "containerPort": 8080,
              "hostPort": 8080
            }
          ]
        }
      ],
      "requiresCompatibilities": [
        "FARGATE"
      ],
      "networkMode": "awsvpc",
      "memory": "512",
      "cpu": "256"
    }
    
  2. Create a Fargate Service:

    aws ecs create-service --cluster my-cluster --service-name my-service --task-definition my-task-def --desired-count 1 --launch-type FARGATE --network-configuration "awsvpcConfiguration={subnets=[<subnet-id>],securityGroups=[<security-group-id>],assignPublicIp=ENABLED}"
    
Published on: Jul 08, 2024, 04:54 AM  
 

Comments

Add your comment