AWS CDK Elastic Load Balancing (V1)
This library provides AWS CDK V1 constructs for provisioning and managing AWS Classic Load Balancers (ELBv1). As part of the AWS CDK v1 ecosystem, it is currently in maintenance mode. The latest version is 1.204.0, with releases typically tied to the overall CDK v1 release cycle.
Common errors
-
ModuleNotFoundError: No module named 'aws_cdk.aws_elasticloadbalancingv2'
cause Attempting to import v2 load balancer constructs (like ALB/NLB) after installing only the v1 Classic Load Balancer library.fixInstall the correct CDK v2 library: `pip install aws-cdk.aws-elasticloadbalancingv2` or ensure you are using CDK v2. -
AttributeError: module 'aws_cdk.aws_elasticloadbalancing' has no attribute 'ApplicationLoadBalancer'
cause Trying to instantiate an Application Load Balancer using the v1 Classic Load Balancer library.fixThe `ApplicationLoadBalancer` construct is part of `aws_cdk.aws_elasticloadbalancingv2`. Import it from there: `from aws_cdk import aws_elasticloadbalancingv2 as elbv2`. -
TypeError: __init__() missing 1 required positional argument: 'scope'
cause A CDK construct was instantiated without its required `scope` argument, which is typically the parent construct (e.g., `self` within a Stack).fixEnsure all CDK constructs are instantiated with `(self, "LogicalId", ...)` within a class extending `Construct` or `Stack`.
Warnings
- breaking AWS CDK v1 to v2 migration involves significant breaking changes across the entire CDK ecosystem. This `aws-cdk.aws-elasticloadbalancing` library is specific to CDK v1 and is not compatible with CDK v2 without a full migration of your CDK application.
- gotcha This library specifically manages *Classic Load Balancers (ELBv1)*. Do not confuse it with Application Load Balancers (ALB) or Network Load Balancers (NLB), which are managed by the `aws-cdk.aws-elasticloadbalancingv2` library.
- deprecated AWS Classic Load Balancers themselves are considered a legacy service by AWS, with Application and Network Load Balancers being the recommended modern alternatives. New deployments should generally prefer ELBv2 constructs.
Install
-
pip install aws-cdk.aws-elasticloadbalancing aws-cdk.core aws-cdk.aws-ec2 constructs
Imports
- CfnLoadBalancer
from aws_cdk.aws_elasticloadbalancingv2 import ApplicationLoadBalancer
from aws_cdk import aws_elasticloadbalancing as elb
Quickstart
import os
from aws_cdk import (
App, Stack, Environment,
aws_ec2 as ec2,
aws_elasticloadbalancing as elb,
CfnOutput
)
from constructs import Construct
class ElbV1Stack(Stack):
def __init__(self, scope: Construct, id: str, **kwargs) -> None:
super().__init__(scope, id, **kwargs)
# Create a VPC
vpc = ec2.Vpc(self, "MyVpc",
max_azs=2,
cidr="10.10.0.0/16",
subnet_configuration=[
ec2.SubnetConfiguration(
name="Public",
subnet_type=ec2.SubnetType.PUBLIC,
cidr_mask=24
),
ec2.SubnetConfiguration(
name="Private",
subnet_type=ec2.SubnetType.PRIVATE_ISOLATED,
cidr_mask=24
)
]
)
# Select public subnets for the Classic Load Balancer
public_subnets = vpc.select_subnets(subnet_type=ec2.SubnetType.PUBLIC)
# Create a Classic Load Balancer
clb = elb.CfnLoadBalancer(self, "MyClassicLoadBalancer",
listeners=[
elb.CfnLoadBalancer.ListenersProperty(
instance_port="80",
instance_protocol="HTTP",
load_balancer_port="80",
protocol="HTTP"
)
],
scheme="internet-facing",
subnets=[s.subnet_id for s in public_subnets.subnets]
# For production, consider adding security_groups and health_check.
# The CLB will create a default security group if none is provided.
)
CfnOutput(self, "LoadBalancerDNS", value=clb.get_att("DNSName").to_string())
app = App()
ElbV1Stack(app, "ElbV1ExampleStack",
env=Environment(
account=os.environ.get("CDK_DEFAULT_ACCOUNT"),
region=os.environ.get("CDK_DEFAULT_REGION")
)
)
app.synth()