Skip to content

Commit 1c13faa

Browse files
authored
fix(stepfunctions): improve Task payload encoding (#2706)
Improve referencing data fields for StepFunctions tasks, in preparation of callback task implementaion. Get rid of `JsonPath`, and in its place we have 2 new classes: - `Data`, for fields that come from the user payload (`$.My.Field`). Settle on the term "data" since that's the term used in most of StepFunctions' docs. - `Context`, for fields that come from the service-defined task "context" (like `$$.Execution.StartTime`, and in particular `$$.Task.Token`). These classes have been moved from the `-tasks` module to the `aws-stepfunctions` module, where it seems to make more sense for them to live. Add support for SQS and SNS tasks to publish an arbitrary JSON structure that can reference fields from context and execution data. Remove `NumberValue` since we can now encode Tokens in regular number values. BREAKING CHANGES: - **stepfunctions**: `JsonPath.stringFromPath` (and others) are now called `Data.stringAt()`. The `DataField` class now lives in the main stepfunctions module. - **stepfunctions**: `PublishToTopic` property `messageObject` used to take a JSON string, now pass `sfn.TaskInput.fromObject()` or `sfn.TaskInput.fromText()` into the `message` field. - **stepfunctions**: `SendToQueue` property `messageBody` used to take a JSON string, now pass `sfn.TaskInput.fromObject()` or `sfn.TaskInput.fromText()` into the `message` field. - **stepfunctions**: Instead of passing `NumberValue`s to StepFunctions tasks, pass regular numbers.
1 parent ccf3636 commit 1c13faa

22 files changed

+739
-251
lines changed

packages/@aws-cdk/aws-stepfunctions-tasks/lib/index.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,4 @@ export * from './run-ecs-task-base-types';
55
export * from './publish-to-topic';
66
export * from './send-to-queue';
77
export * from './run-ecs-ec2-task';
8-
export * from './run-ecs-fargate-task';
9-
export * from './number-value';
10-
export * from './json-path';
8+
export * from './run-ecs-fargate-task';

packages/@aws-cdk/aws-stepfunctions-tasks/lib/json-path.ts

Lines changed: 0 additions & 119 deletions
This file was deleted.

packages/@aws-cdk/aws-stepfunctions-tasks/lib/number-value.ts

Lines changed: 0 additions & 53 deletions
This file was deleted.

packages/@aws-cdk/aws-stepfunctions-tasks/lib/publish-to-topic.ts

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,15 @@
11
import iam = require('@aws-cdk/aws-iam');
22
import sns = require('@aws-cdk/aws-sns');
33
import sfn = require('@aws-cdk/aws-stepfunctions');
4-
import cdk = require('@aws-cdk/cdk');
5-
import { renderString } from './json-path';
64

75
/**
86
* Properties for PublishTask
97
*/
108
export interface PublishToTopicProps {
119
/**
12-
* The text message to send to the queue.
13-
*
14-
* Exactly one of `message` and `messageObject` is required.
15-
*/
16-
readonly message?: string;
17-
18-
/**
19-
* Object to be JSON-encoded and used as message
20-
*
21-
* Exactly one of `message`, `messageObject` and `messagePath` is required.
10+
* The text message to send to the topic.
2211
*/
23-
readonly messageObject?: string;
12+
readonly message: sfn.TaskInput;
2413

2514
/**
2615
* If true, send a different message to every subscription type
@@ -48,12 +37,9 @@ export interface PublishToTopicProps {
4837
*/
4938
export class PublishToTopic implements sfn.IStepFunctionsTask {
5039
constructor(private readonly topic: sns.ITopic, private readonly props: PublishToTopicProps) {
51-
if ((props.message === undefined) === (props.messageObject === undefined)) {
52-
throw new Error(`Supply exactly one of 'message' or 'messageObject'`);
53-
}
5440
}
5541

56-
public bind(task: sfn.Task): sfn.StepFunctionsTaskProperties {
42+
public bind(_task: sfn.Task): sfn.StepFunctionsTaskProperties {
5743
return {
5844
resourceArn: 'arn:aws:states:::sns:publish',
5945
policyStatements: [new iam.PolicyStatement()
@@ -62,11 +48,11 @@ export class PublishToTopic implements sfn.IStepFunctionsTask {
6248
],
6349
parameters: {
6450
TopicArn: this.topic.topicArn,
65-
...(this.props.messageObject
66-
? { Message: new cdk.Token(() => task.node.stringifyJson(this.props.messageObject)) }
67-
: renderString('Message', this.props.message)),
68-
MessageStructure: this.props.messagePerSubscriptionType ? "json" : undefined,
69-
...renderString('Subject', this.props.subject),
51+
...sfn.FieldUtils.renderObject({
52+
Message: this.props.message.value,
53+
MessageStructure: this.props.messagePerSubscriptionType ? "json" : undefined,
54+
Subject: this.props.subject,
55+
})
7056
}
7157
};
7258
}

packages/@aws-cdk/aws-stepfunctions-tasks/lib/run-ecs-task-base-types.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import { NumberValue } from "./number-value";
2-
31
export interface ContainerOverride {
42
/**
53
* Name of the container inside the task definition
@@ -23,21 +21,21 @@ export interface ContainerOverride {
2321
*
2422
* @Default The default value from the task definition.
2523
*/
26-
readonly cpu?: NumberValue;
24+
readonly cpu?: number;
2725

2826
/**
2927
* Hard memory limit on the container
3028
*
3129
* @Default The default value from the task definition.
3230
*/
33-
readonly memoryLimit?: NumberValue;
31+
readonly memoryLimit?: number;
3432

3533
/**
3634
* Soft memory limit on the container
3735
*
3836
* @Default The default value from the task definition.
3937
*/
40-
readonly memoryReservation?: NumberValue;
38+
readonly memoryReservation?: number;
4139
}
4240

4341
/**

packages/@aws-cdk/aws-stepfunctions-tasks/lib/run-ecs-task-base.ts

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import ecs = require('@aws-cdk/aws-ecs');
33
import iam = require('@aws-cdk/aws-iam');
44
import sfn = require('@aws-cdk/aws-stepfunctions');
55
import cdk = require('@aws-cdk/cdk');
6-
import { renderNumber, renderString, renderStringList } from './json-path';
76
import { ContainerOverride } from './run-ecs-task-base-types';
87

98
/**
@@ -162,17 +161,17 @@ function renderOverrides(containerOverrides?: ContainerOverride[]) {
162161

163162
const ret = new Array<any>();
164163
for (const override of containerOverrides) {
165-
ret.push({
166-
...renderString('Name', override.containerName),
167-
...renderStringList('Command', override.command),
168-
...renderNumber('Cpu', override.cpu),
169-
...renderNumber('Memory', override.memoryLimit),
170-
...renderNumber('MemoryReservation', override.memoryReservation),
164+
ret.push(sfn.FieldUtils.renderObject({
165+
Name: override.containerName,
166+
Command: override.command,
167+
Cpu: override.cpu,
168+
Memory: override.memoryLimit,
169+
MemoryReservation: override.memoryReservation,
171170
Environment: override.environment && override.environment.map(e => ({
172-
...renderString('Name', e.name),
173-
...renderString('Value', e.value),
171+
Name: e.name,
172+
Value: e.value,
174173
}))
175-
});
174+
}));
176175
}
177176

178177
return { ContainerOverrides: ret };

packages/@aws-cdk/aws-stepfunctions-tasks/lib/send-to-queue.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
11
import iam = require('@aws-cdk/aws-iam');
22
import sqs = require('@aws-cdk/aws-sqs');
33
import sfn = require('@aws-cdk/aws-stepfunctions');
4-
import { renderNumber, renderString } from './json-path';
5-
import { NumberValue } from './number-value';
64

75
/**
86
* Properties for SendMessageTask
97
*/
108
export interface SendToQueueProps {
119
/**
12-
* The message body to send to the queue.
10+
* The text message to send to the queue.
1311
*/
14-
readonly messageBody: string;
12+
readonly messageBody: sfn.TaskInput;
1513

1614
/**
1715
* The length of time, in seconds, for which to delay a specific message.
@@ -20,7 +18,7 @@ export interface SendToQueueProps {
2018
*
2119
* @default Default value of the queue is used
2220
*/
23-
readonly delaySeconds?: NumberValue;
21+
readonly delaySeconds?: number;
2422

2523
/**
2624
* The token used for deduplication of sent messages.
@@ -59,10 +57,12 @@ export class SendToQueue implements sfn.IStepFunctionsTask {
5957
],
6058
parameters: {
6159
QueueUrl: this.queue.queueUrl,
62-
...renderString('MessageBody', this.props.messageBody),
63-
...renderNumber('DelaySeconds', this.props.delaySeconds),
64-
...renderString('MessageDeduplicationId', this.props.messageDeduplicationId),
65-
...renderString('MessageGroupId', this.props.messageGroupId),
60+
...sfn.FieldUtils.renderObject({
61+
MessageBody: this.props.messageBody.value,
62+
DelaySeconds: this.props.delaySeconds,
63+
MessageDeduplicationId: this.props.messageDeduplicationId,
64+
MessageGroupId: this.props.messageGroupId,
65+
})
6666
}
6767
};
6868
}

packages/@aws-cdk/aws-stepfunctions-tasks/test/ecs-tasks.test.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import ecs = require('@aws-cdk/aws-ecs');
44
import sfn = require('@aws-cdk/aws-stepfunctions');
55
import { Stack } from '@aws-cdk/cdk';
66
import tasks = require('../lib');
7-
import { JsonPath, NumberValue } from '../lib';
87

98
let stack: Stack;
109
let vpc: ec2.Vpc;
@@ -64,7 +63,7 @@ test('Running a Fargate Task', () => {
6463
{
6564
containerName: 'TheContainer',
6665
environment: [
67-
{name: 'SOME_KEY', value: JsonPath.stringFromPath('$.SomeKey')}
66+
{name: 'SOME_KEY', value: sfn.Data.stringAt('$.SomeKey')}
6867
]
6968
}
7069
]
@@ -162,7 +161,7 @@ test('Running an EC2 Task with bridge network', () => {
162161
{
163162
containerName: 'TheContainer',
164163
environment: [
165-
{name: 'SOME_KEY', value: JsonPath.stringFromPath('$.SomeKey')}
164+
{name: 'SOME_KEY', value: sfn.Data.stringAt('$.SomeKey')}
166165
]
167166
}
168167
]
@@ -296,9 +295,9 @@ test('Running an EC2 Task with overridden number values', () => {
296295
containerOverrides: [
297296
{
298297
containerName: 'TheContainer',
299-
command: JsonPath.listFromPath('$.TheCommand'),
300-
cpu: NumberValue.fromNumber(5),
301-
memoryLimit: JsonPath.numberFromPath('$.MemoryLimit'),
298+
command: sfn.Data.listAt('$.TheCommand'),
299+
cpu: 5,
300+
memoryLimit: sfn.Data.numberAt('$.MemoryLimit'),
302301
}
303302
]
304303
});

0 commit comments

Comments
 (0)