Serverless Walkthrough on AWS - Part 3
Todd Bernson
Award Winning Technology Leader | AWS Ambassador | AWS Machine Learning Community Builder | Lifelong Learner | Data Analytics, ML, AI
AWS Step Functions workflow:
Introduction
AWS Step Functions is a serverless workflow service that allows you to coordinate multiple AWS services into serverless workflows. It simplifies creating and managing long-running workflows by providing a graphical console for designing and debugging workflows and APIs for programmatic access. This article will explore using Terraform to create an AWS Step Functions workflow, including defining state machines, defining input/output templates, and executing the workflows.
AWS Step Functions Overview
AWS Step Functions is a fully managed service that allows you to build, run, and orchestrate workflows using a visual interface. You can create workflows by arranging AWS services, functions, and steps into a state machine defining your workflow. The state machine defines the states of the workflow and the transitions between them and can also include error handling and retry logic. AWS Step Functions also allow you to define input and output templates that define the data format passed between the states.
AWS Step Functions can create workflows for various use cases, including data processing, ETL, business processes, and application orchestration. It provides a reliable and scalable way to orchestrate AWS services and functions, making it an ideal choice for building serverless applications.
Defining State Machines with Terraform
To create an AWS Step Functions workflow with Terraform, you must first define the state machine using the "aws_sfn_state_machine" resource. The state machine defines the states of the workflow and the transitions between them. You can define states using the "aws_sfn_state" resource and transitions using the "aws_sfn_transition" resource. You can also define error handling and retry logic using the "aws_sfn_catch" resource.
resource "aws_sfn_state_machine" "sfn_state_machine" { name = var.environment role_arn = aws_iam_role.step_function_execution_role.arn definition = <<EOF { "Comment": "A Step Functions state machine that invokes a Lambda function with input, conditionally including the user's name.", "StartAt": "ValidateInput", "States": { "ValidateInput": { "Type": "Choice", "Choices": [ { "Variable": "$.name", "StringEquals": "", "Next": "InvalidInput" }, { "Variable": "$.name", "IsPresent": false, "Next": "InvalidInput" } ], "Default": "GetName" }, "InvalidInput": { "Type": "Fail", "Cause": "Invalid input format", "Error": "InvalidInput" }, "GetName": { "Type": "Pass", "ResultPath": "$", "Next": "InvokeLambda" }, "InvokeLambda": { "Type": "Task", "Resource": "${aws_lambda_function.this.arn}", "InputPath": "$", "ResultPath": "$.lambda_output", "OutputPath": "$.lambda_output", "End": true } } } EOF logging_configuration { log_destination = "${aws_cloudwatch_log_group.step_function.arn}:*" include_execution_data = true level = "ERROR" } type = "STANDARD" tags = var.tags }
In the example above, we define a state machine with four states: We validate our input JSON and either error or invoke a Lambda function. Then we can view the result of the Lambda function.
Testing
After applying our Terraform, located here, we can view the console and start execution.
We then need to put JSON in, in the proper format.
{ "name": "<your_name>" }
Then, we can view the result of the Lambda.
Conclusion
In this article, we have explored how to use Terraform to create an AWS Step Functions workflow, including defining state machines with Terraform and executing workflows. AWS Step Functions provides a powerful and flexible way to orchestrate AWS services and functions, and Terraform makes it easy to define and manage your workflows as code. With AWS Step Functions and Terraform, you can build reliable and scalable serverless workflows for a wide range of use cases.