# StateMachine

Define a StepFunctions State Machine.

Extends: Construct › Resource › StateMachine
Implements: IResource, IGrantable, IStateMachine, IEnvironmentAware, IStateMachineRef, IConstruct, IDependable

- CloudFormation: [`AWS::StepFunctions::StateMachine`](https://docs.aws.amazon.com/AWSCloudFormation/latest/TemplateReference/aws-resource-stepfunctions-statemachine.html)
- L1 type: [`CfnStateMachine`](/cdk/api/v2/docs/aws-cdk-lib.aws_stepfunctions.CfnStateMachine.html)
- Source: [`aws-stepfunctions/lib/state-machine.ts`](https://github.com/aws/aws-cdk/blob/v2.274.0/packages/aws-cdk-lib/aws-stepfunctions/lib/state-machine.ts#L407)
- Type last changed: v2.227.0 · 21 Nov 2025

## Example

```ts
import * as stepfunctions from 'aws-cdk-lib/aws-stepfunctions';

const pipeline = new codepipeline.Pipeline(this, 'MyPipeline');
const inputArtifact = new codepipeline.Artifact();
const startState = new stepfunctions.Pass(this, 'StartState');
const simpleStateMachine  = new stepfunctions.StateMachine(this, 'SimpleStateMachine', {
  definition: startState,
});
const stepFunctionAction = new codepipeline_actions.StepFunctionInvokeAction({
  actionName: 'Invoke',
  stateMachine: simpleStateMachine,
  stateMachineInput: codepipeline_actions.StateMachineInput.filePath(inputArtifact.atPath('assets/input.json')),
});
pipeline.addStage({
  stageName: 'StepFunctions',
  actions: [stepFunctionAction],
});
```

## Initializer

```ts
new StateMachine(scope: Construct, id: string, props: StateMachineProps)
```

## Construct Props

- `comment`: string — Comment that describes this state machine.

  **Default:** No comment

- `definition`: [IChainable](/cdk/api/v2/docs/aws-cdk-lib.aws_stepfunctions.IChainable.html) — use definitionBody: DefinitionBody.fromChainable()

- `definitionBody`: [DefinitionBody](/cdk/api/v2/docs/aws-cdk-lib.aws_stepfunctions.DefinitionBody.html) — Definition for this state machine.

- `definitionSubstitutions`: { [key: string]: string } — substitutions for the definition body as a key-value map.

- `encryptionConfiguration`: [EncryptionConfiguration](/cdk/api/v2/docs/aws-cdk-lib.aws_stepfunctions.EncryptionConfiguration.html) — Configures server-side encryption of the state machine definition and execution history.

  **Default:** data is transparently encrypted using an AWS owned key

- `logs`: [LogOptions](/cdk/api/v2/docs/aws-cdk-lib.aws_stepfunctions.LogOptions.html) — Defines what execution history events are logged and where they are logged.

  **Default:** No logging

- `queryLanguage`: [QueryLanguage](/cdk/api/v2/docs/aws-cdk-lib.aws_stepfunctions.QueryLanguage.html) = JSON_PATH — The name of the query language used by the state machine.

  If the state does not contain a `queryLanguage` field,
  then it will use the query language specified in this `queryLanguage` field.

- `removalPolicy`: [RemovalPolicy](/cdk/api/v2/docs/aws-cdk-lib.RemovalPolicy.html) = RemovalPolicy.DESTROY — The removal policy to apply to state machine.

- `role`: [IRole](/cdk/api/v2/docs/aws-cdk-lib.aws_iam.IRole.html) — The execution role for the state machine service.

  **Default:** A role is automatically created

- `stateMachineName`: string — A name for the state machine.

  **Default:** A name is automatically generated

- `stateMachineType`: [StateMachineType](/cdk/api/v2/docs/aws-cdk-lib.aws_stepfunctions.StateMachineType.html) = StateMachineType.STANDARD — Type of the state machine.

- `timeout`: [Duration](/cdk/api/v2/docs/aws-cdk-lib.Duration.html) — Maximum run time for this state machine.

  **Default:** No timeout

- `tracingEnabled`: boolean = false — Specifies whether Amazon X-Ray tracing is enabled for this state machine.

## Properties

- `env`: [ResourceEnvironment](/cdk/api/v2/docs/aws-cdk-lib.interfaces.ResourceEnvironment.html) (required) — The environment this resource belongs to.

  For resources that are created and managed in a Stack (those created by
  creating new class instances like `new Role()`, `new Bucket()`, etc.), this
  is always the same as the environment of the stack they belong to.

  For referenced resources (those obtained from referencing methods like
  `Role.fromRoleArn()`, `Bucket.fromBucketName()`, etc.), they might be
  different than the stack they were imported into.

- `grantPrincipal`: [IPrincipal](/cdk/api/v2/docs/aws-cdk-lib.aws_iam.IPrincipal.html) (required) — The principal this state machine is running as.

- `grants`: [StateMachineGrants](/cdk/api/v2/docs/aws-cdk-lib.aws_stepfunctions.StateMachineGrants.html) (required) — Collection of grant methods for a StateMachine.

- `node`: [Node](/cdk/api/v2/docs/constructs.Node.html) (required) — The tree node.

- `PROPERTY_INJECTION_ID`: string (required) (static) — Uniquely identifies this class.

- `role`: [IRole](/cdk/api/v2/docs/aws-cdk-lib.aws_iam.IRole.html) (required) — Execution role of this state machine.

- `stack`: [Stack](/cdk/api/v2/docs/aws-cdk-lib.Stack.html) (required) — The stack in which this resource is defined.

- `stateMachineArn`: string (required) — The ARN of the state machine.

- `stateMachineName`: string (required) — The name of the state machine.

- `stateMachineRef`: [StateMachineReference](/cdk/api/v2/docs/aws-cdk-lib.interfaces.aws_stepfunctions.StateMachineReference.html) (required) — A reference to a StateMachine resource.

- `stateMachineRevisionId`: string (required) — Identifier for the state machine revision, which is an immutable, read-only snapshot of a state machine’s definition and configuration.

- `stateMachineType`: [StateMachineType](/cdk/api/v2/docs/aws-cdk-lib.aws_stepfunctions.StateMachineType.html) (required) — Type of the state machine.

## Methods

- `addToRolePolicy(statement: PolicyStatement): void` — Add the given statement to the role's policy.

- `applyCrossStackReferenceStrength(strength: ReferenceStrength): void` — Override the cross-stack reference strength for this resource.

  When set, any cross-stack reference to this resource will use the specified
  mechanism instead of the global default determined by the
  `@aws-cdk/core:defaultCrossStackReferences` context key. This is useful for
  selectively weakening specific references to avoid the "deadly embrace" problem
  without changing the app-wide default.

  **Parameters**

  - `strength` — - The reference strength to use for this resource.

- `applyRemovalPolicy(policy: RemovalPolicy): void` — Apply the given removal policy to this resource.

  The Removal Policy controls what happens to this resource when it stops
  being managed by CloudFormation, either because you've removed it from the
  CDK application or because you've made a change that requires the resource
  to be replaced.

  The resource can be deleted (`RemovalPolicy.DESTROY`), or left in your AWS
  account for data recovery and cleanup later (`RemovalPolicy.RETAIN`).

- `fromStateMachineArn(scope: Construct, id: string, stateMachineArn: string): IStateMachine` (static) — Import a state machine.

- `fromStateMachineName(scope: Construct, id: string, stateMachineName: string): IStateMachine` (static) — Import a state machine via resource name.

- `grant(identity: IGrantable, ...actions: string[]): Grant` — Grant the given identity custom permissions.

  [disable-awslint:no-grants]

- `grantExecution(identity: IGrantable, ...actions: string[]): Grant` — Grant the given identity permissions on all executions of the state machine.

  The use of this method is discouraged. Please use `grants.execution()` instead.

  [disable-awslint:no-grants]

- `grantRead(identity: IGrantable): Grant` — Grant the given identity permissions to read results from state machine.

  The use of this method is discouraged. Please use `grants.read()` instead.

  [disable-awslint:no-grants]

- `grantRedriveExecution(identity: IGrantable): Grant` — Grant the given identity permission to redrive the execution of the state machine.

  The use of this method is discouraged. Please use `grants.redriveExecution()` instead.

  [disable-awslint:no-grants]

- `grantStartExecution(identity: IGrantable): Grant` — Grant the given identity permissions to start an execution of this state machine.

  The use of this method is discouraged. Please use `grants.startExecution()` instead.

  [disable-awslint:no-grants]

- `grantStartSyncExecution(identity: IGrantable): Grant` — Grant the given identity permissions to start a synchronous execution of this state machine.

  The use of this method is discouraged. Please use `grants.startSyncExecution()` instead.

  [disable-awslint:no-grants]

- `grantTaskResponse(identity: IGrantable): Grant` — Grant the given identity task response permissions on a state machine.

  The use of this method is discouraged. Please use `grants.taskResponse()` instead.

  [disable-awslint:no-grants]

- `isConstruct(x: any): boolean` (static) — Checks if `x` is a construct.

  Use this method instead of `instanceof` to properly detect `Construct`
  instances, even when the construct library is symlinked.

  Explanation: in JavaScript, multiple copies of the `constructs` library on
  disk are seen as independent, completely different libraries. As a
  consequence, the class `Construct` in each copy of the `constructs` library
  is seen as a different class, and an instance of one class will not test as
  `instanceof` the other class. `npm install` will not create installations
  like this, but users may manually symlink construct libraries together or
  use a monorepo tool: in those cases, multiple copies of the `constructs`
  library can be accidentally installed, and `instanceof` will behave
  unpredictably. It is safest to avoid using `instanceof`, and using
  this type-testing method instead.

  **Parameters**

  - `x` — Any object.

  **Returns:** true if `x` is an object created from a class which extends `Construct`.

- `isOwnedResource(construct: IConstruct): boolean` (static) — Returns true if the construct was created by CDK, and false otherwise.

- `isResource(construct: IConstruct): boolean` (static) — Check whether the given construct is a Resource.

- `metric(metricName: string, props?: MetricOptions): Metric` — Return the given named metric for this State Machine's executions.

- `metricAborted(props?: MetricOptions): Metric` — Metric for the number of executions that were aborted.

- `metricFailed(props?: MetricOptions): Metric` — Metric for the number of executions that failed.

- `metricStarted(props?: MetricOptions): Metric` — Metric for the number of executions that were started.

- `metricSucceeded(props?: MetricOptions): Metric` — Metric for the number of executions that succeeded.

- `metricThrottled(props?: MetricOptions): Metric` — Metric for the number of executions that were throttled.

- `metricTime(props?: MetricOptions): Metric` — Metric for the interval, in milliseconds, between the time the execution starts and the time it closes.

- `metricTimedOut(props?: MetricOptions): Metric` — Metric for the number of executions that timed out.

- `toString(): string` — Returns a string representation of this construct.

- `with(...mixins: IMixin[]): IConstruct` — Applies one or more mixins to this construct.

  Mixins are applied in order. The list of constructs is captured at the
  start of the call, so constructs added by a mixin will not be visited.
  Use multiple `with()` calls if subsequent mixins should apply to added
  constructs.
