AWSCDK::ElasticLoadBalancing

11 types

Amazon Elastic Load Balancing Construct Library

The aws-cdk-lib/aws-elasticloadbalancing package provides constructs for configuring classic load balancers.

Configuring a Load Balancer

Load balancers send traffic to one or more AutoScalingGroups. Create a load balancer, set up listeners and a health check, and supply the fleet(s) you want to load balance to in the targets property. If you want the load balancer to be accessible from the internet, set internetFacing: true.

vpc = nil # AWSCDK::EC2::IVPC

my_auto_scaling_group = nil # AWSCDK::Autoscaling::AutoScalingGroup

lb = AWSCDK::ElasticLoadBalancing::LoadBalancer.new(self, "LB", {
    vpc: vpc,
    internet_facing: true,
    health_check: {
        port: 80,
    },
})
lb.add_target(my_auto_scaling_group)
lb.add_listener({
    external_port: 80,
})

The load balancer allows all connections by default. If you want to change that, pass the allow_connections_from property while setting up the listener:

my_security_group = nil # AWSCDK::EC2::SecurityGroup
lb = nil # AWSCDK::ElasticLoadBalancing::LoadBalancer

lb.add_listener({
    external_port: 80,
    allow_connections_from: [my_security_group],
})

Adding Ec2 Instance as a target for the load balancer

You can add an EC2 instance to the load balancer by calling using new InstanceTarget as the argument to add_target():

vpc = nil # AWSCDK::EC2::IVPC

lb = AWSCDK::ElasticLoadBalancing::LoadBalancer.new(self, "LB", {
    vpc: vpc,
    internet_facing: true,
})

# instance to add as the target for load balancer.
instance = AWSCDK::EC2::Instance.new(self, "targetInstance", {
    vpc: vpc,
    instance_type: AWSCDK::EC2::InstanceType.of(AWSCDK::EC2::InstanceClass::BURSTABLE2, AWSCDK::EC2::InstanceSize::MICRO),
    machine_image: AWSCDK::EC2::AmazonLinuxImage.new({generation: AWSCDK::EC2::AmazonLinuxGeneration::AMAZON_LINUX_2}),
})
lb.add_target(AWSCDK::ElasticLoadBalancing::InstanceTarget.new(instance))

API Reference

Classes 4

CfnLoadBalancerSpecifies a Classic Load Balancer. InstanceTargetAn EC2 instance that is the target for load balancing. ListenerPortReference to a listener's port just created. LoadBalancerA load balancer with a single listener.

Interfaces 6

CfnLoadBalancerPropsProperties for defining a `CfnLoadBalancer`. HealthCheckDescribe the health check to a load balancer. ILoadBalancerRepresents a load balancer. ILoadBalancerTargetInterface that is going to be implemented by constructs that you can load balance to. LoadBalancerListenerAdd a backend to the load balancer. LoadBalancerPropsConstruction properties for a LoadBalancer.

Enums 1

LoadBalancingProtocol