aws-cdk-lib.aws_stepfunctions.Choice

class Choice (construct)

LanguageType name
.NETAmazon.CDK.AWS.StepFunctions.Choice
Gogithub.com/aws/aws-cdk-go/awscdk/v2/awsstepfunctions#Choice
Javasoftware.amazon.awscdk.services.stepfunctions.Choice
Pythonaws_cdk.aws_stepfunctions.Choice
TypeScript (source)aws-cdk-lib » aws_stepfunctions » Choice

Implements IConstruct, IDependable, IChainable

Define a Choice in the state machine.

A choice state can be used to make decisions based on the execution state.

Example

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', {
  definition,
  timeout: Duration.minutes(5),
});

Initializer

new Choice(scope: Construct, id: string, props?: ChoiceProps)

Parameters

  • scope Construct
  • id string — Descriptive identifier for this chainable.
  • props ChoiceProps

Construct Props

NameTypeDescription
comment?stringAn optional description for this state.
inputPath?stringJSONPath expression to select part of the state to be the input to this state.
outputPath?stringJSONPath expression to select part of the state to be the output to this state.

comment?

Type: string (optional, default: No comment)

An optional description for this state.


inputPath?

Type: string (optional, default: $)

JSONPath expression to select part of the state to be the input to this state.

May also be the special value DISCARD, which will cause the effective input to be the empty object {}.


outputPath?

Type: string (optional, default: $)

JSONPath expression to select part of the state to be the output to this state.

May also be the special value DISCARD, which will cause the effective output to be the empty object {}.

Properties

NameTypeDescription
endStatesINextable[]Continuable states of this Chainable.
idstringDescriptive identifier for this chainable.
nodeNodeThe tree node.
startStateStateFirst state of this Chainable.
stateIdstringTokenized string that evaluates to the state's ID.

endStates

Type: INextable[]

Continuable states of this Chainable.


id

Type: string

Descriptive identifier for this chainable.


node

Type: Node

The tree node.


startState

Type: State

First state of this Chainable.


stateId

Type: string

Tokenized string that evaluates to the state's ID.

Methods

NameDescription
addPrefix(x)Add a prefix to the stateId of this state.
afterwards(options?)Return a Chain that contains all reachable end states from this Choice.
bindToGraph(graph)Register this state as part of the given graph.
otherwise(def)If none of the given conditions match, continue execution with the given state.
toStateJson()Return the Amazon States Language object for this state.
toString()Returns a string representation of this construct.
when(condition, next)If the given condition matches, continue execution with the given state.

addPrefix(x)

public addPrefix(x: string): void

Parameters

  • x string

Add a prefix to the stateId of this state.


afterwards(options?)

public afterwards(options?: AfterwardsOptions): Chain

Parameters

  • options AfterwardsOptions

Returns

  • Chain

Return a Chain that contains all reachable end states from this Choice.

Use this to combine all possible choice paths back.


bindToGraph(graph)

public bindToGraph(graph: StateGraph): void

Parameters

  • graph StateGraph

Register this state as part of the given graph.

Don't call this. It will be called automatically when you work with states normally.


otherwise(def)

public otherwise(def: IChainable): Choice

Parameters

  • def IChainable

Returns

  • Choice

If none of the given conditions match, continue execution with the given state.

If no conditions match and no otherwise() has been given, an execution error will be raised.


toStateJson()

public toStateJson(): json

Returns

  • json

Return the Amazon States Language object for this state.


toString()

public toString(): string

Returns

  • string

Returns a string representation of this construct.


when(condition, next)

public when(condition: Condition, next: IChainable): Choice

Parameters

  • condition Condition
  • next IChainable

Returns

  • Choice

If the given condition matches, continue execution with the given state.