{"id":9517,"library":"aws-cdk-aws-elasticloadbalancingv2","title":"AWS CDK Elastic Load Balancing V2","description":"The `aws-cdk-aws-elasticloadbalancingv2` library is the AWS Cloud Development Kit (CDK) Construct Library for AWS Elastic Load Balancing V2 (ALB and NLB). It allows developers to define and deploy load balancers using familiar programming languages. The current version is 1.204.0, and it follows the frequent release cadence of the broader AWS CDK project.","status":"active","version":"1.204.0","language":"en","source_language":"en","source_url":"https://github.com/aws/aws-cdk.git","tags":["aws","cdk","load balancing","elbv2","alb","nlb"],"install":[{"cmd":"pip install aws-cdk-aws-elasticloadbalancingv2","lang":"bash","label":"Install library"}],"dependencies":[],"imports":[{"symbol":"ApplicationLoadBalancer","correct":"from aws_cdk import aws_elasticloadbalancingv2 as elbv2"},{"symbol":"NetworkLoadBalancer","correct":"from aws_cdk import aws_elasticloadbalancingv2 as elbv2"},{"symbol":"ApplicationTargetGroup","correct":"from aws_cdk import aws_elasticloadbalancingv2 as elbv2"},{"note":"CDK best practice is to import entire modules with aliases (e.g., `aws_ec2 as ec2`) rather than individual symbols, to avoid name clashes and keep code consistent.","wrong":"from aws_cdk.aws_ec2 import Vpc","symbol":"Vpc","correct":"from aws_cdk import aws_ec2 as ec2"}],"quickstart":{"code":"import os\nfrom aws_cdk import (\n    App, Stack,\n    aws_ec2 as ec2,\n    aws_elasticloadbalancingv2 as elbv2,\n    Environment\n)\n\nclass MyAlbStack(Stack):\n    def __init__(self, scope: App, id: str, **kwargs):\n        super().__init__(scope, id, **kwargs)\n\n        # Look up an existing VPC or create a new one\n        # For simplicity, creating a minimal one for quickstart\n        vpc = ec2.Vpc(self, \"MyAlbVpc\",\n            max_azs=2,\n            nat_gateways=0 # Minimal cost\n        )\n\n        # Create an Application Load Balancer\n        alb = elbv2.ApplicationLoadBalancer(self, \"MyAlb\",\n            vpc=vpc,\n            internet_facing=True,\n            load_balancer_name=\"MyQuickstartALB\"\n        )\n\n        # Add a listener for HTTP traffic on port 80\n        listener = alb.add_listener(\"MyHttpListener\",\n            port=80,\n            open=True # Allow all inbound traffic for quickstart\n        )\n\n        # Add a default target group (e.g., an empty one for now)\n        # In a real app, you would add instances or IP targets here\n        listener.add_targets(\"MyTargetGroup\",\n            port=80,\n            targets=[] # No targets initially\n        )\n\n        # Output the ALB DNS name\n        # CfnOutput(self, \"AlbDnsName\", value=alb.load_balancer_dns_name)\n\napp = App()\nMyAlbStack(app, \"AlbQuickstartStack\",\n    env=Environment(\n        account=os.environ.get(\"CDK_DEFAULT_ACCOUNT\"),\n        region=os.environ.get(\"CDK_DEFAULT_REGION\")\n    )\n)\napp.synth()","lang":"python","description":"This quickstart code defines a basic AWS CDK stack that creates an Application Load Balancer (ALB) within a new VPC. It configures the ALB to be internet-facing with an HTTP listener on port 80 and an empty default target group. This provides a minimal runnable example to demonstrate the core components of ELBv2 with CDK."},"warnings":[{"fix":"Refer to the official AWS CDK v2 migration guide for a complete list of changes. Update import statements (`from aws_cdk import aws_elasticloadbalancingv2 as elbv2` becomes `from aws_cdk import aws_lb as elbv2`), and review construct property changes.","message":"Major breaking changes occurred during migration from AWS CDK v1 to v2. Import paths, module names, and certain APIs for ELBv2 constructs (e.g., `aws_elasticloadbalancingv2` changed to `aws_lb`) have been updated.","severity":"breaking","affected_versions":"All v1 versions when upgrading to any v2 version."},{"fix":"Always specify `internet_facing=False` in the `ApplicationLoadBalancer` or `NetworkLoadBalancer` constructor if the load balancer should only be accessible within the VPC. The default behavior can vary based on other properties like `vpc_placement`.","message":"Failing to explicitly set `internet_facing=False` when an internal ALB/NLB is desired can accidentally expose services publicly or result in unexpected network behavior.","severity":"gotcha","affected_versions":"All versions."},{"fix":"Carefully review the `health_check` properties on `ApplicationTargetGroup` or `NetworkTargetGroup` and ensure they precisely match the application's health endpoint. Verify security groups allow traffic from the load balancer to the target health check port.","message":"Misconfigured target group health checks (e.g., wrong path, port, or status codes, or invalid security group rules) are a very common cause of targets not registering, appearing unhealthy, or failing to receive traffic.","severity":"gotcha","affected_versions":"All versions."}],"env_vars":null,"last_verified":"2026-04-17T00:00:00.000Z","next_check":"2026-07-16T00:00:00.000Z","problems":[{"fix":"Ensure `aws-cdk-aws-elasticloadbalancingv2` is installed and `aws-cdk.core` is compatible. For CDK v1, use `from aws_cdk import aws_elasticloadbalancingv2 as elbv2`. If targeting CDK v2, the import should be `from aws_cdk import aws_lb as elbv2`.","cause":"Attempting to use a CDK v2 import pattern (e.g., `aws_cdk.aws_lb`) in a CDK v1 project, or incorrect import alias/name, or missing package installation.","error":"AttributeError: module 'aws_cdk.aws_elasticloadbalancingv2' has no attribute 'ApplicationLoadBalancer'"},{"fix":"Ensure the `ec2.Vpc` object passed to the load balancer has at least two subnets, ideally across different AZs. If using `ec2.Vpc.from_lookup`, verify the looked-up VPC meets this requirement. When creating a new VPC, ensure `max_azs` is at least 2.","cause":"Load balancers require at least two subnets in different Availability Zones within the VPC for high availability. This error occurs if the provided VPC does not have sufficient subnets or if explicit `vpc_subnets` selection results in fewer than two.","error":"ValidationError: At least two subnets must be specified."},{"fix":"Provide one or more `ec2.SecurityGroup` objects to the `security_groups` property of the `ApplicationLoadBalancer`. Ensure these security groups allow necessary inbound traffic (e.g., HTTP/80, HTTPS/443). Confirm `internet_facing` property matches intended access pattern.","cause":"ALBs require security groups to control network access. If none are provided, or if the load balancer is configured as internal (`internet_facing=False`) but accessed externally, these issues arise.","error":"No security groups were specified for the load balancer (when trying to access the ALB) or The load balancer does not have an internet-facing scheme. Accessing it via public DNS will fail."}]}