# ApplicationListenerRule

Define a new listener rule.

Extends: [Construct](https://docs.aws.amazon.com/cdk/api/v2/docs/constructs.Construct.html) › ApplicationListenerRule
Implements: [IConstruct](https://docs.aws.amazon.com/cdk/api/v2/docs/constructs.IConstruct.html), [IDependable](https://docs.aws.amazon.com/cdk/api/v2/docs/constructs.IDependable.html)
Used as argument in: [ListenerRuleConfiguration.applicationListenerRule](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_ecs.ListenerRuleConfiguration.html#applicationlistenerrule)

- Source: [`aws-elasticloadbalancingv2/lib/alb/application-listener-rule.ts`](https://github.com/aws/aws-cdk/blob/v2.278.0/packages/aws-cdk-lib/aws-elasticloadbalancingv2/lib/alb/application-listener-rule.ts#L205)

## Example

TypeScript:

```ts
declare const cluster: ecs.Cluster;
declare const taskDefinition: ecs.TaskDefinition;
declare const blueTargetGroup: elbv2.ApplicationTargetGroup;
declare const greenTargetGroup: elbv2.ApplicationTargetGroup;
declare const prodListenerRule: elbv2.ApplicationListenerRule;

const service = new ecs.FargateService(this, 'Service', {
  cluster,
  taskDefinition,
  deploymentStrategy: ecs.DeploymentStrategy.LINEAR,
  linearConfiguration: {
    stepPercent: 10.0,
    stepBakeTime: Duration.minutes(5),
  },
});

const target = service.loadBalancerTarget({
  containerName: 'web',
  containerPort: 80,
  alternateTarget: new ecs.AlternateTarget('AlternateTarget', {
    alternateTargetGroup: greenTargetGroup,
    productionListener: ecs.ListenerRuleConfiguration.applicationListenerRule(prodListenerRule),
  }),
});

target.attachToApplicationTargetGroup(blueTargetGroup);
```

Python:

```python
# cluster: ecs.Cluster
# task_definition: ecs.TaskDefinition
# blue_target_group: elbv2.ApplicationTargetGroup
# green_target_group: elbv2.ApplicationTargetGroup
# prod_listener_rule: elbv2.ApplicationListenerRule

service = ecs.FargateService(self, "Service",
    cluster=cluster,
    task_definition=task_definition,
    deployment_strategy=ecs.DeploymentStrategy.LINEAR,
    linear_configuration=ecs.TrafficShiftConfig(
        step_percent=10,
        step_bake_time=Duration.minutes(5)
    )
)

target = service.load_balancer_target(
    container_name="web",
    container_port=80,
    alternate_target=ecs.AlternateTarget("AlternateTarget",
        alternate_target_group=green_target_group,
        production_listener=ecs.ListenerRuleConfiguration.application_listener_rule(prod_listener_rule)
    )
)

target.attach_to_application_target_group(blue_target_group)
```

Java:

```java
Cluster cluster;
TaskDefinition taskDefinition;
ApplicationTargetGroup blueTargetGroup;
ApplicationTargetGroup greenTargetGroup;
ApplicationListenerRule prodListenerRule;

FargateService service = FargateService.Builder.create(this, "Service")
        .cluster(cluster)
        .taskDefinition(taskDefinition)
        .deploymentStrategy(DeploymentStrategy.LINEAR)
        .linearConfiguration(TrafficShiftConfig.builder()
                .stepPercent(10)
                .stepBakeTime(Duration.minutes(5))
                .build())
        .build();

IEcsLoadBalancerTarget target = service.loadBalancerTarget(LoadBalancerTargetOptions.builder()
        .containerName("web")
        .containerPort(80)
        .alternateTarget(AlternateTarget.Builder.create("AlternateTarget")
                .alternateTargetGroup(greenTargetGroup)
                .productionListener(ListenerRuleConfiguration.applicationListenerRule(prodListenerRule))
                .build())
        .build());

target.attachToApplicationTargetGroup(blueTargetGroup);
```

C# / .NET:

```csharp
// Install: dotnet add package Amazon.CDK.Lib
Cluster cluster;
TaskDefinition taskDefinition;
ApplicationTargetGroup blueTargetGroup;
ApplicationTargetGroup greenTargetGroup;
ApplicationListenerRule prodListenerRule;

var service = new FargateService(this, "Service", new FargateServiceProps {
    Cluster = cluster,
    TaskDefinition = taskDefinition,
    DeploymentStrategy = DeploymentStrategy.LINEAR,
    LinearConfiguration = new TrafficShiftConfig {
        StepPercent = 10,
        StepBakeTime = Duration.Minutes(5)
    }
});

var target = service.LoadBalancerTarget(new LoadBalancerTargetOptions {
    ContainerName = "web",
    ContainerPort = 80,
    AlternateTarget = new AlternateTarget("AlternateTarget", new AlternateTargetProps {
        AlternateTargetGroup = greenTargetGroup,
        ProductionListener = ListenerRuleConfiguration.ApplicationListenerRule(prodListenerRule)
    })
});

target.AttachToApplicationTargetGroup(blueTargetGroup);
```

Golang:

```go
// Install: go get github.com/aws/aws-cdk-go/awscdk/v2
var cluster Cluster
var taskDefinition TaskDefinition
var blueTargetGroup ApplicationTargetGroup
var greenTargetGroup ApplicationTargetGroup
var prodListenerRule ApplicationListenerRule

service := ecs.NewFargateService(this, jsii.String("Service"), &FargateServiceProps{
	Cluster: Cluster,
	TaskDefinition: TaskDefinition,
	DeploymentStrategy: ecs.DeploymentStrategy_LINEAR,
	LinearConfiguration: &TrafficShiftConfig{
		StepPercent: jsii.Number(10),
		StepBakeTime: awscdk.Duration_Minutes(jsii.Number(5)),
	},
})

target := service.LoadBalancerTarget(&LoadBalancerTargetOptions{
	ContainerName: jsii.String("web"),
	ContainerPort: jsii.Number(80),
	AlternateTarget: ecs.NewAlternateTarget(jsii.String("AlternateTarget"), &AlternateTargetProps{
		AlternateTargetGroup: greenTargetGroup,
		ProductionListener: ecs.ListenerRuleConfiguration_ApplicationListenerRule(prodListenerRule),
	}),
})

target.AttachToApplicationTargetGroup(blueTargetGroup)
```

## Initializer

TypeScript:

```ts
new ApplicationListenerRule(scope: Construct, id: string, props: ApplicationListenerRuleProps)
```

Python:

```python
ApplicationListenerRule(scope: Construct, id: str, props: ApplicationListenerRuleProps)
```

Java:

```java
new ApplicationListenerRule(Construct scope, String id, ApplicationListenerRuleProps props)
```

C# / .NET:

```csharp
new ApplicationListenerRule(Construct scope, string id, ApplicationListenerRuleProps props)
```

Golang:

```go
NewApplicationListenerRule(Scope Construct, Id *string, Props ApplicationListenerRuleProps)
```

## Construct Props

- `action`: [ListenerAction](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_elasticloadbalancingv2.ListenerAction.html) — Action to perform when requests are received.

  **Default:** No action

  Only one of `action`, `fixedResponse`, `redirectResponse` or `targetGroups` can be specified.

- `conditions`: [ListenerCondition](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_elasticloadbalancingv2.ListenerCondition.html)[] — Rule applies if matches the conditions.

  **Default:** No conditions.

- `listener`: [IApplicationListener](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_elasticloadbalancingv2.IApplicationListener.html) (required) — The listener to attach the rule to.

- `priority`: number (required) — Priority of the rule.

  The rule with the lowest priority will be used for every request.

  Priorities must be unique.

- `targetGroups`: [IApplicationTargetGroup](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_elasticloadbalancingv2.IApplicationTargetGroup.html)[] — Target groups to forward requests to.

  **Default:** No target groups.

  Only one of `action`, `fixedResponse`, `redirectResponse` or `targetGroups` can be specified.

  Implies a `forward` action.

## Properties

- `listenerRuleArn`: string — The ARN of this rule.

- `node`: [Node](https://docs.aws.amazon.com/cdk/api/v2/docs/constructs.Node.html) — The tree node.

## Methods

- `addCondition(condition: ListenerCondition): void` — Add a non-standard condition to this rule.

- `configureAction(action: ListenerAction): void` — Configure the action to perform for this rule.

- `isConstruct(x: any): boolean` (static) — 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 object.

  **Returns:** true if `x` is an object created from a class which extends `Construct`.

- `toString(): string` — Returns a string representation of this construct.

- `with(...mixins: IMixin[]): IConstruct` — Applies one or more mixins to this construct.

  Mixins are applied in order. The list of constructs is captured at the
  start of the call, so constructs added by a mixin will not be visited.
  Use multiple `with()` calls if subsequent mixins should apply to added
  constructs.

  **Parameters**

  - `mixins` — The mixins to apply.

  **Returns:** This construct for chaining
