Using Terraform to deploy state machines in Step Functions
Terraform
If you're familiar with Terraform, you can follow the development lifecycle described in this topic as a model for creating and deploying your state machines in Terraform. If you aren't familiar with Terraform, we recommend that you first complete the workshop Introduction to Terraform on AWS
Tip
To deploy an example of a state machine built using Terraform to your AWS account, see the module Managing state machines with infrastructure as code
In this topic
Prerequisites
Before you get started, make sure you complete the following prerequisites:
-
Install Terraform on your machine. For information about installing Terraform, see Install Terraform
. -
Install Step Functions Local on your machine. We recommend that you install the Step Functions Local Docker image to use Step Functions Local. For more information, see Testing state machines with Step Functions Local (unsupported).
-
Install AWS SAM CLI. For installation information, see Installing the AWS SAM CLI in the AWS Serverless Application Model Developer Guide.
-
Install the AWS Toolkit for Visual Studio Code to view the workflow diagram of your state machines. For installation information, see Installing the AWS Toolkit for Visual Studio Code in the AWS Toolkit for Visual Studio Code User Guide.
State machine development lifecycle with Terraform
The following procedure explains how you can use a state machine prototype that you build using Workflow Studio in the Step Functions console as a starting point for local development with Terraform and the AWS Toolkit for Visual Studio Code.
To view the complete example that discusses the state machine development with Terraform and presents the best practices in detail, see Best practices for writing Step Functions Terraform projects
To start the development lifecycle of a state machine with Terraform
-
Bootstrap a new Terraform project with the following command.
terraform init
-
Open the Step Functions console
to create a prototype for your state machine. -
In Workflow Studio, do the following:
-
Create your workflow prototype.
-
Export the Amazon States Language (ASL) definition of your workflow. To do this, choose the Import/Export dropdownlist, and then select Export JSON definition.
-
-
Save the exported ASL definition within your project directory.
You pass the exported ASL definition as an input parameter to the
aws_sfn_state_machine
Terraform resource that uses the templatefile
function. This function is used inside the definition field that passes the exported ASL definition and any variable substitutions. Tip
Because the ASL definition file can contain lengthy blocks of text, we recommend you avoid the inline EOF method. This makes it easier to substitute parameters into your state machine definition.
-
(Optional) Update the ASL definition within your IDE and visualize your changes using the AWS Toolkit for Visual Studio Code.
To avoid continuously exporting your definition and refactoring it into your project, we recommend that you make updates locally in you IDE and track these updates with Git
. -
Test your workflow using Step Functions Local.
Tip
You can also locally test service integrations with Lambda functions and API Gateway APIs in your state machine using AWS SAM CLI Local.
-
Preview your state machine and other AWS resources before deploying the state machine. To do this, run the following command.
terraform plan
-
Deploy your state machine from your local environment or through CI/CD pipelines
using the following command. terraform apply
-
(Optional) Clean up your resources and delete the state machine using the following command.
terraform destroy
IAM roles and policies for your state machine
Use the Terraform service integration policies
The following IAM policy example grants your state machine access to invoke a Lambda function named
.myFunction
{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": [ "lambda:InvokeFunction" ], "Resource": "arn:aws:lambda:us-east-1:123456789012:function:
myFunction
" } ] }
We also recommend using the aws_iam_policy_document
The following IAM policy example uses the aws_iam_policy_document
data source and grants your state machine access to invoke a Lambda function named
.myFunction
data "aws_iam_policy_document" "state_machine_role_policy" { statement { effect = "Allow" actions = [ "lambda:InvokeFunction" ] resources = ["${aws_lambda_function.
[[myFunction]]
.arn}:*"] } }
Tip
To view more advanced AWS architectural patterns deployed with Terraform, see Terraform examples at Serverless Land Workflows Collection