LoadBalancer

class aws_cdk.aws_elasticloadbalancing.LoadBalancer(scope, id, *, vpc, access_logging_policy=None, cross_zone=None, health_check=None, internet_facing=None, listeners=None, subnet_selection=None, targets=None)

Bases: Resource

A load balancer with a single listener.

Routes to a fleet of of instances in a VPC.

ExampleMetadata:

infused

Example:

# cluster: ecs.Cluster
# task_definition: ecs.TaskDefinition
# vpc: ec2.Vpc

service = ecs.Ec2Service(self, "Service", cluster=cluster, task_definition=task_definition)

lb = elb.LoadBalancer(self, "LB", vpc=vpc)
lb.add_listener(external_port=80)
lb.add_target(service.load_balancer_target(
    container_name="MyContainer",
    container_port=80
))
Parameters:
  • scope (Construct) –

  • id (str) –

  • vpc (IVpc) – VPC network of the fleet instances.

  • access_logging_policy (Union[AccessLoggingPolicyProperty, Dict[str, Any], None]) – Enable Loadbalancer access logs Can be used to avoid manual work as aws console Required S3 bucket name , enabled flag Can add interval for pushing log Can set bucket prefix in order to provide folder name inside bucket. Default: - disabled

  • cross_zone (Optional[bool]) – Whether cross zone load balancing is enabled. This controls whether the load balancer evenly distributes requests across each availability zone Default: true

  • health_check (Union[HealthCheck, Dict[str, Any], None]) – Health check settings for the load balancing targets. Not required but recommended. Default: - None.

  • internet_facing (Optional[bool]) – Whether this is an internet-facing Load Balancer. This controls whether the LB has a public IP address assigned. It does not open up the Load Balancer’s security groups to public internet access. Default: false

  • listeners (Optional[Sequence[Union[LoadBalancerListener, Dict[str, Any]]]]) – What listeners to set up for the load balancer. Can also be added by .addListener() Default: -

  • subnet_selection (Union[SubnetSelection, Dict[str, Any], None]) – Which subnets to deploy the load balancer. Can be used to define a specific set of subnets to deploy the load balancer to. Useful multiple public or private subnets are covering the same availability zone. Default: - Public subnets if internetFacing, Private subnets otherwise

  • targets (Optional[Sequence[ILoadBalancerTarget]]) – What targets to load balance to. Can also be added by .addTarget() Default: - None.

Methods

add_listener(*, external_port, allow_connections_from=None, external_protocol=None, internal_port=None, internal_protocol=None, policy_names=None, ssl_certificate_arn=None)

Add a backend to the load balancer.

Parameters:
  • external_port (Union[int, float]) – External listening port.

  • allow_connections_from (Optional[Sequence[IConnectable]]) – Allow connections to the load balancer from the given set of connection peers. By default, connections will be allowed from anywhere. Set this to an empty list to deny connections, or supply a custom list of peers to allow connections from (IP ranges or security groups). Default: Anywhere

  • external_protocol (Optional[LoadBalancingProtocol]) – What public protocol to use for load balancing. Either ‘tcp’, ‘ssl’, ‘http’ or ‘https’. May be omitted if the external port is either 80 or 443.

  • internal_port (Union[int, float, None]) – Instance listening port. Same as the externalPort if not specified. Default: externalPort

  • internal_protocol (Optional[LoadBalancingProtocol]) – What public protocol to use for load balancing. Either ‘tcp’, ‘ssl’, ‘http’ or ‘https’. May be omitted if the internal port is either 80 or 443. The instance protocol is ‘tcp’ if the front-end protocol is ‘tcp’ or ‘ssl’, the instance protocol is ‘http’ if the front-end protocol is ‘https’.

  • policy_names (Optional[Sequence[str]]) – SSL policy names.

  • ssl_certificate_arn (Optional[str]) – the ARN of the SSL certificate. Default: - none

Return type:

ListenerPort

Returns:

A ListenerPort object that controls connections to the listener port

add_target(target)
Parameters:

target (ILoadBalancerTarget) –

Return type:

None

apply_removal_policy(policy)

Apply the given removal policy to this resource.

The Removal Policy controls what happens to this resource when it stops being managed by CloudFormation, either because you’ve removed it from the CDK application or because you’ve made a change that requires the resource to be replaced.

The resource can be deleted (RemovalPolicy.DESTROY), or left in your AWS account for data recovery and cleanup later (RemovalPolicy.RETAIN).

Parameters:

policy (RemovalPolicy) –

Return type:

None

to_string()

Returns a string representation of this construct.

Return type:

str

Attributes

connections

Control all connections from and to this load balancer.

env

The environment this resource belongs to.

For resources that are created and managed by the CDK (generally, those created by creating new class instances like Role, Bucket, etc.), this is always the same as the environment of the stack they belong to; however, for imported resources (those obtained from static methods like fromRoleArn, fromBucketName, etc.), that might be different than the stack they were imported into.

listener_ports

An object controlling specifically the connections for each listener added to this load balancer.

load_balancer_canonical_hosted_zone_name

true

Type:

attribute

load_balancer_canonical_hosted_zone_name_id

true

Type:

attribute

load_balancer_dns_name

true

Type:

attribute

load_balancer_name

true

Type:

attribute

load_balancer_source_security_group_group_name

true

Type:

attribute

load_balancer_source_security_group_owner_alias

true

Type:

attribute

node

The tree node.

stack

The stack in which this resource is defined.

Static Methods

classmethod is_construct(x)

Checks if x is a construct.

Use this method instead of instanceof to properly detect Construct instances, even when the construct library is symlinked.

Explanation: in JavaScript, multiple copies of the constructs library on disk are seen as independent, completely different libraries. As a consequence, the class Construct in each copy of the constructs library is seen as a different class, and an instance of one class will not test as instanceof the other class. npm install will not create installations like this, but users may manually symlink construct libraries together or use a monorepo tool: in those cases, multiple copies of the constructs library can be accidentally installed, and instanceof will behave unpredictably. It is safest to avoid using instanceof, and using this type-testing method instead.

Parameters:

x (Any) – Any object.

Return type:

bool

Returns:

true if x is an object created from a class which extends Construct.

classmethod is_owned_resource(construct)

Returns true if the construct was created by CDK, and false otherwise.

Parameters:

construct (IConstruct) –

Return type:

bool

classmethod is_resource(construct)

Check whether the given construct is a Resource.

Parameters:

construct (IConstruct) –

Return type:

bool