How to manage your AWS Step Functions with Serverless
Tags: serverless, aws, stepfunctions, lambda
Published: 2022-04-12 18:56:48

Originally posted at [Serverless](https://www.serverless.com/blog/how-to-manage-your-aws-step-functions-with-serverless/) on Sep 18, 2017

![](https://cdn-images-1.medium.com/max/2400/0*CBt1wZ2qYT_4z4fz.jpg)

* *New to Step Functions?* Check out this [Ultimate Guide to AWS Step Functions](https://serverless.com/aws-step-functions/)

When diving into the Functions as a Service (FaaS) world, a question that often pops up is:
>  *If serverless functions are stateless, how do I manage state?*

There are a number of ways to manage state with backend data stores, tmp directories & building this logic into your existing lambda functions but there is a simpler alternative provided by AWS: **Step Functions**.

[Step Functions](https://aws.amazon.com/step-functions/) allows you to control complex workflows using Lambda functions without the underlying application managing and orchestrating the state. In essence, it’s a state machine to help with complex workflows and aims at keeping your lambda functions free of this additional logic.

## Serverless + Step Functions

A couple months ago, I created the [Serverless Step Functions](https://github.com/horike37/serverless-step-functions) plugin to deploy and manage Step Functions and a bunch of composed Lambda functions via the Serverless Framework.

In this post, I will share the functionality and usage of the plugin, and a workflow for your development.

So let’s get down to business!

## Install

Before getting started, you need to install the plugin. This is hosted on the [Serverless Plugins registry](https://github.com/serverless/plugins), so you can install this via the plugin install command which is introduced since v1.22.0.

Please run the following command in your service, then the plugin will be added automatically in plugins array in your serverless.yml file.
{% gist https://gist.github.com/richardgrantserverless/e8490f1e7ccb2adbe53ab76b531d6298.js %}
If you run serverless --help command and you can see an explanation of subcommands for the plugin like `serverless invoke stepf, installing is successful.

## Getting Started

### Define AWS state language

To define a workflow with Step Functions, you need write a structured language called [Amazon States Language](http://docs.aws.amazon.com/step-functions/latest/dg/concepts-amazon-states-language.html), which can be defined within definition section with yaml format in your serverless.yml.

I recommend using in combination with [Serverless AWS Pseudo Parameters](https://www.npmjs.com/package/serverless-pseudo-parameters) since it makes it easy to set up in Resource section in serverless.yml.

The following is an example which is a simplest state machine definition, which is composed of a single lambda function.
{% gist https://gist.github.com/richardgrantserverless/e89d3ce8ebd10128ac1f010c19973523.js %}
## Event

You can define events to invoke your Step Functions. Currently, http and scheduled events have been supported. The configuration syntax is similar to the Lambda events provided by the framework core.

Here’s how to define those events:
{% gist https://gist.github.com/richardgrantserverless/9e70463e9dc210f7e411a2b336e6f99f.js %}
## Use triggered Lambda events

If you want to use events other than http and scheduled, you can create a Lambda function which only run your statemachine

Using the AWS SDK, you can trigger your step functions like:
{% gist https://gist.github.com/richardgrantserverless/792a64e2033b779ec01b2c2a67a3d201.js %}
Then, you set up the Lambda will be triggered by events what you want. startExecution API requires a statemachine ARN so you can pass that via environment variables system.

Here’s serverless.yml sample which a triggered statemachine by S3 event.
{% gist https://gist.github.com/richardgrantserverless/cfb51153651c3d59f121513c9a2f2faf.js %}
## Create a sample application

Let’s consider a small 2 step application that starts EC2 and write the result on S3 bucket.

First, we will create a Lambda function that only starts an EC2 instance, to which will be passed instanceId via API Body request parameter.
{% gist https://gist.github.com/richardgrantserverless/2753a30c9b808058d66de9e1631a31e2.js %}
Then, here is another Lambda function which writes a log to S3 Bucket.
{% gist https://gist.github.com/richardgrantserverless/760bdeae4e6c7317bab2cf90d5c25149.js %}
In the end, describe your serverless.yml looks like, and deploy with serverless deploy.
{% gist https://gist.github.com/richardgrantserverless/17d43b1f7d1fafb4fe5955ede93d769d.js %}
If you can see the API Gateway endpoint on your console, it means to deploy successfully。
{% gist https://gist.github.com/richardgrantserverless/63524144dad89fbb30b3cd27aafe6de4.js %}
Send a CURL request to your live endpoint:
{% gist https://gist.github.com/richardgrantserverless/183c1bb831393c43828ba31a0ca75a38.js %}
You should see that specified EC2 will be started and a log will be written to S3 Bucket.

## Summary

The Serverless Step Functions plugin makes it easier to manage and deploy your Step Functions.

If you have any comments or feedback, please create a new [issue](https://github.com/horike37/serverless-step-functions/issues/new) or send a Pull Request. I always welcome them!!

One more thing, tutorial on how to use the plugin has been coverd on [FOOBAR](https://www.youtube.com/channel/UCSLIvjWJwLRQze9Pn4cectQ) youtube channel. You can also learn it there. Thanks [@mavi888uy](https://twitter.com/mavi888uy) for making the great video!
{% embed https://www.youtube.com/watch?v=bEB0zDHXXG4 %}

*Originally published at [https://www.serverless.com](https://www.serverless.com/blog/how-to-manage-your-aws-step-functions-with-serverless/).*