1. Answers
  2. Configuring Health Checks in an ALB Server Group

How do I configure health checks in an ALB server group?

This guide demonstrates how to configure health checks within an Application Load Balancer (ALB) server group using Pulumi. Health checks are crucial for ensuring that your load balancer routes traffic only to healthy instances. We will create an ALB, a target group, and configure health checks for the target group.

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

// Create a new VPC
const vpc = new aws.ec2.Vpc("myVpc", {
    cidrBlock: "10.0.0.0/16",
});

// Create a subnet
const subnet = new aws.ec2.Subnet("mySubnet", {
    vpcId: vpc.id,
    cidrBlock: "10.0.1.0/24",
    availabilityZone: "us-west-2a",
});

// Create a security group
const securityGroup = new aws.ec2.SecurityGroup("mySecurityGroup", {
    vpcId: vpc.id,
    ingress: [
        { protocol: "tcp", fromPort: 80, toPort: 80, cidrBlocks: ["0.0.0.0/0"] },
    ],
    egress: [
        { protocol: "tcp", fromPort: 0, toPort: 0, cidrBlocks: ["0.0.0.0/0"] },
    ],
});

// Create an ALB
const alb = new aws.lb.LoadBalancer("myLoadBalancer", {
    internal: false,
    securityGroups: [securityGroup.id],
    subnets: [subnet.id],
});

// Create a target group
const targetGroup = new aws.lb.TargetGroup("myTargetGroup", {
    port: 80,
    protocol: "HTTP",
    vpcId: vpc.id,
    healthCheck: {
        enabled: true,
        path: "/health",
        interval: 30,
        timeout: 5,
        healthyThreshold: 2,
        unhealthyThreshold: 2,
        matcher: "200",
    },
});

// Create a listener for the ALB
const listener = new aws.lb.Listener("myListener", {
    loadBalancerArn: alb.arn,
    port: 80,
    defaultActions: [
        {
            type: "forward",
            targetGroupArn: targetGroup.arn,
        },
    ],
});

// Export the DNS name of the load balancer
export const loadBalancerDnsName = alb.dnsName;

Key Points

  • VPC and Subnet: We created a VPC and a subnet to host our ALB and target instances.
  • Security Group: A security group was created to allow HTTP traffic.
  • ALB: An Application Load Balancer was created and attached to the subnet and security group.
  • Target Group: A target group was created with health checks configured to monitor the /health path.
  • Listener: A listener was set up to forward traffic to the target group.

Summary

In this guide, we configured health checks for an ALB server group using Pulumi. We created a VPC, subnet, security group, ALB, target group, and listener. The health checks ensure that only healthy instances receive traffic.

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