1. Answers
  2. Deploy an AWS ECS Task Definition

How do I deploy an AWS ECS task definition?

In this guide, we will deploy an AWS ECS task definition using Pulumi in TypeScript. An ECS task definition is a blueprint for your application, describing one or more containers (up to a maximum of ten) that form your application. It specifies various parameters for the containers, such as the Docker image to use, CPU and memory requirements, and networking settings.

Key Points:

  • We will define an ECS task definition specifying the container details.
  • We will use Pulumi to deploy this task definition to AWS.
  • The example will include required resources like ECS Task Definition and IAM roles.
import * as aws from "@pulumi/aws";

// Create an IAM role for the ECS task
const taskRole = new aws.iam.Role("taskRole", {
    assumeRolePolicy: {
        Version: "2012-10-17",
        Statement: [
            {
                Action: "sts:AssumeRole",
                Principal: {
                    Service: "ecs-tasks.amazonaws.com",
                },
                Effect: "Allow",
                Sid: "",
            },
        ],
    },
});

// Attach a policy to the IAM role
const taskRolePolicy = new aws.iam.RolePolicyAttachment("taskRolePolicy", {
    role: taskRole.name,
    policyArn: "arn:aws:iam::aws:policy/service-role/AmazonECSTaskExecutionRolePolicy",
});

// Define the ECS task definition
const taskDefinition = new aws.ecs.TaskDefinition("taskDefinition", {
    family: "my-task-family",
    cpu: "256",
    memory: "512",
    networkMode: "awsvpc",
    requiresCompatibilities: ["FARGATE"],
    executionRoleArn: taskRole.arn,
    containerDefinitions: JSON.stringify([
        {
            name: "my-container",
            image: "nginx",
            essential: true,
            portMappings: [
                {
                    containerPort: 80,
                    hostPort: 80,
                    protocol: "tcp",
                },
            ],
        },
    ]),
});

// Export the task definition ARN
export const taskDefinitionArn = taskDefinition.arn;

Summary:

In this guide, we created an IAM role for the ECS task, attached the necessary policy, and defined an ECS task definition with a container running the NGINX image. We then deployed this task definition using Pulumi. The ARN of the deployed task definition is exported for reference. This setup allows you to run containerized applications on AWS ECS using the specified task definition.

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