aws-cdk-lib.aws_elasticloadbalancing-readme

aws-cdk-lib.aws_elasticloadbalancing module

LanguagePackage
.NETAmazon.CDK.AWS.ElasticLoadBalancing
Gogithub.com/aws/aws-cdk-go/awscdk/v2/awselasticloadbalancing
Javasoftware.amazon.awscdk.services.elasticloadbalancing
Pythonaws_cdk.aws_elasticloadbalancing
TypeScriptaws-cdk-lib » aws_elasticloadbalancing

Amazon Elastic Load Balancing Construct Library

The @aws-cdk/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.

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

Example not in your language?

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

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

Example not in your language?

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():

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));

Example not in your language?