AWS Step Functions

AWS Step Functions

January 17, 2017 1 By Eric Shanks

This year at AWS re:Invent Amazon announced a new service called Step Functions. According to AWS, Step Functions is an easy way to coordinate the components of distributed applications and microservices using visual workflows. That pretty much sums it up! When you’ve got a series of small microservices that need to be coordinated, it can be tricky to write this code into each lambda function to call the next function. Step Functions gives you a visual editor to manage the calls to multiple Lambda functions to make your life easier. I’ve written about this before on the AHEAD blog.

This post will cover how to create a very basic AWS Step Function to get you started and on your way with your server-less application.

Prerequisites

Lambda Functions

To get started, you’ll want at least one AWS Lambda function. I have two main Lambda functions that I’ll be using as examples. I have “AutoOn-Tagged” and “AutoOff-Tagged” Lamda functions that are written in Python. These functions look for any EC2 instances in my VPC that are tagged with a key name of “AutoOff” and a value of “True.” The idea behind this is that you can tag all your instances that can be shut down without affecting services, like a dev environment at the end of the workday, and power them all off to save money. If you want to see more specifics about these Lambda functions go see this Equinix link where you too can borrow some code.

Lambda Functions to be used in Step Function

 

IAM Policy

Before you create your Step Functions you will want to create an IAM role that has permissions to run your code. Open your AWS Console and go to the IAM section. Click Policies and then click “Create Policy.” Click “Select” next to “Create Your Own Policy. Give the policy a name and a description and then paste in the policy document. I’ve saved you some time and put the IAM Policy in my github repo so you can copy it. Validate and then create the policy.

 

IAM Role

Once you have your IAM Policy created, click on the Roles link to attach the policy you just created and add it to a role. Click “Create New Role” to get started. Give the role a descriptive name and then click “Next Step.”

 

On the Role type screen select “AWS Lambda.”

On the Attach Policy screen find and select the policy you created earlier. and then click “Next Step.”

 

Review the role setup and click the “Create Role” button.

Create Step Function

Once you’ve got your Lambda functions tested and working, we can place them behind our Step Functions. Open up the Step Functions menu in the AWS Console and it will take you to the State Machine dashboard. Click “Create a State Machine” button. You’ll need to name the state machine something descriptive and then we can use some of the blueprints below to get started. The square boxes below the name field will show you different methods on using Step Functions. Feel free to click any of those blueprints to see different ways of arranging your Step Functions. The “Preview” pane will take the blueprint you’ve selected and visually draw the logic gates that would be used in that blueprint. If you look below this, you’ll see the “Code” pane and the JSON that would be used to create those logic gates.

 

Please note though, that you don’t need to use these blueprints at all, they’re just useful if you’re getting started. You can paste your JSON code in the code pane and click the refresh button in the preview pane at any time to see your code visually. I’ve decided that for this example I would use the “Choice State” blueprint as my getting started template and modify it to meet my needs. If you would like to download this code, check out my github repo here.  I’ve made some notes in the screenshot to get you started on learning how the code is used.

I’ve modified the code pane for my own purposes and then clicked the refresh button next to the preview pane to show what my State Machine will do. It begins with a choice where we’ll take some input and make a decision based on whether I entered On or Off. If the input is on, we’ll go to the OnMatchState, if Off we’ll go to the OffMatchState and if i were to enter anything else then we go to the Default State.

Preview of Step Functions

 

Once you’ve uploaded your code and are happy with it, click the “Create State Machine” button at the bottom of the screen. The next window that pops up will be to select an IAM role with permissions to run code. Select the IAM role that we created earlier in this post.

 

Select the IAM Role with Lambda Execute Permissions

 

Execute Step Functions

Now that you’ve created the “State Machine” you can execute the Step Functions. Click on the “New Execution” button if you just created your Step Function. Otherwise, you can go to the Step Functions dashboard and find the one you want to run. On the first screen, you’ll be asked to enter some inputs. Depending on your Step Functions, you may or may not need any inputs. For the example code that I used, we need a variable named “onoff” and a value which should be on or off. If you enter in any other value like “banana” the Step Function will default to the end.

 

As the Step Function runs, you’ll be able to watch the state in the console, as well as the detailed audit log and inputs/outputs.

 

If you look at the State Machine dashboard again, you’ll see the execution runs so you can keep an eye on the successes and failures.

 

Summary

You can use Step Functions for a number or reasons but they are a great way to manage Lambda functions. The pricing of step functions breaks down to $.025 cents per 1,000 state transitions and you get your first 4000 for free in the free tier.  Your Lambda functions are also not free, so remember you’re paying for those still. For more information check out the AWS documentation