Rust: execute AWS Step Function from a Lambda function

Rust: execute AWS Step Function from a Lambda function

This blog post is concise, and it will follow the previous, adding just the part of the code that we need to execute the Step Function.

As a part of the series Serverless Rust you can check out the other parts:

Part 1 and 2 describe how to set up Rust and VsCode.

Part 3 how to process in parallel AWS SQS messages.

AWS Step Functions

Use AWS Step Functions to orchestrate multiple Lambda functions or interact with over 200 AWS services. In 2021 AWS also released another tool that I love Workflow Studio. I use this tool as a whiteboard to explain flows.

AWS Step Functions comes with many benefits and built-in features, and it is based on concepts like tasks and state machines. A task performs an action while a state machine is a final status that expresses the overall workflow.

You can reduce a lot of boilerplate code by combining states Screenshot 2022-01-10 at 10.28.50.png

You can build synchronous and asynchronous workflow. Asynchronous workflow is helpful if you wait for a service or a manual operation to come back to you after a while. AWS offer three ways:

  • Request Response
  • Run a Job (.sync)
  • Wait for a Callback with the Task Token

I have also written how to move from AWS Lambda to AWS Step Function here.

The Express workflow is much cheaper than the standard workflow, but it doesn’t come with any visual support, so you need to rely on the CloudWatch log that makes it, for me, not the best.

Implementation

async fn start_stepfunction(client: &AWSClient, request: Request) -> Result<(), Error> { 
  let pay_load = PayLoad {
    is_hello_world_example: request.id
  };
  let state_machine_arn = std::env::var("STATE_MACHINE_ARN").expect("STATE_MACHINE_ARN must be set");

  let result = client.sf_client
    .start_execution()
    .set_state_machine_arn(Some(state_machine_arn))
    .input(serde_json::to_string(&pay_load).unwrap())
    //.set_name(UNIQUE_NAME)
    .send()
    .await?;

  log::info!("RESULT {:?}", result);

  Ok(())
}

It is pretty much this. You can find all the code on GitHub

Conclusion

AWS Step Functions is an important part of a Serverless ecosystem. It provides many advantages, allowing you to focus on your business logic instead of writing boilerplate code. In addition, you can orchestrate automated workflows according to your needs.