11 types
The aws-cdk-lib/aws-elasticloadbalancing package provides constructs for configuring
classic load balancers.
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],
})
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))