DeploymentLifecycleLambdaTarget Class
Use an AWS Lambda function as a deployment lifecycle hook target.
Example
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
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
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);
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);
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
ts
new DeploymentLifecycleLambdaTarget(handler: IFunction, id: string, props: DeploymentLifecycleLambdaTargetProps)
python
DeploymentLifecycleLambdaTarget(handler: IFunction, id: str, props: DeploymentLifecycleLambdaTargetProps)
java
new DeploymentLifecycleLambdaTarget(IFunction handler, String id, DeploymentLifecycleLambdaTargetProps props)
csharp
new DeploymentLifecycleLambdaTarget(IFunction handler, string id, DeploymentLifecycleLambdaTargetProps props)
go
NewDeploymentLifecycleLambdaTarget(Handler IFunction, Id *string, Props DeploymentLifecycleLambdaTargetProps)
Properties
Methods
bind(scope: IConstruct): DeploymentLifecycleHookTargetConfigdef bind(self, scope: IConstruct) -> DeploymentLifecycleHookTargetConfigpublic DeploymentLifecycleHookTargetConfig bind(IConstruct scope)public DeploymentLifecycleHookTargetConfig Bind(IConstruct scope)func Bind(Scope IConstruct) DeploymentLifecycleHookTargetConfig#Bind this target to a deployment lifecycle hook.