🐾Simple way to configure Step Functions with Terraform🐾
🤓 Have you ever Terraformed Step Functions workflow? I can imagine that if the workflow is more complex and consists of more than 5 components, it is not an easy task to write a configuration. Usually, engineers do not use the AWS console while writing Terraform code. They prefer to use documentation and maybe some examples from GitHub or StackOverflow. But what if an easier way exists?
Start with the AWS console
Let’s imagine, that you would start with creating StepFunctions workflow using the AWS console. While configuring StepFunctions workflow using the AWS console, you get a visual editor that allows you to combine different components and provide configurations for them. You can combine steps by choosing specific actions and drugging them to design a graph, as displayed below:
It is much easier to understand the connection between steps because of the diagram provided by the console. Code configuration is usually more messy and long, especially when several yes/no splits exist.
Choose configuration parameters from the list
Also, you can check all configurations available for this kind of step. For example, Lambda:Invoke action configuration via console has several fields:
Test before deployment
You can run tests of each step, check for errors, and debug them even without deploying your workflow. It’s a handy option because, with Terraform configuration, you still need to run the workload via the console and redeploy it to test fixes.
The best part — import to Terraform
At this point, you may wonder how it is related to Terraform configuration. The answer to this is pretty simple: you can export your workflow definition as a JSON file by clicking on Actions → Export definition → as JSON file.
After that, you can insert this file into your Terraform configuration, create a data resource, and reference it in the aws_sfn_state_machine resource:
data "template_file" "definition_file" {
template = file("statemachines/statemachine.asl.json")
vars = {
LambdaFunction = aws_lambda_function.test_lambda.arn
}
}
resource "aws_sfn_state_machine" "sample_workflow" {
name_prefix = "sample-workflow-name"
role_arn = aws_iam_role.sample_role.arn
definition = data.template_file.definition_file.rendered
type = "STANDARD"
}
That’s it, now you can use this configuration to deploy your Step Functions workflow. As you can see it’s much easier than using plain code for configuration.
Thank you for reading, let’s chat 💬
💬 Have you heard about this approach before?
💬 Any inconveniences you noticed while using it?
💬 Any advice or tips you can share about configuring workflow?
I love hearing from readers 🫶🏻 Please feel free to drop comments, questions, and opinions below👇🏻