

# Orchestrating Amazon SAM resources with Amazon Step Functions
<a name="serverless-step-functions-in-sam"></a>

You can use [Amazon Step Functions](https://docs.amazonaws.cn/step-functions/latest/dg/) to orchestrate Amazon Lambda functions and other Amazon resources to form complex and robust workflows. Step Functions to tell your application when and under what conditions your Amazon resources, like Amazon Lambda functions, are used. This simplifies the process of forming complex and robust workflows. Using [AWS::Serverless::StateMachine](sam-resource-statemachine.md), you define the individual steps in your workflow, associate resources in each step, and then sequence these steps together. You also add transitions and conditions where they are needed. This simplifies the process of making a complex and robust workflow.

**Note**  
To manage Amazon SAM templates that contain Step Functions state machines, you must use version 0.52.0 or later of the Amazon SAM CLI. To check which version you have, execute the command `sam --version`.

Step Functions is based on the concepts of [tasks](https://docs.amazonaws.cn/step-functions/latest/dg/amazon-states-language-task-state.html) and [state machines](https://docs.amazonaws.cn/step-functions/latest/dg/concepts-states.html). You define state machines using the JSON-based [Amazon States Language](https://docs.amazonaws.cn/step-functions/latest/dg/concepts-amazon-states-language.html). The [Step Functions console](https://console.amazonaws.cn/states/home?region=us-east-1#/) displays a graphical view of your state machine's structure so you can visually check your state machine's logic and monitor executions.

With Step Functions support in Amazon Serverless Application Model (Amazon SAM), you can do the following:
+ Define state machines, either directly within an Amazon SAM template or in a separate file 
+ Create state machine execution roles through Amazon SAM policy templates, inline policies, or managed policies 
+ Trigger state machine executions with API Gateway or Amazon EventBridge events, on a schedule within an Amazon SAM template, or by calling APIs directly
+ Use available [Amazon SAM Policy Templates](https://docs.amazonaws.cn/serverless-application-model/latest/developerguide/serverless-policy-templates.html) for common Step Functions development patterns.

## Example
<a name="serverless-step-functions-in-sam-example"></a>

The following example snippet from a Amazon SAM template file defines a Step Functions state machine in a definition file. Note that the `my_state_machine.asl.json` file must be written in [Amazon States Language](https://docs.amazonaws.cn/step-functions/latest/dg/concepts-amazon-states-language.html).

```
AWSTemplateFormatVersion: "2010-09-09"
Transform: AWS::Serverless-2016-10-31
Description: Sample SAM template with Step Functions State Machine

Resources:
  MyStateMachine:
    Type: AWS::Serverless::StateMachine
    Properties:
      DefinitionUri: statemachine/my_state_machine.asl.json
      ...
```

To download a sample Amazon SAM application that includes a Step Functions state machine, see [Create a Step Functions State Machine Using Amazon SAM](https://docs.amazonaws.cn/step-functions/latest/dg/tutorial-state-machine-using-sam.html) in the *Amazon Step Functions Developer Guide*.

## More information
<a name="serverless-step-functions-in-sam-more-information"></a>

To learn more about Step Functions and using it with Amazon SAM, see the following:
+ [How Amazon Step Functions works](https://docs.amazonaws.cn/step-functions/latest/dg/how-step-functions-works.html)
+ [Amazon Step Functions and Amazon Serverless Application Model](https://docs.amazonaws.cn/step-functions/latest/dg/concepts-sam-sfn.html)
+ [Tutorial: Create a Step Functions State Machine Using Amazon SAM](https://docs.amazonaws.cn/step-functions/latest/dg/tutorial-state-machine-using-sam.html)
+ [Amazon SAM Specification: AWS::Serverless::StateMachine](sam-resource-statemachine.md)