Skip to content

Commit 1d2eda2

Browse files
committed
fix(ecs): add support for hook details field
1 parent 8a6cf46 commit 1d2eda2

File tree

10 files changed

+253
-107
lines changed

10 files changed

+253
-107
lines changed

packages/@aws-cdk-testing/framework-integ/test/aws-ecs/test/base/integ.blue-green-deployment-strategy.js.snapshot/aws-ecs-blue-green-deployment.assets.json

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/@aws-cdk-testing/framework-integ/test/aws-ecs/test/base/integ.blue-green-deployment-strategy.js.snapshot/aws-ecs-blue-green-deployment.template.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -742,6 +742,7 @@
742742
"DeploymentConfiguration": {
743743
"LifecycleHooks": [
744744
{
745+
"HookDetails": "{\"stringKey\":\"stringValue\",\"intKey\":1,\"boolKey\":true,\"listKey\":[\"stringValue1\",\"stringValue2\"]}",
745746
"HookTargetArn": {
746747
"Fn::GetAtt": [
747748
"LambdaHookBF1BC8B4",

packages/@aws-cdk-testing/framework-integ/test/aws-ecs/test/base/integ.blue-green-deployment-strategy.js.snapshot/integ.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/@aws-cdk-testing/framework-integ/test/aws-ecs/test/base/integ.blue-green-deployment-strategy.js.snapshot/manifest.json

Lines changed: 11 additions & 101 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/@aws-cdk-testing/framework-integ/test/aws-ecs/test/base/integ.blue-green-deployment-strategy.js.snapshot/tree.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/@aws-cdk-testing/framework-integ/test/aws-ecs/test/base/integ.blue-green-deployment-strategy.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,12 @@ const service = new ecs.FargateService(stack, 'Service', {
125125

126126
service.addLifecycleHook(new ecs.DeploymentLifecycleLambdaTarget(lambdaHook, 'PreScaleUp', {
127127
lifecycleStages: [ecs.DeploymentLifecycleStage.PRE_SCALE_UP],
128+
hookDetails: {
129+
stringKey: 'stringValue',
130+
intKey: 1,
131+
boolKey: true,
132+
listKey: ['stringValue1', 'stringValue2'],
133+
},
128134
}));
129135

130136
const target = service.loadBalancerTarget({

packages/aws-cdk-lib/aws-ecs/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2200,6 +2200,7 @@ const service = new ecs.FargateService(this, 'Service', {
22002200

22012201
service.addLifecycleHook(new ecs.DeploymentLifecycleLambdaTarget(lambdaHook, 'PreScaleHook', {
22022202
lifecycleStages: [ecs.DeploymentLifecycleStage.PRE_SCALE_UP],
2203+
hookDetails: {'stringKey':'stringValue', 'intKey':1, 'boolKey':true, "list": ["abc",1,true]},
22032204
}));
22042205

22052206
const target = service.loadBalancerTarget({

packages/aws-cdk-lib/aws-ecs/lib/base/base-service.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -879,6 +879,7 @@ export abstract class BaseService extends Resource
879879
hookTargetArn: config.targetArn,
880880
roleArn: config.role!.roleArn,
881881
lifecycleStages: config.lifecycleStages.map(stage => stage.toString()),
882+
hookDetails: config.hookDetails,
882883
};
883884
});
884885
}

packages/aws-cdk-lib/aws-ecs/lib/deployment-lifecycle-hook-target.ts

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,38 @@
11
import { IConstruct } from 'constructs';
22
import * as iam from '../../aws-iam';
33
import * as lambda from '../../aws-lambda';
4+
import { Fn } from '../../core';
5+
6+
/**
7+
* Error thrown when hook details validation fails
8+
*/
9+
class HookDetailsValidationError extends Error {
10+
constructor(message: string) {
11+
super(message);
12+
this.name = 'HookDetailsValidationError';
13+
}
14+
}
15+
16+
/**
17+
* Validates that input is a valid JSON object and returns it as a stringified JSON using Fn::ToJsonString
18+
* @param hookDetails The input to validate (must be a JSON object)
19+
* @returns The stringified JSON using CloudFormation's Fn::ToJsonString intrinsic function
20+
* @throws HookDetailsValidationError if the input is not a valid JSON object
21+
*/
22+
export function stringifyHookDetails(hookDetails: any): string {
23+
// Reject arrays
24+
if (Array.isArray(hookDetails)) {
25+
throw new HookDetailsValidationError('hookDetails must be a JSON object, got: array');
26+
}
27+
28+
// Check if it's a plain object
29+
if (typeof hookDetails === 'object' && hookDetails.constructor === Object) {
30+
return Fn.toJsonString(hookDetails);
31+
}
32+
33+
// Everything else is invalid (primitives, functions, dates, etc.)
34+
throw new HookDetailsValidationError(`hookDetails must be a JSON object, got: ${typeof hookDetails}`);
35+
}
436

537
/**
638
* Deployment lifecycle stages where hooks can be executed
@@ -55,6 +87,12 @@ export interface DeploymentLifecycleHookTargetConfig {
5587
* The lifecycle stages when this hook should be executed
5688
*/
5789
readonly lifecycleStages: DeploymentLifecycleStage[];
90+
91+
/**
92+
* Use this field to specify custom parameters that Amazon ECS will pass to your hook target invocations (such as a Lambda function).
93+
* @default - No custom parameters will be passed
94+
*/
95+
readonly hookDetails?: string;
5896
}
5997

6098
/**
@@ -65,7 +103,6 @@ export interface IDeploymentLifecycleHookTarget {
65103
* Bind this target to a deployment lifecycle hook
66104
*
67105
* @param scope The construct scope
68-
* @param id A unique identifier for this binding
69106
*/
70107
bind(scope: IConstruct): DeploymentLifecycleHookTargetConfig;
71108
}
@@ -84,6 +121,19 @@ export interface DeploymentLifecycleLambdaTargetProps {
84121
* The lifecycle stages when this hook should be executed
85122
*/
86123
readonly lifecycleStages: DeploymentLifecycleStage[];
124+
125+
/**
126+
* Use this field to specify custom parameters that Amazon ECS will pass to your hook target invocations (such as a Lambda function).
127+
*
128+
* This field accepts JSON objects only
129+
*
130+
* @example
131+
* // JSON object
132+
* hookDetails: { environment: "production", timeout: 300 }
133+
*
134+
* @default - No custom parameters will be passed
135+
*/
136+
readonly hookDetails?: any;
87137
}
88138

89139
/**
@@ -122,6 +172,8 @@ export class DeploymentLifecycleLambdaTarget implements IDeploymentLifecycleHook
122172
targetArn: this.handler.functionArn,
123173
role: this._role,
124174
lifecycleStages: this.props.lifecycleStages,
175+
hookDetails: (this.props.hookDetails === undefined || this.props.hookDetails === null)?
176+
this.props.hookDetails : stringifyHookDetails(this.props.hookDetails),
125177
};
126178
}
127179
}

0 commit comments

Comments
 (0)