1. Answers
  2. Performing Health Checks on ECS Service Tasks

How do I perform health checks on ECS service tasks?

In this example, we are going to configure a health check for tasks running in an AWS ECS service. Health checks are a critical part of maintaining the reliability and availability of your applications. By implementing health checks, ECS can ensure that tasks are running correctly and can automatically restart tasks that are unhealthy.

We’ll define an ECS service that uses a task definition, and we’ll configure the health check parameters to ensure that the service consistently monitors the health of the tasks.

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

// Define a VPC
const main = new aws.ec2.Vpc("main", {cidrBlock: "10.0.0.0/16"});
// Define an ECS cluster
const example = new aws.ecs.Cluster("example", {});
// Define a security group
const allowHttp = new aws.ec2.SecurityGroup("allow_http", {
    vpcId: main.id,
    ingress: [{
        fromPort: 80,
        toPort: 80,
        protocol: "tcp",
        cidrBlocks: ["0.0.0.0/0"],
    }],
    egress: [{
        fromPort: 0,
        toPort: 0,
        protocol: "-1",
        cidrBlocks: ["0.0.0.0/0"],
    }],
});
// Create a subnet
const mainSubnet = new aws.ec2.Subnet("main", {
    vpcId: main.id,
    cidrBlock: "10.0.1.0/24",
});
// Define a load balancer
const internal = new aws.lb.LoadBalancer("internal", {
    name: "internal",
    internal: false,
    loadBalancerType: "application",
    securityGroups: [allowHttp.id],
    subnets: [mainSubnet.id],
});
// Define a target group
const exampleTargetGroup = new aws.lb.TargetGroup("example", {
    name: "example",
    port: 80,
    protocol: "HTTP",
    vpcId: main.id,
    healthCheck: {
        matcher: "200",
        interval: 30,
        timeout: 10,
        healthyThreshold: 2,
        unhealthyThreshold: 2,
        path: "/health",
    },
});
// Define a load balancer listener
const frontEnd = new aws.lb.Listener("front_end", {
    loadBalancerArn: internal.arn,
    port: 80,
    protocol: "HTTP",
    defaultActions: [{
        type: "forward",
        targetGroupArn: exampleTargetGroup.arn,
    }],
});
// Define a task definition
const exampleTaskDefinition = new aws.ecs.TaskDefinition("example", {
    family: "example",
    networkMode: "awsvpc",
    requiresCompatibilities: ["FARGATE"],
    memory: "512",
    cpu: "256",
    containerDefinitions: JSON.stringify([{
        name: "nginx",
        image: "nginx",
        portMappings: [{
            containerPort: 80,
            hostPort: 80,
        }],
    }]),
});
// Define an ECS service
const exampleService = new aws.ecs.Service("example", {
    cluster: example.id,
    taskDefinition: exampleTaskDefinition.arn,
    desiredCount: 1,
    launchType: "FARGATE",
    networkConfiguration: {
        subnets: [mainSubnet.id],
        securityGroups: [allowHttp.id],
        assignPublicIp: true,
    },
    loadBalancers: [{
        targetGroupArn: exampleTargetGroup.arn,
        containerName: "nginx",
        containerPort: 80,
    }],
});
export const serviceName = exampleService.name;
export const loadBalancerDns = internal.dnsName;

In summary, we created an ECS cluster and defined an ECS service complete with a task definition. A load balancer with health checks ensures tasks are healthy by monitoring them on the specified endpoint and performing recovery actions as needed. Outputs provided will help you find the service name and the DNS of the load balancer to access your service.

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