# 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`.

```ts
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,
});
```

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

```ts
declare const mySecurityGroup: ec2.SecurityGroup;
declare const lb: elb.LoadBalancer;
lb.addListener({
  externalPort: 80,
  allowConnectionsFrom: [mySecurityGroup],
});
```

### 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 `addTarget()`:

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

// instance to add as the target for load balancer.
const instance = new ec2.Instance(this, 'targetInstance', {
  vpc: vpc,
  instanceType: ec2.InstanceType.of(ec2.InstanceClass.BURSTABLE2, ec2.InstanceSize.MICRO),
  machineImage: new ec2.AmazonLinuxImage({ generation: ec2.AmazonLinuxGeneration.AMAZON_LINUX_2 }),
});
lb.addTarget(new elb.InstanceTarget(instance));
```

## Types

| Name | Kind | Summary |
| --- | --- | --- |
| [`LoadBalancer`](/v2/docs/aws-cdk-lib.aws_elasticloadbalancing.LoadBalancer) | Construct (L2) | A load balancer with a single listener. |
| [`CfnLoadBalancer`](/v2/docs/aws-cdk-lib.aws_elasticloadbalancing.CfnLoadBalancer) | Construct (L1) | Specifies a Classic Load Balancer. |
| [`CfnLoadBalancer.AccessLoggingPolicyProperty`](/v2/docs/aws-cdk-lib.aws_elasticloadbalancing.CfnLoadBalancer.AccessLoggingPolicyProperty) | Struct | Specifies where and how access logs are stored for your Classic Load Balancer. |
| [`CfnLoadBalancer.AppCookieStickinessPolicyProperty`](/v2/docs/aws-cdk-lib.aws_elasticloadbalancing.CfnLoadBalancer.AppCookieStickinessPolicyProperty) | Struct | Specifies a policy for application-controlled session stickiness for your Classic Load Balancer. |
| [`CfnLoadBalancer.ConnectionDrainingPolicyProperty`](/v2/docs/aws-cdk-lib.aws_elasticloadbalancing.CfnLoadBalancer.ConnectionDrainingPolicyProperty) | Struct | Specifies the connection draining settings for your Classic Load Balancer. |
| [`CfnLoadBalancer.ConnectionSettingsProperty`](/v2/docs/aws-cdk-lib.aws_elasticloadbalancing.CfnLoadBalancer.ConnectionSettingsProperty) | Struct | Specifies the idle timeout value for your Classic Load Balancer. |
| [`CfnLoadBalancer.HealthCheckProperty`](/v2/docs/aws-cdk-lib.aws_elasticloadbalancing.CfnLoadBalancer.HealthCheckProperty) | Struct | Specifies health check settings for your Classic Load Balancer. |
| [`CfnLoadBalancer.LBCookieStickinessPolicyProperty`](/v2/docs/aws-cdk-lib.aws_elasticloadbalancing.CfnLoadBalancer.LBCookieStickinessPolicyProperty) | Struct | Specifies a policy for duration-based session stickiness for your Classic Load Balancer. |
| [`CfnLoadBalancer.ListenersProperty`](/v2/docs/aws-cdk-lib.aws_elasticloadbalancing.CfnLoadBalancer.ListenersProperty) | Struct | Specifies a listener for your Classic Load Balancer. |
| [`CfnLoadBalancer.PoliciesProperty`](/v2/docs/aws-cdk-lib.aws_elasticloadbalancing.CfnLoadBalancer.PoliciesProperty) | Struct | Specifies policies for your Classic Load Balancer. |
| [`CfnLoadBalancer.SourceSecurityGroupProperty`](/v2/docs/aws-cdk-lib.aws_elasticloadbalancing.CfnLoadBalancer.SourceSecurityGroupProperty) | Struct |  |
| [`CfnLoadBalancerProps`](/v2/docs/aws-cdk-lib.aws_elasticloadbalancing.CfnLoadBalancerProps) | Struct | Properties for defining a `CfnLoadBalancer`. |
| [`HealthCheck`](/v2/docs/aws-cdk-lib.aws_elasticloadbalancing.HealthCheck) | Struct | Describe the health check to a load balancer. |
| [`ILoadBalancer`](/v2/docs/aws-cdk-lib.aws_elasticloadbalancing.ILoadBalancer) | Interface | Represents a load balancer. |
| [`ILoadBalancerTarget`](/v2/docs/aws-cdk-lib.aws_elasticloadbalancing.ILoadBalancerTarget) | Interface | Interface that is going to be implemented by constructs that you can load balance to. |
| [`InstanceTarget`](/v2/docs/aws-cdk-lib.aws_elasticloadbalancing.InstanceTarget) | Class | An EC2 instance that is the target for load balancing. |
| [`ListenerPort`](/v2/docs/aws-cdk-lib.aws_elasticloadbalancing.ListenerPort) | Class | Reference to a listener's port just created. |
| [`LoadBalancerListener`](/v2/docs/aws-cdk-lib.aws_elasticloadbalancing.LoadBalancerListener) | Struct | Add a backend to the load balancer. |
| [`LoadBalancerProps`](/v2/docs/aws-cdk-lib.aws_elasticloadbalancing.LoadBalancerProps) | Struct | Construction properties for a LoadBalancer. |
| [`LoadBalancingProtocol`](/v2/docs/aws-cdk-lib.aws_elasticloadbalancing.LoadBalancingProtocol) | Enum |  |
