DEV Community

Cover image for How to manage your AWS Step Functions with Serverless
We're Serverless! for Serverless Inc.

Posted on • Originally published at serverless.com

How to manage your AWS Step Functions with Serverless

Originally posted at Serverless on Sep 18, 2017

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 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 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, 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.


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, which can be defined within definition section with yaml format in your serverless.yml.

I recommend using in combination with Serverless AWS 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.

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:

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:


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.

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.


Then, here is another Lambda function which writes a log to S3 Bucket.

In the end, describe your serverless.yml looks like, and deploy with serverless deploy.

If you can see the API Gateway endpoint on your console, it means to deploy successfully。

Send a CURL request to your live endpoint:

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 or send a Pull Request. I always welcome them!!

One more thing, tutorial on how to use the plugin has been coverd on FOOBAR youtube channel. You can also learn it there. Thanks @mavi888uy for making the great video!

Originally published at https://www.serverless.com.

Top comments (0)