aws-cdk-lib.aws_elasticloadbalancingv2.NetworkListener

class NetworkListener (construct)

LanguageType name
.NETAmazon.CDK.AWS.ElasticLoadBalancingV2.NetworkListener
Gogithub.com/aws/aws-cdk-go/awscdk/v2/awselasticloadbalancingv2#NetworkListener
Javasoftware.amazon.awscdk.services.elasticloadbalancingv2.NetworkListener
Pythonaws_cdk.aws_elasticloadbalancingv2.NetworkListener
TypeScript (source)aws-cdk-lib » aws_elasticloadbalancingv2 » NetworkListener

Implements IConstruct, IDependable, IResource, IListener, INetworkListener

Define a Network Listener.

Example

import { HttpNlbIntegration } from '@aws-cdk/aws-apigatewayv2-integrations-alpha';

const vpc = new ec2.Vpc(this, 'VPC');
const lb = new elbv2.NetworkLoadBalancer(this, 'lb', { vpc });
const listener = lb.addListener('listener', { port: 80 });
listener.addTargets('target', {
  port: 80,
});

const httpEndpoint = new apigwv2.HttpApi(this, 'HttpProxyPrivateApi', {
  defaultIntegration: new HttpNlbIntegration('DefaultIntegration', listener),
});

Initializer

new NetworkListener(scope: Construct, id: string, props: NetworkListenerProps)

Parameters

  • scope Construct
  • id string
  • props NetworkListenerProps

Construct Props

NameTypeDescription
loadBalancerINetworkLoadBalancerThe load balancer to attach this listener to.
portnumberThe port on which the listener listens for requests.
alpnPolicy?AlpnPolicyApplication-Layer Protocol Negotiation (ALPN) is a TLS extension that is sent on the initial TLS handshake hello messages.
certificates?IListenerCertificate[]Certificate list of ACM cert ARNs.
defaultAction?NetworkListenerActionDefault action to take for requests to this listener.
defaultTargetGroups?INetworkTargetGroup[]Default target groups to load balance to.
protocol?ProtocolProtocol for listener, expects TCP, TLS, UDP, or TCP_UDP.
sslPolicy?SslPolicySSL Policy.

loadBalancer

Type: INetworkLoadBalancer

The load balancer to attach this listener to.


port

Type: number

The port on which the listener listens for requests.


alpnPolicy?

Type: AlpnPolicy (optional, default: None)

Application-Layer Protocol Negotiation (ALPN) is a TLS extension that is sent on the initial TLS handshake hello messages.

ALPN enables the application layer to negotiate which protocols should be used over a secure connection, such as HTTP/1 and HTTP/2.

Can only be specified together with Protocol TLS.


certificates?

Type: IListenerCertificate[] (optional, default: No certificates.)

Certificate list of ACM cert ARNs.

You must provide exactly one certificate if the listener protocol is HTTPS or TLS.


defaultAction?

Type: NetworkListenerAction (optional, default: None.)

Default action to take for requests to this listener.

This allows full control of the default Action of the load balancer, including weighted forwarding. See the NetworkListenerAction class for all options.

Cannot be specified together with defaultTargetGroups.


defaultTargetGroups?

Type: INetworkTargetGroup[] (optional, default: None.)

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.


protocol?

Type: Protocol (optional, default: TLS if certificates are provided. TCP otherwise.)

Protocol for listener, expects TCP, TLS, UDP, or TCP_UDP.


sslPolicy?

Type: SslPolicy (optional, default: Current predefined security policy.)

SSL Policy.

Properties

NameTypeDescription
envResourceEnvironmentThe environment this resource belongs to.
listenerArnstringARN of the listener.
loadBalancerINetworkLoadBalancerThe load balancer this listener is attached to.
nodeNodeThe tree node.
stackStackThe stack in which this resource is defined.

env

Type: ResourceEnvironment

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.


listenerArn

Type: string

ARN of the listener.


loadBalancer

Type: INetworkLoadBalancer

The load balancer this listener is attached to.


node

Type: Node

The tree node.


stack

Type: Stack

The stack in which this resource is defined.

Methods

NameDescription
addAction(_id, props)Perform the given Action on incoming requests.
addCertificates(id, certificates)Add one or more certificates to this listener.
addTargetGroups(_id, ...targetGroups)Load balance incoming requests to the given target groups.
addTargets(id, props)Load balance incoming requests to the given load balancing targets.
applyRemovalPolicy(policy)Apply the given removal policy to this resource.
toString()Returns a string representation of this construct.
static fromLookup(scope, id, options)Looks up a network listener.
static fromNetworkListenerArn(scope, id, networkListenerArn)Import an existing listener.

addAction(_id, props)

public addAction(_id: string, props: AddNetworkActionProps): void

Parameters

  • _id string
  • props AddNetworkActionProps

Perform the given Action on incoming requests.

This allows full control of the default Action of the load balancer, including weighted forwarding. See the NetworkListenerAction class for all options.


addCertificates(id, certificates)

public addCertificates(id: string, certificates: IListenerCertificate[]): void

Parameters

  • id string
  • certificates IListenerCertificate[]

Add one or more certificates to this listener.

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


addTargetGroups(_id, ...targetGroups)

public addTargetGroups(_id: string, ...targetGroups: INetworkTargetGroup[]): void

Parameters

  • _id string
  • targetGroups INetworkTargetGroup

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().


addTargets(id, props)

public addTargets(id: string, props: AddNetworkTargetsProps): NetworkTargetGroup

Parameters

  • id string
  • props AddNetworkTargetsProps

Returns

  • NetworkTargetGroup

Load balance incoming requests to the given load balancing targets.

This method implicitly creates a NetworkTargetGroup 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.


applyRemovalPolicy(policy)

public applyRemovalPolicy(policy: RemovalPolicy): void

Parameters

  • policy RemovalPolicy

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).


toString()

public toString(): string

Returns

  • string

Returns a string representation of this construct.


static fromLookup(scope, id, options)

public static fromLookup(scope: Construct, id: string, options: NetworkListenerLookupOptions): INetworkListener

Parameters

  • scope Construct
  • id string
  • options NetworkListenerLookupOptions

Returns

  • INetworkListener

Looks up a network listener.


static fromNetworkListenerArn(scope, id, networkListenerArn)

public static fromNetworkListenerArn(scope: Construct, id: string, networkListenerArn: string): INetworkListener

Parameters

  • scope Construct
  • id string
  • networkListenerArn string

Returns

  • INetworkListener

Import an existing listener.