ApplicationListener

class aws_cdk.aws_elasticloadbalancingv2.ApplicationListener(scope, id, *, load_balancer, certificates=None, default_action=None, default_target_groups=None, open=None, port=None, protocol=None, ssl_policy=None)

Bases: BaseListener

Define an ApplicationListener.

Resource:

AWS::ElasticLoadBalancingV2::Listener

ExampleMetadata:

infused

Example:

from aws_cdk.aws_autoscaling import AutoScalingGroup
# asg: AutoScalingGroup
# vpc: ec2.Vpc


# Create the load balancer in a VPC. 'internetFacing' is 'false'
# by default, which creates an internal load balancer.
lb = elbv2.ApplicationLoadBalancer(self, "LB",
    vpc=vpc,
    internet_facing=True
)

# Add a listener and open up the load balancer's security group
# to the world.
listener = lb.add_listener("Listener",
    port=80,

    # 'open: true' is the default, you can leave it out if you want. Set it
    # to 'false' and use `listener.connections` if you want to be selective
    # about who can access the load balancer.
    open=True
)

# Create an AutoScaling group and add it as a load balancing
# target to the listener.
listener.add_targets("ApplicationFleet",
    port=8080,
    targets=[asg]
)
Parameters:
  • scope (Construct) –

  • id (str) –

  • load_balancer (IApplicationLoadBalancer) – The load balancer to attach this listener to.

  • certificates (Optional[Sequence[IListenerCertificate]]) – Certificate list of ACM cert ARNs. You must provide exactly one certificate if the listener protocol is HTTPS or TLS. Default: - No certificates.

  • default_action (Optional[ListenerAction]) – Default action to take for requests to this listener. This allows full control of the default action of the load balancer, including Action chaining, fixed responses and redirect responses. See the ListenerAction class for all options. Cannot be specified together with defaultTargetGroups. Default: - None.

  • default_target_groups (Optional[Sequence[IApplicationTargetGroup]]) – Default target groups to load balance to. All target groups will be load balanced to with equal weight and without stickiness. For a more complex configuration than that, use either defaultAction or addAction(). Cannot be specified together with defaultAction. Default: - None.

  • open (Optional[bool]) – Allow anyone to connect to the load balancer on the listener port. If this is specified, the load balancer will be opened up to anyone who can reach it. For internal load balancers this is anyone in the same VPC. For public load balancers, this is anyone on the internet. If you want to be more selective about who can access this load balancer, set this to false and use the listener’s connections object to selectively grant access to the load balancer on the listener port. Default: true

  • port (Union[int, float, None]) – The port on which the listener listens for requests. Default: - Determined from protocol if known.

  • protocol (Optional[ApplicationProtocol]) – The protocol to use. Default: - Determined from port if known.

  • ssl_policy (Optional[SslPolicy]) – The security policy that defines which ciphers and protocols are supported. Default: - The current predefined security policy.

Methods

add_action(id, *, action, remove_suffix=None, conditions=None, priority=None)

Perform the given default action on incoming requests.

This allows full control of the default action of the load balancer, including Action chaining, fixed responses and redirect responses. See the ListenerAction class for all options.

It’s possible to add routing conditions to the Action added in this way. At least one Action must be added without conditions (which becomes the default Action).

Parameters:
  • id (str) –

  • action (ListenerAction) – Action to perform.

  • remove_suffix (Optional[bool]) – ListenerRule``s have a ``Rule suffix on their logicalId by default. This allows you to remove that suffix. Legacy behavior of the addTargetGroups() convenience method did not include the Rule suffix on the logicalId of the generated ListenerRule. At some point, increasing complexity of requirements can require users to switch from the addTargetGroups() method to the addAction() method. When migrating ListenerRule``s deployed by a legacy version of ``addTargetGroups(), you will need to enable this flag to avoid changing the logicalId of your resource. Otherwise Cfn will attempt to replace the ListenerRule and fail. Default: - use standard logicalId with the Rule suffix

  • conditions (Optional[Sequence[ListenerCondition]]) – Rule applies if matches the conditions. Default: - No conditions.

  • priority (Union[int, float, None]) – Priority of this target group. The rule with the lowest priority will be used for every request. If priority is not given, these target groups will be added as defaults, and must not have conditions. Priorities must be unique. Default: Target groups are used as defaults

Return type:

None

add_certificates(id, certificates)

Add one or more certificates to this listener.

After the first certificate, this creates ApplicationListenerCertificates resources since cloudformation requires the certificates array on the listener resource to have a length of 1.

Parameters:
Return type:

None

add_target_groups(id, *, target_groups, conditions=None, priority=None)

Load balance incoming requests to the given target groups.

All target groups will be load balanced to with equal weight and without stickiness. For a more complex configuration than that, use addAction().

It’s possible to add routing conditions to the TargetGroups added in this way. At least one TargetGroup must be added without conditions (which will become the default Action for this listener).

Parameters:
  • id (str) –

  • target_groups (Sequence[IApplicationTargetGroup]) – Target groups to forward requests to.

  • conditions (Optional[Sequence[ListenerCondition]]) – Rule applies if matches the conditions. Default: - No conditions.

  • priority (Union[int, float, None]) – Priority of this target group. The rule with the lowest priority will be used for every request. If priority is not given, these target groups will be added as defaults, and must not have conditions. Priorities must be unique. Default: Target groups are used as defaults

Return type:

None

add_targets(id, *, deregistration_delay=None, health_check=None, load_balancing_algorithm_type=None, port=None, protocol=None, protocol_version=None, slow_start=None, stickiness_cookie_duration=None, stickiness_cookie_name=None, target_group_name=None, targets=None, conditions=None, priority=None)

Load balance incoming requests to the given load balancing targets.

This method implicitly creates an ApplicationTargetGroup for the targets involved, and a ‘forward’ action to route traffic to the given TargetGroup.

If you want more control over the precise setup, create the TargetGroup and use addAction yourself.

It’s possible to add conditions to the targets added in this way. At least one set of targets must be added without conditions.

Parameters:
  • id (str) –

  • deregistration_delay (Optional[Duration]) – The amount of time for Elastic Load Balancing to wait before deregistering a target. The range is 0-3600 seconds. Default: Duration.minutes(5)

  • health_check (Union[HealthCheck, Dict[str, Any], None]) – Health check configuration. Default: - The default value for each property in this configuration varies depending on the target.

  • load_balancing_algorithm_type (Optional[TargetGroupLoadBalancingAlgorithmType]) – The load balancing algorithm to select targets for routing requests. Default: round_robin.

  • port (Union[int, float, None]) – The port on which the listener listens for requests. Default: Determined from protocol if known

  • protocol (Optional[ApplicationProtocol]) – The protocol to use. Default: Determined from port if known

  • protocol_version (Optional[ApplicationProtocolVersion]) – The protocol version to use. Default: ApplicationProtocolVersion.HTTP1

  • slow_start (Optional[Duration]) – The time period during which the load balancer sends a newly registered target a linearly increasing share of the traffic to the target group. The range is 30-900 seconds (15 minutes). Default: 0

  • stickiness_cookie_duration (Optional[Duration]) – The stickiness cookie expiration period. Setting this value enables load balancer stickiness. After this period, the cookie is considered stale. The minimum value is 1 second and the maximum value is 7 days (604800 seconds). Default: Stickiness disabled

  • stickiness_cookie_name (Optional[str]) – The name of an application-based stickiness cookie. Names that start with the following prefixes are not allowed: AWSALB, AWSALBAPP, and AWSALBTG; they’re reserved for use by the load balancer. Note: stickinessCookieName parameter depends on the presence of stickinessCookieDuration parameter. If stickinessCookieDuration is not set, stickinessCookieName will be omitted. Default: - If stickinessCookieDuration is set, a load-balancer generated cookie is used. Otherwise, no stickiness is defined.

  • target_group_name (Optional[str]) – The name of the target group. This name must be unique per region per account, can have a maximum of 32 characters, must contain only alphanumeric characters or hyphens, and must not begin or end with a hyphen. Default: Automatically generated

  • targets (Optional[Sequence[IApplicationLoadBalancerTarget]]) – The targets to add to this target group. Can be Instance, IPAddress, or any self-registering load balancing target. All target must be of the same type.

  • conditions (Optional[Sequence[ListenerCondition]]) – Rule applies if matches the conditions. Default: - No conditions.

  • priority (Union[int, float, None]) – Priority of this target group. The rule with the lowest priority will be used for every request. If priority is not given, these target groups will be added as defaults, and must not have conditions. Priorities must be unique. Default: Target groups are used as defaults

Return type:

ApplicationTargetGroup

Returns:

The newly created target group

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

register_connectable(connectable, port_range)

Register that a connectable that has been added to this load balancer.

Don’t call this directly. It is called by ApplicationTargetGroup.

Parameters:
Return type:

None

to_string()

Returns a string representation of this construct.

Return type:

str

Attributes

connections

Manage connections to this ApplicationListener.

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_arn

ARN of the listener.

Attribute:

true

load_balancer

Load balancer this listener is associated with.

node

The tree node.

stack

The stack in which this resource is defined.

Static Methods

classmethod from_application_listener_attributes(scope, id, *, listener_arn, security_group, default_port=None)

Import an existing listener.

Parameters:
  • scope (Construct) –

  • id (str) –

  • listener_arn (str) – ARN of the listener.

  • security_group (ISecurityGroup) – Security group of the load balancer this listener is associated with.

  • default_port (Union[int, float, None]) – The default port on which this listener is listening.

Return type:

IApplicationListener

classmethod from_lookup(scope, id, *, listener_arn=None, listener_protocol=None, listener_port=None, load_balancer_arn=None, load_balancer_tags=None)

Look up an ApplicationListener.

Parameters:
  • scope (Construct) –

  • id (str) –

  • listener_arn (Optional[str]) – ARN of the listener to look up. Default: - does not filter by listener arn

  • listener_protocol (Optional[ApplicationProtocol]) – Filter listeners by listener protocol. Default: - does not filter by listener protocol

  • listener_port (Union[int, float, None]) – Filter listeners by listener port. Default: - does not filter by listener port

  • load_balancer_arn (Optional[str]) – Filter listeners by associated load balancer arn. Default: - does not filter by load balancer arn

  • load_balancer_tags (Optional[Mapping[str, str]]) – Filter listeners by associated load balancer tags. Default: - does not filter by load balancer tags

Return type:

IApplicationListener

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