1. Answers
  2. How to setup ECS with multiple containers?

How do I setup ECS with multiple containers?

To set up an ECS (Elastic Container Service) cluster with multiple containers, we need to follow these steps:

  1. Create an ECS Cluster: This is the logical grouping of tasks or services.
  2. Define Task Definitions: These specify the containers to run, including their configurations.
  3. Create an ECS Service: This ensures the desired number of task instances are running and handles load balancing.

We’ll use Pulumi to define these resources in TypeScript. Below is the detailed explanation followed by the actual Pulumi program.

Explanation

  1. ECS Cluster: We’ll create an ECS cluster where our tasks will run.
  2. Task Definition: This will define the containers that make up our application. Each container can have its own image, CPU, memory, environment variables, ports, etc.
  3. ECS Service: This will manage the running instances of our task definition, ensuring that the desired number of tasks are maintained.

Steps:

  1. Create an ECS Cluster: This is the base on which we will run our tasks.
  2. Define Task Definition: This will include the configuration for two containers.
  3. Create an ECS Service: This will manage the tasks and ensure they are running as expected.

Pulumi Program

import * as pulumi from "@pulumi/pulumi";
import * as aws from "@pulumi/aws";
import * as awsx from "@pulumi/awsx";

// Create an ECS cluster
const cluster = new aws.ecs.Cluster("my-cluster");

// Create the task execution role
const executionRole = new aws.iam.Role("ecsTaskExecutionRole", {
    assumeRolePolicy: JSON.stringify({
        Version: "2012-10-17",
        Statement: [{
            Action: "sts:AssumeRole",
            Effect: "Allow", 
            Principal: {
                Service: "ecs-tasks.amazonaws.com"
            }
        }]
    })
});

// Attach the required policy for task execution
const executionRolePolicy = new aws.iam.RolePolicyAttachment("ecsTaskExecutionRolePolicy", {
    role: executionRole.name,
    policyArn: "arn:aws:iam::aws:policy/service-role/AmazonECSTaskExecutionRolePolicy"
});


// Define a task definition with multiple containers
const taskDefinition = new aws.ecs.TaskDefinition("my-task", {
    family: "my-task-family",
    cpu: "256",
    memory: "512",
    networkMode: "awsvpc",
    requiresCompatibilities: ["FARGATE"],
    executionRoleArn: executionRole.arn,
    containerDefinitions: pulumi.output([
        {
            name: "nginx",
            image: "nginx:latest",
            essential: true,
            portMappings: [{
                containerPort: 80,
                hostPort: 80,
                protocol: "tcp"
            }]
        },
        {
            name: "app",
            image: "my-app-image:latest",
            essential: true,
            portMappings: [{
                containerPort: 8080,
                hostPort: 8080,
                protocol: "tcp"
            }]
        }
    ]).apply(JSON.stringify)
});

// Create a Fargate service to run the task definition
const service = new awsx.ecs.FargateService("my-service", {
    cluster: cluster.arn,
    taskDefinition: taskDefinition.arn,
    desiredCount: 2,
    networkConfiguration: {
        assignPublicIp: true,
        subnets: ["subnet-12345678"],  // Replace with your subnet ID
        securityGroups: ["sg-12345678"]  // Replace with your security group ID
    }
});

// Export the cluster
export const clusterArn = cluster.arn;

Explanation of the Code:

  1. Cluster: We create an ECS cluster using aws.ecs.Cluster.
  2. Task Definition:
    • We define a task with two containers:
      • nginx container with port 80 exposed.
      • app container with port 8080 exposed.
    • The executionRoleArn is required for the task to pull container images and log to CloudWatch.
  3. Service:
    • We use awsx.ecs.FargateService to create a service that will run our task definition.
    • desiredCount specifies the number of task instances to run.
    • networkConfiguration includes subnets and security groups for the tasks.

This Pulumi program sets up an ECS cluster with a task definition containing multiple containers and ensures that the specified number of instances of the task are running.

Deploy this code

Want to deploy this code? Sign up for a free Pulumi account to deploy in a few clicks.

Sign up

New to Pulumi?

Want to deploy this code? Sign up with Pulumi to deploy in a few clicks.

Sign up