aws-cdk-lib
Version:
Version 2 of the AWS Cloud Development Kit library
1,185 lines (958 loc) • 71.2 kB
Markdown
# AWS Step Functions Construct Library
The `aws-cdk-lib/aws-stepfunctions` package contains constructs for building
serverless workflows using objects. Use this in conjunction with the
`aws-cdk-lib/aws-stepfunctions-tasks` package, which contains classes used
to call other AWS services.
Defining a workflow looks like this (for the [Step Functions Job Poller
example](https://docs.aws.amazon.com/step-functions/latest/dg/job-status-poller-sample.html)):
## State Machine
A `stepfunctions.StateMachine` is a resource that takes a state machine
definition. The definition is specified by its start state, and encompasses
all states reachable from the start state:
```ts
const startState = sfn.Pass.jsonata(this, 'StartState');
new sfn.StateMachine(this, 'StateMachine', {
definitionBody: sfn.DefinitionBody.fromChainable(startState),
});
```
State machines are made up of a sequence of **Steps**, which represent different actions
taken in sequence. Some of these steps represent *control flow* (like `Choice`, `Map` and `Wait`)
while others represent calls made against other AWS services (like `LambdaInvoke`).
The second category are called `Task`s and they can all be found in the module [`aws-stepfunctions-tasks`].
[`aws-stepfunctions-tasks`]: https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_stepfunctions_tasks-readme.html
State machines execute using an IAM Role, which will automatically have all
permissions added that are required to make all state machine tasks execute
properly (for example, permissions to invoke any Lambda functions you add to
your workflow). A role will be created by default, but you can supply an
existing one as well.
Set the `removalPolicy` prop to `RemovalPolicy.RETAIN` if you want to retain the execution
history when CloudFormation deletes your state machine.
Alternatively you can specify an existing step functions definition by providing a string or a file that contains the ASL JSON.
```ts
new sfn.StateMachine(this, 'StateMachineFromString', {
definitionBody: sfn.DefinitionBody.fromString('{"StartAt":"Pass","States":{"Pass":{"Type":"Pass","End":true}}}'),
});
new sfn.StateMachine(this, 'StateMachineFromFile', {
definitionBody: sfn.DefinitionBody.fromFile('./asl.json'),
});
```
## Query Language
Step Functions now provides the ability to select the `QueryLanguage` for the state machine or its states: `JSONata` or `JSONPath`.
For new state machines, we recommend using `JSONata` by specifying it at the state machine level.
If you do not specify a `QueryLanguage`, the state machine will default to using `JSONPath`.
If a state contains a specified `QueryLanguage`, Step Functions will use the specified query language for that state.
If the state does not specify a `QueryLanguage`, then it will use the query language specified in the state machine level, or `JSONPath` if none.
If the state machine level `QueryLanguage` is set to `JSONPath`, then any individual state-level `QueryLanguage` can be set to either `JSONPath` or `JSONata` to support incremental upgrades.
If the state-level `QueryLanguage` is set to `JSONata`, then any individual state-level `QueryLanguage` can either be `JSONata` or not set.
```ts
const jsonata = sfn.Pass.jsonata(this, 'JSONata');
const jsonPath = sfn.Pass.jsonPath(this, 'JSONPath');
const definition = jsonata.next(jsonPath);
new sfn.StateMachine(this, 'MixedStateMachine', {
// queryLanguage: sfn.QueryLanguage.JSON_PATH, // default
definitionBody: sfn.DefinitionBody.fromChainable(definition),
});
// This throws an error. If JSONata is specified at the top level, JSONPath cannot be used in the state machine definition.
new sfn.StateMachine(this, 'JSONataOnlyStateMachine', {
queryLanguage: sfn.QueryLanguage.JSONATA,
definitionBody: sfn.DefinitionBody.fromChainable(definition),
});
```
The AWS CDK defines state constructs, and there are 3 ways to initialize them.
| Method | Query Language | Description |
| ------ | ------- | ------- |
| `State.jsonata()` | `JSONata` | Use this method to specify a state definition using JSONata only fields. |
| `State.jsonPath()` | `JSONPath` | Use this method to specify a state definition using JSONPath only fields. |
| `new State()` | `JSONata` or `JSONPath` | This is a legacy pattern. Since fields for both JSONata and JSONPath can be used, it is recommended to use `State.jsonata()` or `State.jsonPath()` for better type safety and clarity. |
Code examples for initializing a `Pass` State with each pattern are shown below.
```ts
// JSONata Pattern
sfn.Pass.jsonata(this, 'JSONata Pattern', {
outputs: { foo: 'bar' },
// The outputPath does not exist in the props type
// outputPath: '$.status',
});
// JSONPath Pattern
sfn.Pass.jsonPath(this, 'JSONPath Pattern', {
// The outputs does not exist in the props type
// outputs: { foo: 'bar' },
outputPath: '$.status',
});
// Constructor (Legacy) Pattern
new sfn.Pass(this, 'Constructor Pattern', {
queryLanguage: sfn.QueryLanguage.JSONATA, // or JSON_PATH
// Both outputs and outputPath exist as prop types.
outputs: { foo: 'bar' }, // For JSONata
// or
outputPath: '$.status', // For JSONPath
});
```
Learn more in the blog post [Simplifying developer experience with variables and JSONata in AWS Step Functions](https://aws.amazon.com/jp/blogs/compute/simplifying-developer-experience-with-variables-and-jsonata-in-aws-step-functions/).
### JSONata example
The following example defines a state machine in `JSONata` that calls a fictional service API to update issue labels.
```ts
import * as events from 'aws-cdk-lib/aws-events';
declare const connection: events.Connection;
const getIssue = tasks.HttpInvoke.jsonata(this, 'Get Issue', {
connection,
apiRoot: "{% 'https://' & $states.input.hostname %}",
apiEndpoint: sfn.TaskInput.fromText("{% 'issues/' & $states.input.issue.id %}"),
method: sfn.TaskInput.fromText('GET'),
// Parse the API call result to object and set to the variables
assign: {
hostname: '{% $states.input.hostname %}',
issue: '{% $parse($states.result.ResponseBody) %}',
},
});
const updateLabels = tasks.HttpInvoke.jsonata(this, 'Update Issue Labels', {
connection,
apiRoot: "{% 'https://' & $states.input.hostname %}",
apiEndpoint: sfn.TaskInput.fromText("{% 'issues/' & $states.input.issue.id & 'labels' %}"),
method: sfn.TaskInput.fromText('POST'),
body: sfn.TaskInput.fromObject({
labels: '{% [$type, $component] %}',
}),
});
const notMatchTitleTemplate = sfn.Pass.jsonata(this, 'Not Match Title Template');
const definition = getIssue
.next(sfn.Choice.jsonata(this, 'Match Title Template?')
// Look at the "title" field of issue in variables using Regex
.when(sfn.Condition.jsonata('{% $contains($issue.title, /(feat)|(fix)|(chore)\(\w*\):.*/) %}'), updateLabels, {
assign: {
type: '{% $match($states.input.title, /(\w*)\((.*)\)/).groups[0] %}',
component: '{% $match($states.input.title, /(\w*)\((.*)\)/).groups[1] %}',
}
})
.otherwise(notMatchTitleTemplate));
new sfn.StateMachine(this, 'StateMachine', {
definitionBody: sfn.DefinitionBody.fromChainable(definition),
timeout: Duration.minutes(5),
comment: 'automate issue labeling state machine',
});
```
### JSONPath (Legacy pattern) example
Defining a workflow looks like this (for the [Step Functions Job Poller
example](https://docs.aws.amazon.com/step-functions/latest/dg/sample-project-job-poller.html)):
```ts
import * as lambda from 'aws-cdk-lib/aws-lambda';
declare const submitLambda: lambda.Function;
declare const getStatusLambda: lambda.Function;
const submitJob = new tasks.LambdaInvoke(this, 'Submit Job', {
lambdaFunction: submitLambda,
// Lambda's result is in the attribute `guid`
outputPath: '$.guid',
});
const waitX = new sfn.Wait(this, 'Wait X Seconds', {
time: sfn.WaitTime.secondsPath('$.waitSeconds'),
});
const getStatus = new tasks.LambdaInvoke(this, 'Get Job Status', {
lambdaFunction: getStatusLambda,
// Pass just the field named "guid" into the Lambda, put the
// Lambda's result in a field called "status" in the response
inputPath: '$.guid',
outputPath: '$.status',
});
const jobFailed = new sfn.Fail(this, 'Job Failed', {
cause: 'AWS Batch Job Failed',
error: 'DescribeJob returned FAILED',
});
const finalStatus = new tasks.LambdaInvoke(this, 'Get Final Job Status', {
lambdaFunction: getStatusLambda,
// Use "guid" field as input
inputPath: '$.guid',
outputPath: '$.Payload',
});
const definition = submitJob
.next(waitX)
.next(getStatus)
.next(new sfn.Choice(this, 'Job Complete?')
// Look at the "status" field
.when(sfn.Condition.stringEquals('$.status', 'FAILED'), jobFailed)
.when(sfn.Condition.stringEquals('$.status', 'SUCCEEDED'), finalStatus)
.otherwise(waitX));
new sfn.StateMachine(this, 'StateMachine', {
definitionBody: sfn.DefinitionBody.fromChainable(definition),
timeout: Duration.minutes(5),
comment: 'a super cool state machine',
});
```
You can find more sample snippets and learn more about the service integrations
in the `aws-cdk-lib/aws-stepfunctions-tasks` package.
## State Machine Data
With variables and state output, you can pass data between the steps of your workflow.
Using workflow variables, you can store data in a step and retrieve that data in future steps. For example, you could store an API response that contains data you might need later. Conversely, state output can only be used as input to the very next step.
### Variable
With workflow variables, you can store data to reference later. For example, Step 1 might store the result from an API request so a part of that request can be re-used later in Step 5.
In the following scenario, the state machine fetches data from an API once. In Step 1, the workflow stores the returned API data (up to 256 KiB per state) in a variable ‘x’ to use in later steps.
Without variables, you would need to pass the data through output from Step 1 to Step 2 to Step 3 to Step 4 to use it in Step 5. What if those intermediate steps do not need the data? Passing data from state to state through outputs and input would be unnecessary effort.
With variables, you can store data and use it in any future step. You can also modify, rearrange, or add steps without disrupting the flow of your data. Given the flexibility of variables, you might only need to use output to return data from `Parallel` and `Map` sub-workflows, and at the end of your state machine execution.
```txt
(Start)
↓
[Step 1]→[Return from API]
↓ ↓
[Step 2] [Assign data to $x]
↓ │
[Step 3] │
↓ │
[Step 4] │
↓ │
[Step 5]←────────┘
↓ Use variable $x
(End)
```
Use `assign` to express the above example in AWS CDK. You can use both JSONata and JSONPath to assign.
```ts
import * as lambda from 'aws-cdk-lib/aws-lambda';
declare const callApiFunc: lambda.Function;
declare const useVariableFunc: lambda.Function;
const step1 = tasks.LambdaInvoke.jsonata(this, 'Step 1', {
lambdaFunction: callApiFunc,
assign: {
x: '{% $states.result.Payload.x %}', // JSONata
// x: sfn.JsonPath.stringAt('$.Payload.x'), // When use JSONPath
},
});
const step2 = sfn.Pass.jsonata(this, 'Step 2');
const step3 = sfn.Pass.jsonata(this, 'Step 3');
const step4 = sfn.Pass.jsonata(this, 'Step 4');
const step5 = tasks.LambdaInvoke.jsonata(this, 'Step 5', {
lambdaFunction: useVariableFunc,
payload: sfn.TaskInput.fromObject({
x: '{% $x %}', // JSONata
// x: sfn.JsonPath.stringAt('$x'), // When use JSONPath
}),
});
```
For more details, see the [official documentation](https://docs.aws.amazon.com/step-functions/latest/dg/workflow-variables.html)
### State Output
An Execution represents each time the State Machine is run. Every Execution has [State Machine
Data](https://docs.aws.amazon.com/step-functions/latest/dg/concepts-state-machine-data.html):
a JSON document containing keys and values that is fed into the state machine,
gets modified by individual steps as the state machine progresses, and finally
is produced as output.
By default, the entire Data object is passed into every state, and the return data of the step
becomes new the new Data object. You can change this behavior, but the way you specify it differs depending on the query language you use.
#### JSONata
To change the default behavior of using a JSONata, supplying values for `outputs`. When a string in the value of an ASL field, a JSON object field, or a JSON array element is surrounded by `{% %}` characters, that string will be evaluated as JSONata . Note, the string must start with `{%` with no leading spaces, and must end with `%}` with no trailing spaces. Improperly opening or closing the expression will result in a validation error.
The following example uses JSONata expressions for `outputs` and `time`.
```ts
sfn.Wait.jsonata(this, 'Wait', {
time: sfn.WaitTime.timestamp('{% $timestamp %}'),
outputs: {
stringArgument: 'inital-task',
numberArgument: 123,
booleanArgument: true,
arrayArgument: [1, '{% $number %}', 3],
intrinsicFunctionsArgument: "{% $join($each($obj, function($v) { $v }), ', ') %}",
},
});
```
For a brief introduction and complete JSONata reference, see [JSONata.org documentation](https://docs.jsonata.org/overview.html).
##### Reserved variable : $states
Step Functions defines a single reserved variable called $states. In JSONata states, the following structures are assigned to $states for use in JSONata expressions:
```js
// Reserved $states variable in JSONata states
const $states = {
"input": // Original input to the state
"result": // API or sub-workflow's result (if successful)
"errorOutput":// Error Output in a Catch
"context": // Context object
}
```
The structure is as follows when expressed as a type.
```ts
interface JsonataStates {
input: any;
result?: any;
errorOutput?: {
Error: string;
Cause: string;
};
context: {
Execution: {
Id: string;
Input: any;
Name: string;
RoleArn: string;
StartTime: string; // Format: ISO 8601;
RedriveCount: number;
RedriveTime: string; // Format: ISO 8601
};
State: {
EnteredTime: string; // Format: ISO 8601;
Name: string;
RetryCount: number;
};
StateMachine: {
Id: string;
Name: string;
};
Task: {
Token: string;
};
};
}
```
You can access reserved variables as follows:
```ts
sfn.Pass.jsonata(this, 'Pass', {
outputs: {
foo: '{% $states.input.foo %}',
},
});
```
#### JSONPath
To change the default behavior of using a JSON path, supplying values for `inputPath`, `resultSelector`, `resultPath`, and `outputPath`.
##### Manipulating state machine data using inputPath, resultSelector, resultPath and outputPath
These properties impact how each individual step interacts with the state machine data:
* `stateName`: the name of the state in the state machine definition. If not supplied, defaults to the construct id.
* `inputPath`: the part of the data object that gets passed to the step (`itemsPath` for `Map` states)
* `resultSelector`: the part of the step result that should be added to the state machine data
* `resultPath`: where in the state machine data the step result should be inserted
* `outputPath`: what part of the state machine data should be retained
* `errorPath`: the part of the data object that gets returned as the step error
* `causePath`: the part of the data object that gets returned as the step cause
Their values should be a string indicating a [JSON path](https://docs.aws.amazon.com/step-functions/latest/dg/amazon-states-language-paths.html) into the State Machine Data object (like `"$.MyKey"`). If absent, the values are treated as if they were `"$"`, which means the entire object.
The following pseudocode shows how AWS Step Functions uses these parameters when executing a step:
```js
// Schematically show how Step Functions evaluates functions.
// [] represents indexing into an object by a using JSON path.
input = state[inputPath]
result = invoke_step(select_parameters(input))
state[resultPath] = result[resultSelector]
state = state[outputPath]
```
Instead of a JSON path string, each of these paths can also have the special value `JsonPath.DISCARD`, which causes the corresponding indexing expression to return an empty object (`{}`). Effectively, that means there will be an empty input object, an empty result object, no effect on the state, or an empty state, respectively.
Some steps (mostly Tasks) have *Parameters*, which are selected differently. See the next section.
See the official documentation on [input and output processing in Step Functions](https://docs.aws.amazon.com/step-functions/latest/dg/concepts-input-output-filtering.html).
### Passing Parameters to Tasks
Tasks take parameters, whose values can be taken from the State Machine Data object. For example, your
workflow may want to start a CodeBuild with an environment variable that is taken from the State Machine data, or pass part of the State Machine Data into an AWS Lambda Function.
In the original JSON-based states language used by AWS Step Functions, you would
add `.$` to the end of a key to indicate that a value needs to be interpreted as
a JSON path. In the CDK API you do not change the names of any keys. Instead, you
pass special values. There are 3 types of task inputs to consider:
* Tasks that accept a "payload" type of input (like AWS Lambda invocations, or posting messages to SNS topics or SQS queues), will take an object of type `TaskInput`, like `TaskInput.fromObject()` or `TaskInput.fromJsonPathAt()`.
* When tasks expect individual string or number values to customize their behavior, you can also pass a value constructed by `JsonPath.stringAt()` or `JsonPath.numberAt()`.
* When tasks expect strongly-typed resources and you want to vary the resource that is referenced based on a name from the State Machine Data, reference the resource as if it was external (using `JsonPath.stringAt()`). For example, for a Lambda function: `Function.fromFunctionName(this, 'ReferencedFunction', JsonPath.stringAt('$.MyFunctionName'))`.
For example, to pass the value that's in the data key of `OrderId` to a Lambda
function as you invoke it, use `JsonPath.stringAt('$.OrderId')`, like so:
```ts
import * as lambda from 'aws-cdk-lib/aws-lambda';
declare const orderFn: lambda.Function;
const submitJob = new tasks.LambdaInvoke(this, 'InvokeOrderProcessor', {
lambdaFunction: orderFn,
payload: sfn.TaskInput.fromObject({
OrderId: sfn.JsonPath.stringAt('$.OrderId'),
}),
});
```
The following methods are available:
| Method | Purpose |
|--------|---------|
| `JsonPath.stringAt('$.Field')` | reference a field, return the type as a `string`. |
| `JsonPath.listAt('$.Field')` | reference a field, return the type as a list of strings. |
| `JsonPath.numberAt('$.Field')` | reference a field, return the type as a number. Use this for functions that expect a number argument. |
| `JsonPath.objectAt('$.Field')` | reference a field, return the type as an `IResolvable`. Use this for functions that expect an object argument. |
| `JsonPath.entirePayload` | reference the entire data object (equivalent to a path of `$`). |
| `JsonPath.taskToken` | reference the [Task Token](https://docs.aws.amazon.com/step-functions/latest/dg/connect-to-resource.html#connect-wait-token), used for integration patterns that need to run for a long time. |
| `JsonPath.executionId` | reference the Execution Id field of the context object. |
| `JsonPath.executionInput` | reference the Execution Input object of the context object. |
| `JsonPath.executionName` | reference the Execution Name field of the context object. |
| `JsonPath.executionRoleArn` | reference the Execution RoleArn field of the context object. |
| `JsonPath.executionStartTime` | reference the Execution StartTime field of the context object. |
| `JsonPath.stateEnteredTime` | reference the State EnteredTime field of the context object. |
| `JsonPath.stateName` | reference the State Name field of the context object. |
| `JsonPath.stateRetryCount` | reference the State RetryCount field of the context object. |
| `JsonPath.stateMachineId` | reference the StateMachine Id field of the context object. |
| `JsonPath.stateMachineName` | reference the StateMachine Name field of the context object. |
You can also call [intrinsic functions](https://docs.aws.amazon.com/step-functions/latest/dg/amazon-states-language-intrinsic-functions.html) using the methods on `JsonPath`:
| Method | Purpose |
|--------|---------|
| `JsonPath.array(JsonPath.stringAt('$.Field'), ...)` | make an array from other elements. |
| `JsonPath.arrayPartition(JsonPath.listAt('$.inputArray'), 4)` | partition an array. |
| `JsonPath.arrayContains(JsonPath.listAt('$.inputArray'), 5)` | determine if a specific value is present in an array. |
| `JsonPath.arrayRange(1, 9, 2)` | create a new array containing a specific range of elements. |
| `JsonPath.arrayGetItem(JsonPath.listAt('$.inputArray'), 5)` | get a specified index's value in an array. |
| `JsonPath.arrayLength(JsonPath.listAt('$.inputArray'))` | get the length of an array. |
| `JsonPath.arrayUnique(JsonPath.listAt('$.inputArray'))` | remove duplicate values from an array. |
| `JsonPath.base64Encode(JsonPath.stringAt('$.input'))` | encode data based on MIME Base64 encoding scheme. |
| `JsonPath.base64Decode(JsonPath.stringAt('$.base64'))` | decode data based on MIME Base64 decoding scheme. |
| `JsonPath.hash(JsonPath.objectAt('$.Data'), JsonPath.stringAt('$.Algorithm'))` | calculate the hash value of a given input. |
| `JsonPath.jsonMerge(JsonPath.objectAt('$.Obj1'), JsonPath.objectAt('$.Obj2'))` | merge two JSON objects into a single object. |
| `JsonPath.stringToJson(JsonPath.stringAt('$.ObjStr'))` | parse a JSON string to an object |
| `JsonPath.jsonToString(JsonPath.objectAt('$.Obj'))` | stringify an object to a JSON string |
| `JsonPath.mathRandom(1, 999)` | return a random number. |
| `JsonPath.mathAdd(JsonPath.numberAt('$.value1'), JsonPath.numberAt('$.step'))` | return the sum of two numbers. |
| `JsonPath.stringSplit(JsonPath.stringAt('$.inputString'), JsonPath.stringAt('$.splitter'))` | split a string into an array of values. |
| `JsonPath.uuid()` | return a version 4 universally unique identifier (v4 UUID). |
| `JsonPath.format('The value is {}.', JsonPath.stringAt('$.Value'))` | insert elements into a format string. |
## Amazon States Language
This library comes with a set of classes that model the [Amazon States
Language](https://states-language.net/spec.html). The following State classes
are supported:
* [`Task`](#task)
* [`Pass`](#pass)
* [`Wait`](#wait)
* [`Choice`](#choice)
* [`Parallel`](#parallel)
* [`Succeed`](#succeed)
* [`Fail`](#fail)
* [`Map`](#map)
* [`Distributed Map`](#distributed-map)
* [`Custom State`](#custom-state)
An arbitrary JSON object (specified at execution start) is passed from state to
state and transformed during the execution of the workflow. For more
information, see the States Language spec.
### Task
A `Task` represents some work that needs to be done. Do not use the `Task` class directly.
Instead, use one of the classes in the `aws-cdk-lib/aws-stepfunctions-tasks` module,
which provide a much more ergonomic way to integrate with various AWS services.
### Pass
A `Pass` state passes its input to its output, without performing work.
Pass states are useful when constructing and debugging state machines.
The following example injects some fixed data into the state machine through
the `result` field. The `result` field will be added to the input and the result
will be passed as the state's output.
```ts
// Makes the current JSON state { ..., "subObject": { "hello": "world" } }
const pass = new sfn.Pass(this, 'Add Hello World', {
result: sfn.Result.fromObject({ hello: 'world' }),
resultPath: '$.subObject',
});
// Set the next state
const nextState = new sfn.Pass(this, 'NextState');
pass.next(nextState);
```
When using JSONata, you can use only `outputs`.
```ts
const pass = new sfn.Pass(this, 'Add Hello World', {
outputs: {
subObject: { hello: 'world' }
},
});
```
The `Pass` state also supports passing key-value pairs as input. Values can
be static, or selected from the input with a path.
The following example filters the `greeting` field from the state input
and also injects a field called `otherData`.
```ts
const pass = new sfn.Pass(this, 'Filter input and inject data', {
stateName: 'my-pass-state', // the custom state name for the Pass state, defaults to 'Filter input and inject data' as the state name
parameters: { // input to the pass state
input: sfn.JsonPath.stringAt('$.input.greeting'),
otherData: 'some-extra-stuff',
},
});
```
The object specified in `parameters` will be the input of the `Pass` state.
Since neither `Result` nor `ResultPath` are supplied, the `Pass` state copies
its input through to its output.
Learn more about the [Pass state](https://docs.aws.amazon.com/step-functions/latest/dg/amazon-states-language-pass-state.html)
### Wait
A `Wait` state waits for a given number of seconds, or until the current time
hits a particular time. The time to wait may be taken from the execution's JSON
state.
```ts
// Wait until it's the time mentioned in the the state object's "triggerTime"
// field.
const wait = new sfn.Wait(this, 'Wait For Trigger Time', {
time: sfn.WaitTime.timestampPath('$.triggerTime'),
});
// When using JSONata
// const wait = sfn.Wait.jsonata(this, 'Wait For Trigger Time', {
// time: sfn.WaitTime.timestamp('{% $triggerTime %}'),
// });
// Set the next state
const startTheWork = new sfn.Pass(this, 'StartTheWork');
wait.next(startTheWork);
```
### Choice
A `Choice` state can take a different path through the workflow based on the
values in the execution's JSON state:
```ts
const choice = new sfn.Choice(this, 'Did it work?');
// Add conditions with .when()
const successState = new sfn.Pass(this, 'SuccessState');
const failureState = new sfn.Pass(this, 'FailureState');
choice.when(sfn.Condition.stringEquals('$.status', 'SUCCESS'), successState);
choice.when(sfn.Condition.numberGreaterThan('$.attempts', 5), failureState);
// When using JSONata
// choice.when(sfn.Condition.jsonata("{% $status = 'SUCCESS'"), successState);
// choice.when(sfn.Condition.jsonata('{% $attempts > 5 %}'), failureState);
// Use .otherwise() to indicate what should be done if none of the conditions match
const tryAgainState = new sfn.Pass(this, 'TryAgainState');
choice.otherwise(tryAgainState);
```
If you want to temporarily branch your workflow based on a condition, but have
all branches come together and continuing as one (similar to how an `if ...
then ... else` works in a programming language), use the `.afterwards()` method:
```ts
const choice = new sfn.Choice(this, 'What color is it?');
const handleBlueItem = new sfn.Pass(this, 'HandleBlueItem');
const handleRedItem = new sfn.Pass(this, 'HandleRedItem');
const handleOtherItemColor = new sfn.Pass(this, 'HanldeOtherItemColor');
choice.when(sfn.Condition.stringEquals('$.color', 'BLUE'), handleBlueItem);
choice.when(sfn.Condition.stringEquals('$.color', 'RED'), handleRedItem);
choice.otherwise(handleOtherItemColor);
// Use .afterwards() to join all possible paths back together and continue
const shipTheItem = new sfn.Pass(this, 'ShipTheItem');
choice.afterwards().next(shipTheItem);
```
You can add comments to `Choice` states as well as conditions that use `choice.when`.
```ts
const choice = new sfn.Choice(this, 'What color is it?', {
comment: 'color comment',
});
const handleBlueItem = new sfn.Pass(this, 'HandleBlueItem');
const handleOtherItemColor = new sfn.Pass(this, 'HanldeOtherItemColor');
choice.when(sfn.Condition.stringEquals('$.color', 'BLUE'), handleBlueItem, {
comment: 'blue item comment',
});
choice.otherwise(handleOtherItemColor);
```
If your `Choice` doesn't have an `otherwise()` and none of the conditions match
the JSON state, a `NoChoiceMatched` error will be thrown. Wrap the state machine
in a `Parallel` state if you want to catch and recover from this.
#### Available Conditions
#### JSONata
When you're using JSONata, use the `jsonata` function to specify the condition using a JSONata expression:
```ts
sfn.Condition.jsonata('{% 1+1 = 2 %}'); // true
sfn.Condition.jsonata('{% 1+1 != 3 %}'); // true
sfn.Condition.jsonata("{% 'world' in ['hello', 'world'] %}"); // true
sfn.Condition.jsonata("{% $contains('abracadabra', /a.*a/) %}"); // true
```
See the [JSONata comparison operators](https://docs.jsonata.org/comparison-operators) to find more operators.
##### JSONPath
see [step function comparison operators](https://docs.aws.amazon.com/step-functions/latest/dg/amazon-states-language-choice-state.html#amazon-states-language-choice-state-rules)
* `Condition.isPresent` - matches if a json path is present
* `Condition.isNotPresent` - matches if a json path is not present
* `Condition.isString` - matches if a json path contains a string
* `Condition.isNotString` - matches if a json path is not a string
* `Condition.isNumeric` - matches if a json path is numeric
* `Condition.isNotNumeric` - matches if a json path is not numeric
* `Condition.isBoolean` - matches if a json path is boolean
* `Condition.isNotBoolean` - matches if a json path is not boolean
* `Condition.isTimestamp` - matches if a json path is a timestamp
* `Condition.isNotTimestamp` - matches if a json path is not a timestamp
* `Condition.isNotNull` - matches if a json path is not null
* `Condition.isNull` - matches if a json path is null
* `Condition.booleanEquals` - matches if a boolean field has a given value
* `Condition.booleanEqualsJsonPath` - matches if a boolean field equals a value in a given mapping path
* `Condition.stringEqualsJsonPath` - matches if a string field equals a given mapping path
* `Condition.stringEquals` - matches if a field equals a string value
* `Condition.stringLessThan` - matches if a string field sorts before a given value
* `Condition.stringLessThanJsonPath` - matches if a string field sorts before a value at given mapping path
* `Condition.stringLessThanEquals` - matches if a string field sorts equal to or before a given value
* `Condition.stringLessThanEqualsJsonPath` - matches if a string field sorts equal to or before a given mapping
* `Condition.stringGreaterThan` - matches if a string field sorts after a given value
* `Condition.stringGreaterThanJsonPath` - matches if a string field sorts after a value at a given mapping path
* `Condition.stringGreaterThanEqualsJsonPath` - matches if a string field sorts after or equal to value at a given mapping path
* `Condition.stringGreaterThanEquals` - matches if a string field sorts after or equal to a given value
* `Condition.numberEquals` - matches if a numeric field has the given value
* `Condition.numberEqualsJsonPath` - matches if a numeric field has the value in a given mapping path
* `Condition.numberLessThan` - matches if a numeric field is less than the given value
* `Condition.numberLessThanJsonPath` - matches if a numeric field is less than the value at the given mapping path
* `Condition.numberLessThanEquals` - matches if a numeric field is less than or equal to the given value
* `Condition.numberLessThanEqualsJsonPath` - matches if a numeric field is less than or equal to the numeric value at given mapping path
* `Condition.numberGreaterThan` - matches if a numeric field is greater than the given value
* `Condition.numberGreaterThanJsonPath` - matches if a numeric field is greater than the value at a given mapping path
* `Condition.numberGreaterThanEquals` - matches if a numeric field is greater than or equal to the given value
* `Condition.numberGreaterThanEqualsJsonPath` - matches if a numeric field is greater than or equal to the value at a given mapping path
* `Condition.timestampEquals` - matches if a timestamp field is the same time as the given timestamp
* `Condition.timestampEqualsJsonPath` - matches if a timestamp field is the same time as the timestamp at a given mapping path
* `Condition.timestampLessThan` - matches if a timestamp field is before the given timestamp
* `Condition.timestampLessThanJsonPath` - matches if a timestamp field is before the timestamp at a given mapping path
* `Condition.timestampLessThanEquals` - matches if a timestamp field is before or equal to the given timestamp
* `Condition.timestampLessThanEqualsJsonPath` - matches if a timestamp field is before or equal to the timestamp at a given mapping path
* `Condition.timestampGreaterThan` - matches if a timestamp field is after the timestamp at a given mapping path
* `Condition.timestampGreaterThanJsonPath` - matches if a timestamp field is after the timestamp at a given mapping path
* `Condition.timestampGreaterThanEquals` - matches if a timestamp field is after or equal to the given timestamp
* `Condition.timestampGreaterThanEqualsJsonPath` - matches if a timestamp field is after or equal to the timestamp at a given mapping path
* `Condition.stringMatches` - matches if a field matches a string pattern that can contain a wild card (\*) e.g: log-\*.txt or \*LATEST\*. No other characters other than "\*" have any special meaning - \* can be escaped: \\\\*
### Parallel
A `Parallel` state executes one or more subworkflows in parallel. It can also
be used to catch and recover from errors in subworkflows. The `parameters` property
can be used to transform the input that is passed to each branch of the parallel execution.
```ts
const parallel = new sfn.Parallel(this, 'Do the work in parallel');
// Add branches to be executed in parallel
const shipItem = new sfn.Pass(this, 'ShipItem');
const sendInvoice = new sfn.Pass(this, 'SendInvoice');
const restock = new sfn.Pass(this, 'Restock');
parallel.branch(shipItem);
parallel.branch(sendInvoice);
parallel.branch(restock);
// Retry the whole workflow if something goes wrong with exponential backoff
parallel.addRetry({
maxAttempts: 1,
maxDelay: Duration.seconds(5),
jitterStrategy: sfn.JitterType.FULL,
});
// How to recover from errors
const sendFailureNotification = new sfn.Pass(this, 'SendFailureNotification');
parallel.addCatch(sendFailureNotification);
// What to do in case everything succeeded
const closeOrder = new sfn.Pass(this, 'CloseOrder');
parallel.next(closeOrder);
```
### Succeed
Reaching a `Succeed` state terminates the state machine execution with a
successful status.
```ts
const success = new sfn.Succeed(this, 'We did it!');
```
### Fail
Reaching a `Fail` state terminates the state machine execution with a
failure status. The fail state should report the reason for the failure.
Failures can be caught by encompassing `Parallel` states.
```ts
const fail = new sfn.Fail(this, 'Fail', {
error: 'WorkflowFailure',
cause: "Something went wrong",
});
```
The `Fail` state also supports returning dynamic values as the error and cause that are selected from the input with a path.
```ts
const fail = new sfn.Fail(this, 'Fail', {
errorPath: sfn.JsonPath.stringAt('$.someError'),
causePath: sfn.JsonPath.stringAt('$.someCause'),
});
```
You can also use an intrinsic function that returns a string to specify CausePath and ErrorPath.
The available functions include States.Format, States.JsonToString, States.ArrayGetItem, States.Base64Encode, States.Base64Decode, States.Hash, and States.UUID.
```ts
const fail = new sfn.Fail(this, 'Fail', {
errorPath: sfn.JsonPath.format('error: {}.', sfn.JsonPath.stringAt('$.someError')),
causePath: "States.Format('cause: {}.', $.someCause)",
});
```
When you use JSONata, you can use JSONata expression in the `error` or `cause` properties.
```ts
const fail = new sfn.Fail(this, 'Fail', {
error: "{% 'error:' & $someError & '.' %}",
cause: "{% 'cause:' & $someCause & '.' %}",
});
```
### Map
A `Map` state can be used to run a set of steps for each element of an input array.
A `Map` state will execute the same steps for multiple entries of an array in the state input.
While the `Parallel` state executes multiple branches of steps using the same input, a `Map` state will
execute the same steps for multiple entries of an array in the state input.
```ts
const map = new sfn.Map(this, 'Map State', {
maxConcurrency: 1,
itemsPath: sfn.JsonPath.stringAt('$.inputForMap'),
itemSelector: {
item: sfn.JsonPath.stringAt('$$.Map.Item.Value'),
},
resultPath: '$.mapOutput',
});
// The Map iterator can contain a IChainable, which can be an individual or multiple steps chained together.
// Below example is with a Choice and Pass step
const choice = new sfn.Choice(this, 'Choice');
const condition1 = sfn.Condition.stringEquals('$.item.status', 'SUCCESS');
const step1 = new sfn.Pass(this, 'Step1');
const step2 = new sfn.Pass(this, 'Step2');
const finish = new sfn.Pass(this, 'Finish');
const definition = choice
.when(condition1, step1)
.otherwise(step2)
.afterwards()
.next(finish);
map.itemProcessor(definition);
```
When using `JSONata`, the `itemSelector` property in a Map state can be specified in one of two ways. You can provide a valid JSON object containing JSONata expressions for each value:
```ts
const map = new sfn.Map(this, 'Map State', {
maxConcurrency: 1,
itemSelector: {
id: '{% $states.context.Map.Item.Value.id %}',
status: '{% $states.context.Map.Item.Value.status %}',
}
});
```
Alternatively, you can use the `jsonataItemSelector` field to directly supply a JSONata string that evaluates to a complete JSON object:
```ts
const map = new sfn.Map(this, 'Map State', {
maxConcurrency: 1,
jsonataItemSelector: '{% {\"id\": $states.input.id, \"status\": $states.input.status} %}'
});
```
To define a distributed `Map` state set `itemProcessors` mode to `ProcessorMode.DISTRIBUTED`.
An `executionType` must be specified for the distributed `Map` workflow.
```ts
const map = new sfn.Map(this, 'Map State', {
maxConcurrency: 1,
itemsPath: sfn.JsonPath.stringAt('$.inputForMap'),
itemSelector: {
item: sfn.JsonPath.stringAt('$$.Map.Item.Value'),
},
resultPath: '$.mapOutput',
});
map.itemProcessor(new sfn.Pass(this, 'Pass State'), {
mode: sfn.ProcessorMode.DISTRIBUTED,
executionType: sfn.ProcessorType.STANDARD,
});
```
> Visit [Using Map state in Distributed mode to orchestrate large-scale parallel workloads](https://docs.aws.amazon.com/step-functions/latest/dg/use-dist-map-orchestrate-large-scale-parallel-workloads.html) for more details.
### Distributed Map
Step Functions provides a high-concurrency mode for the Map state known as Distributed mode. In this mode, the Map state can accept input from large-scale Amazon S3 data sources. For example, your input can be a JSON or CSV file stored in an Amazon S3 bucket, or a JSON array passed from a previous step in the workflow. A Map state set to Distributed is known as a Distributed Map state. In this mode, the Map state runs each iteration as a child workflow execution, which enables high concurrency of up to 10,000 parallel child workflow executions. Each child workflow execution has its own, separate execution history from that of the parent workflow.
Use the Map state in Distributed mode when you need to orchestrate large-scale parallel workloads that meet any combination of the following conditions:
* The size of your dataset exceeds 256 KB.
* The workflow's execution event history exceeds 25,000 entries.
* You need a concurrency of more than 40 parallel iterations.
A `DistributedMap` state can be used to run a set of steps for each element of an input array with high concurrency.
A `DistributedMap` state will execute the same steps for multiple entries of an array in the state input or from S3 objects.
```ts
const distributedMap = new sfn.DistributedMap(this, 'Distributed Map State', {
maxConcurrency: 1,
itemsPath: sfn.JsonPath.stringAt('$.inputForMap'),
});
distributedMap.itemProcessor(new sfn.Pass(this, 'Pass State'));
```
`DistributedMap` supports various input source types to determine a list of objects to iterate over:
* JSON array from the JSON state input
* By default, `DistributedMap` assumes whole JSON state input is an JSON array and iterates over it:
```ts
/**
* JSON state input:
* [
* "item1",
* "item2"
* ]
*/
const distributedMap = new sfn.DistributedMap(this, 'DistributedMap');
distributedMap.itemProcessor(new sfn.Pass(this, 'Pass'));
```
* When input source is present at a specific path in JSON state input, then `itemsPath` can be utilised to configure the iterator source.
```ts
/**
* JSON state input:
* {
* "distributedMapItemList": [
* "item1",
* "item2"
* ]
* }
*/
const distributedMap = new sfn.DistributedMap(this, 'DistributedMap', {
itemsPath: '$.distributedMapItemList',
});
distributedMap.itemProcessor(new sfn.Pass(this, 'Pass'));
```
* Objects in a S3 bucket with an optional prefix.
* When `DistributedMap` is required to iterate over objects stored in a S3 bucket, then an object of `S3ObjectsItemReader` can be passed to `itemReader` to configure the iterator source. Note that `S3ObjectsItemReader` will default to use Distributed map's query language. If the
map does not specify a query language, then it falls back to the State machine's query language. An exmaple of using `S3ObjectsItemReader`
is as follows:
```ts
import * as s3 from 'aws-cdk-lib/aws-s3';
/**
* Tree view of bucket:
* my-bucket
* |
* +--item1
* |
* +--otherItem
* |
* +--item2
* |
* ...
*/
const bucket = new s3.Bucket(this, 'Bucket', {
bucketName: 'my-bucket',
});
const distributedMap = new sfn.DistributedMap(this, 'DistributedMap', {
itemReader: new sfn.S3ObjectsItemReader({
bucket,
prefix: 'item',
}),
});
distributedMap.itemProcessor(new sfn.Pass(this, 'Pass'));
```
* If information about `bucket` is only known while starting execution of `StateMachine` (dynamically or at run-time) via JSON state input:
```ts
/**
* JSON state input:
* {
* "bucketName": "my-bucket",
* "prefix": "item"
* }
*/
const distributedMap = new sfn.DistributedMap(this, 'DistributedMap', {
itemReader: new sfn.S3ObjectsItemReader({
bucketNamePath: sfn.JsonPath.stringAt('$.bucketName'),
prefix: sfn.JsonPath.stringAt('$.prefix'),
}),
});
distributedMap.itemProcessor(new sfn.Pass(this, 'Pass'));
```
* Both `bucket` and `bucketNamePath` are mutually exclusive.
* JSON array in a JSON file stored in S3
* When `DistributedMap` is required to iterate over a JSON array stored in a JSON file in a S3 bucket, then an object of `S3JsonItemReader` can be passed to `itemReader` to configure the iterator source as follows:
```ts
import * as s3 from 'aws-cdk-lib/aws-s3';
/**
* Tree view of bucket:
* my-bucket
* |
* +--input.json
* |
* ...
*
* File content of input.json:
* [
* "item1",
* "item2"
* ]
*/
const bucket = new s3.Bucket(this, 'Bucket', {
bucketName: 'my-bucket',
});
const distributedMap = new sfn.DistributedMap(this, 'DistributedMap', {
itemReader: new sfn.S3JsonItemReader({
bucket,
key: 'input.json',
}),
});
distributedMap.itemProcessor(new sfn.Pass(this, 'Pass'));
```
* If information about `bucket` is only known while starting execution of `StateMachine` (dynamically or at run-time) via state input:
```ts
/**
* JSON state input:
* {
* "bucketName": "my-bucket",
* "key": "input.json"
* }
*/
const distributedMap = new sfn.DistributedMap(this, 'DistributedMap', {
itemReader: new sfn.S3JsonItemReader({
bucketNamePath: sfn.JsonPath.stringAt('$.bucketName'),
key: sfn.JsonPath.stringAt('$.key'),
}),
});
distributedMap.itemProcessor(new sfn.Pass(this, 'Pass'));
```
* CSV file stored in S3
* S3 inventory manifest stored in S3
Map states in Distributed mode also support writing results of the iterator to an S3 bucket and optional prefix. Use a `ResultWriterV2` object provided via the optional `resultWriter` property to configure which S3 location iterator results will be written. The default behavior id `resultWriter` is omitted is to use the state output payload. However, if the iterator results are larger than the 256 kb limit for Step Functions payloads then the State Machine will fail.
ResultWriterV2 object will default to use the Distributed map's query language. If the Distributed map's does not specify a query language, then it
will fall back to the State machine's query langauge.
Note: `ResultWriter` has been deprecated, use `ResultWriterV2` instead. To enable `ResultWriterV2`,
you will have to set the value for `'@aws-cdk/aws-stepfunctions:useDistributedMapResultWriterV2'` to true in the CDK context
```ts
import * as s3 from 'aws-cdk-lib/aws-s3';
// create a bucket
const bucket = new s3.Bucket(this, 'Bucket');
// create a WriterConfig
const distributedMap = new sfn.DistributedMap(this, 'Distributed Map State', {
resultWriterV2: new sfn.ResultWriterV2({
bucket: bucket,
prefix: 'my-prefix',
writerConfig: {
outputType: sfn.OutputType.JSONL,
transformation: sfn.Transformation.NONE,
},
})
});
distributedMap.itemProcessor(new sfn.Pass(this, 'Pass State'));
```
* If information about `bucket` is only known while starting execution of `StateMachine` (dynamically or at run-time) via JSON state input:
```ts
/**
* JSON state input:
* {
* "bucketName": "my-bucket"
* }
*/
const distributedMap = new sfn.DistributedMap(this, 'DistributedMap', {
resultWriterV2: new sfn.ResultWriterV2({
bucketNamePath: sfn.JsonPath.stringAt('$.bucketName'),
}),
});
distributedMap.itemProcessor(new sfn.Pass(this, 'Pass'));
```
* Both `bucket` and `bucketNamePath` are mutually exclusive.
If you want to specify the execution type for the ItemProcessor in the DistributedMap, you must set the `mapExecutionType` property in the `DistributedMap` class. When using the `DistributedMap` class, the `ProcessorConfig.executionType` property is ignored.
In the following example, the execution type for the ItemProcessor in the DistributedMap is set to `EXPRESS` based on the value specified for `mapExecutionType`.
```ts
const distributedMap = new sfn.DistributedMap(this, 'DistributedMap', {
mapExecutionType: sfn.StateMachineType.EXPRESS, // mapExecutionType property is applied.
});
distributedMap.itemProcessor(new sfn.Pass(this, 'Pass'), {
mode: sfn.ProcessorMode.DISTRIBUTED,
executionType: sfn.ProcessorType.STANDARD, // ProcessorConfig.executionType is ignored
});
```
### Custom State
It's possible that the high-level constructs for the states or `stepfunctions-tasks` do not have
the states or service integrations you are looking for. The primary reasons for this lack of
functionality are:
* A [service integration](https://docs.aws.amazon.com/step-functions/latest/dg/concepts-service-integrations.html) is available through Amazon States Language, but not available as construct
classes in the CDK.
* The state or state properties are available through Step Functions, but are not configurable
through constructs
If a feature is not available, a `CustomState` can be used to supply any Amazon States Language
JSON-based object as the state definition.
[Code Snippets](https://docs.aws.amazon.com/step-functions/latest/dg/tutorial-code-snippet.html#tutorial-code-snippet-1) are available and can be plugged in as the state definition.
Custom states can be chained together with any of the other states to create your state machine
definition. You will also need to provide any permissions that are required to the `role` that
the State Machine uses.
The Retry and Catch fields are available for error handling.
You can configure the Retry field by defining it in the JSON object or by adding it using the `addRetry` method.
However, the Catch field cannot be configured by defining it in the JSON object, so it must be added using the `addCatch` method.
The following example uses the `DynamoDB` service integration to insert data into a DynamoDB table.
```ts
import * as dynamodb from 'aws-cdk-lib/aws-dynamodb';
// create a table
const table = new dynamodb.Table(this, 'montable', {
partitionKey: {
name: 'id',
type: dynamodb.AttributeType.STRING,
},
});
const finalStatus = new sfn.Pass(this, 'final step');
// States language JSON to put an item into DynamoDB
// snippet generated from https://docs.aws.amazon.com/step-functions/latest/dg/tutorial-code-snippet.html#tutorial-code-snippet-1
const stateJson = {
Type: 'Task',
Resource: 'arn:aws:states:::dynamodb:putItem',
Parameters: {
TableName: table.tableName,
Item: {
id: {
S: 'MyEntry',
},
},
},
ResultPath: null,
};
// custom state which represents a task to insert data into DynamoDB
const custom = new sfn.CustomState(this, 'my custom task', {
stateJson,
});
// catch errors with addCatch
const errorHandler = new sfn.Pass(this, 'handle failure');
custom.addCatch(errorHandler);
// retry the task if something goes wrong
custom.addRetry({
errors: [sfn.Errors.ALL],
interval: Duration.seconds(10),
maxAttempts: 5,
});
const chain = sfn.Chain.start(custom)
.next(finalStatus);
const sm = new sfn.StateMachine(this, 'StateMachine', {
definitionBody: sfn.DefinitionBody.fromChainable(chain),
timeout: Duration.seconds(30),
comment: 'a super cool state machine',
});
// don't forget permissions. You need to assign them
table.grantWriteData(sm);
```
## Task Chaining
To make defining work flows as convenient (and readable in a top-to-bottom way)
as writing regular programs, it is possible to chain mos