# DeploymentLifecycleLambdaTarget

Use an AWS Lambda function as a deployment lifecycle hook target.

Implements: [IDeploymentLifecycleHookTarget](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_ecs.IDeploymentLifecycleHookTarget.html)

- Source: [`aws-ecs/lib/deployment-lifecycle-hook-target.ts`](https://github.com/aws/aws-cdk/blob/v2.278.0/packages/aws-cdk-lib/aws-ecs/lib/deployment-lifecycle-hook-target.ts#L91)
- Type last changed: v2.211.0 · 13 Aug 2025

## Example

TypeScript:

```ts
import * as lambda from 'aws-cdk-lib/aws-lambda';

declare const cluster: ecs.Cluster;
declare const taskDefinition: ecs.TaskDefinition;
declare const lambdaHook: lambda.Function;
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.BLUE_GREEN,
});

service.addLifecycleHook(new ecs.DeploymentLifecycleLambdaTarget(lambdaHook, 'PreScaleHook', {
  lifecycleStages: [ecs.DeploymentLifecycleStage.PRE_SCALE_UP],
}));

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

target.attachToApplicationTargetGroup(blueTargetGroup);
```

Python:

```python
import aws_cdk.aws_lambda as lambda_

# cluster: ecs.Cluster
# task_definition: ecs.TaskDefinition
# lambda_hook: lambda.Function
# 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.BLUE_GREEN
)

service.add_lifecycle_hook(ecs.DeploymentLifecycleLambdaTarget(lambda_hook, "PreScaleHook",
    lifecycle_stages=[ecs.DeploymentLifecycleStage.PRE_SCALE_UP]
))

target = service.load_balancer_target(
    container_name="nginx",
    container_port=80,
    protocol=ecs.Protocol.TCP,
    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
import software.amazon.awscdk.services.lambda.*;

Cluster cluster;
TaskDefinition taskDefinition;
Function lambdaHook;
ApplicationTargetGroup blueTargetGroup;
ApplicationTargetGroup greenTargetGroup;
ApplicationListenerRule prodListenerRule;

FargateService service = FargateService.Builder.create(this, "Service")
        .cluster(cluster)
        .taskDefinition(taskDefinition)
        .deploymentStrategy(DeploymentStrategy.BLUE_GREEN)
        .build();

service.addLifecycleHook(DeploymentLifecycleLambdaTarget.Builder.create(lambdaHook, "PreScaleHook")
        .lifecycleStages(List.of(DeploymentLifecycleStage.PRE_SCALE_UP))
        .build());

IEcsLoadBalancerTarget target = service.loadBalancerTarget(LoadBalancerTargetOptions.builder()
        .containerName("nginx")
        .containerPort(80)
        .protocol(Protocol.TCP)
        .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
using Amazon.CDK.AWS.Lambda;

Cluster cluster;
TaskDefinition taskDefinition;
Function lambdaHook;
ApplicationTargetGroup blueTargetGroup;
ApplicationTargetGroup greenTargetGroup;
ApplicationListenerRule prodListenerRule;

var service = new FargateService(this, "Service", new FargateServiceProps {
    Cluster = cluster,
    TaskDefinition = taskDefinition,
    DeploymentStrategy = DeploymentStrategy.BLUE_GREEN
});

service.AddLifecycleHook(new DeploymentLifecycleLambdaTarget(lambdaHook, "PreScaleHook", new DeploymentLifecycleLambdaTargetProps {
    LifecycleStages = new [] { DeploymentLifecycleStage.PRE_SCALE_UP }
}));

var target = service.LoadBalancerTarget(new LoadBalancerTargetOptions {
    ContainerName = "nginx",
    ContainerPort = 80,
    Protocol = Protocol.TCP,
    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
import lambda "github.com/aws/aws-cdk-go/awscdk"

var cluster Cluster
var taskDefinition TaskDefinition
var lambdaHook Function
var blueTargetGroup ApplicationTargetGroup
var greenTargetGroup ApplicationTargetGroup
var prodListenerRule ApplicationListenerRule

service := ecs.NewFargateService(this, jsii.String("Service"), &FargateServiceProps{
	Cluster: Cluster,
	TaskDefinition: TaskDefinition,
	DeploymentStrategy: ecs.DeploymentStrategy_BLUE_GREEN,
})

service.AddLifecycleHook(ecs.NewDeploymentLifecycleLambdaTarget(lambdaHook, jsii.String("PreScaleHook"), &DeploymentLifecycleLambdaTargetProps{
	LifecycleStages: []DeploymentLifecycleStage{
		ecs.DeploymentLifecycleStage_PRE_SCALE_UP,
	},
}))

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

target.AttachToApplicationTargetGroup(blueTargetGroup)
```

## Initializer

TypeScript:

```ts
new DeploymentLifecycleLambdaTarget(handler: IFunction, id: string, props: DeploymentLifecycleLambdaTargetProps)
```

Python:

```python
DeploymentLifecycleLambdaTarget(handler: IFunction, id: str, props: DeploymentLifecycleLambdaTargetProps)
```

Java:

```java
new DeploymentLifecycleLambdaTarget(IFunction handler, String id, DeploymentLifecycleLambdaTargetProps props)
```

C# / .NET:

```csharp
new DeploymentLifecycleLambdaTarget(IFunction handler, string id, DeploymentLifecycleLambdaTargetProps props)
```

Golang:

```go
NewDeploymentLifecycleLambdaTarget(Handler IFunction, Id *string, Props DeploymentLifecycleLambdaTargetProps)
```

## Properties

- `role`: [IRole](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_iam.IRole.html) — The IAM role for the deployment lifecycle hook target.

## Methods

- `bind(scope: IConstruct): DeploymentLifecycleHookTargetConfig` — Bind this target to a deployment lifecycle hook.
