aws-cdk-lib.aws_elasticloadbalancing.LoadBalancer

class LoadBalancer (construct)

LanguageType name
.NETAmazon.CDK.AWS.ElasticLoadBalancing.LoadBalancer
Gogithub.com/aws/aws-cdk-go/awscdk/v2/awselasticloadbalancing#LoadBalancer
Javasoftware.amazon.awscdk.services.elasticloadbalancing.LoadBalancer
Pythonaws_cdk.aws_elasticloadbalancing.LoadBalancer
TypeScript (source)aws-cdk-lib » aws_elasticloadbalancing » LoadBalancer

Implements IConstruct, IDependable, IResource, IConnectable

A load balancer with a single listener.

Routes to a fleet of of instances in a VPC.

Example

declare const vpc: ec2.IVpc;
const lb = new elb.LoadBalancer(this, 'LB', {
  vpc,
  internetFacing: true,
  healthCheck: {
    port: 80,
  },
});

declare const myAutoScalingGroup: autoscaling.AutoScalingGroup;
lb.addTarget(myAutoScalingGroup);
lb.addListener({
  externalPort: 80,
});

Initializer

new LoadBalancer(scope: Construct, id: string, props: LoadBalancerProps)

Parameters

  • scope Construct
  • id string
  • props LoadBalancerProps

Construct Props

NameTypeDescription
vpcIVpcVPC network of the fleet instances.
accessLoggingPolicy?AccessLoggingPolicyPropertyEnable Loadbalancer access logs Can be used to avoid manual work as aws console Required S3 bucket name , enabled flag Can add interval for pushing log Can set bucket prefix in order to provide folder name inside bucket.
crossZone?booleanWhether cross zone load balancing is enabled.
healthCheck?HealthCheckHealth check settings for the load balancing targets.
internetFacing?booleanWhether this is an internet-facing Load Balancer.
listeners?LoadBalancerListener[]What listeners to set up for the load balancer.
subnetSelection?SubnetSelectionWhich subnets to deploy the load balancer.
targets?ILoadBalancerTarget[]What targets to load balance to.

vpc

Type: IVpc

VPC network of the fleet instances.


accessLoggingPolicy?

Type: AccessLoggingPolicyProperty (optional, default: disabled)

Enable Loadbalancer access logs Can be used to avoid manual work as aws console Required S3 bucket name , enabled flag Can add interval for pushing log Can set bucket prefix in order to provide folder name inside bucket.


crossZone?

Type: boolean (optional, default: true)

Whether cross zone load balancing is enabled.

This controls whether the load balancer evenly distributes requests across each availability zone


healthCheck?

Type: HealthCheck (optional, default: None.)

Health check settings for the load balancing targets.

Not required but recommended.


internetFacing?

Type: boolean (optional, default: false)

Whether this is an internet-facing Load Balancer.

This controls whether the LB has a public IP address assigned. It does not open up the Load Balancer's security groups to public internet access.


listeners?

Type: LoadBalancerListener[] (optional, default: -)

What listeners to set up for the load balancer.

Can also be added by .addListener()


subnetSelection?

Type: SubnetSelection (optional, default: Public subnets if internetFacing, Private subnets otherwise)

Which subnets to deploy the load balancer.

Can be used to define a specific set of subnets to deploy the load balancer to. Useful multiple public or private subnets are covering the same availability zone.


targets?

Type: ILoadBalancerTarget[] (optional, default: None.)

What targets to load balance to.

Can also be added by .addTarget()

Properties

NameTypeDescription
connectionsConnectionsControl all connections from and to this load balancer.
envResourceEnvironmentThe environment this resource belongs to.
listenerPortsListenerPort[]An object controlling specifically the connections for each listener added to this load balancer.
loadBalancerCanonicalHostedZoneNamestring
loadBalancerCanonicalHostedZoneNameIdstring
loadBalancerDnsNamestring
loadBalancerNamestring
loadBalancerSourceSecurityGroupGroupNamestring
loadBalancerSourceSecurityGroupOwnerAliasstring
nodeNodeThe tree node.
stackStackThe stack in which this resource is defined.

connections

Type: Connections

Control all connections from and to this load balancer.


env

Type: ResourceEnvironment

The environment this resource belongs to.

For resources that are created and managed by the CDK (generally, those created by creating new class instances like Role, Bucket, etc.), this is always the same as the environment of the stack they belong to; however, for imported resources (those obtained from static methods like fromRoleArn, fromBucketName, etc.), that might be different than the stack they were imported into.


listenerPorts

Type: ListenerPort[]

An object controlling specifically the connections for each listener added to this load balancer.


loadBalancerCanonicalHostedZoneName

Type: string


loadBalancerCanonicalHostedZoneNameId

Type: string


loadBalancerDnsName

Type: string


loadBalancerName

Type: string


loadBalancerSourceSecurityGroupGroupName

Type: string


loadBalancerSourceSecurityGroupOwnerAlias

Type: string


node

Type: Node

The tree node.


stack

Type: Stack

The stack in which this resource is defined.

Methods

NameDescription
addListener(listener)Add a backend to the load balancer.
addTarget(target)
applyRemovalPolicy(policy)Apply the given removal policy to this resource.
toString()Returns a string representation of this construct.

addListener(listener)

public addListener(listener: LoadBalancerListener): ListenerPort

Parameters

  • listener LoadBalancerListener

Returns

  • ListenerPort

Add a backend to the load balancer.


addTarget(target)

public addTarget(target: ILoadBalancerTarget): void

Parameters

  • target ILoadBalancerTarget

applyRemovalPolicy(policy)

public applyRemovalPolicy(policy: RemovalPolicy): void

Parameters

  • policy RemovalPolicy

Apply the given removal policy to this resource.

The Removal Policy controls what happens to this resource when it stops being managed by CloudFormation, either because you've removed it from the CDK application or because you've made a change that requires the resource to be replaced.

The resource can be deleted (RemovalPolicy.DESTROY), or left in your AWS account for data recovery and cleanup later (RemovalPolicy.RETAIN).


toString()

public toString(): string

Returns

  • string

Returns a string representation of this construct.