Share article:

How to use Serverless framework for building workflows

Image for How to use Serverless framework for building workflows
Developers 6 min read | wrote in blog on August 2, 2019

About the Author

Hieu Nguyen is a Technical Lead at MessageMedia, interested in systems design and leading teams to achieve challenging goals. Outside of work, he keeps himself busy by reading about new technologies. Hieu loves travelling and playing chess with friends.

Introduction

In a previous post, Introduction to AWS Step Functions, you discovered the steps for coding and setting up a workflow with an AWS console. This post will show you how to organise and deploy a serverless application using a serverless framework.

What is serverless?

Serverless is another term for FAAS (Function As A Service). It’s an architecture for running code without worrying about the server or maintenance. Serverless takes care of managing a server and automatically scaling the system. This means that as an engineer, you can focus on implementing the application logic and deploying to the cloud provider.

Serverless framework is an enterprise framework that supports code deployments to various cloud providers. With the Serverless framework, you can maintain the database, policy and codebase in a serverless template and then build the whole infrastructure in a few seconds.

What is a Workflow?

A workflow is the order of steps, tasks, events, or interactions involved in the process of performing a certain task. Instead of displaying the application logic with complex code, you can use a workflow to visualise models and clearly control the logic in an application.

Step Functions is an AWS service that enables creation and management of. This service communicates with many other AWS services to build an application.

In AWS, there are many ways to manage a workflow definition. If you use an AWS console to create the definition workflow, it will be difficult and time-consuming to move the definition to another environment. Instead of creating a definition in an AWS console, you can build an environment in a few seconds using a severless framework. This method allows you to manage everything from coding to deployment in one place, saving you time and simplifying the process.

How to Build a Workflow with Serverless Framework

Let’s jump into the steps for setting up a serverless framework and an AWS CLI environment.

Prerequisites

  • Node.js v6.5.0 or later.
  • An AWS account. If you don’t have one, you can sign up for a free trial.
  • An AWS CLI environment. Please follow this article.
  • The configuration of your AWS CLI on your computer.
  • Serverless CLI v1.9.0. Please follow this article.

Step One: Create a Serverless Project

You should use the command line serverless create –template aws-python –path aws-workflow to create a new serverless project.

–template aws-python: Set up a project template with a python environment

–path aws-workflow: Project folder

# Create a new Serverless Service/Project
$ serverless create –template aws-python –path aws-workflow

The command generates two files:

handler.py: This file contains the lambda function that you use later in state-machine

serverless.yml: The file is used for the configuration

Step Two: Update the handle.py File

Update the handle.py file by copying and pasting the following code. This function returns a message with the name in the event.

def lambda_handler (event, context):
 name = event['name']
 return name + ': Go Serverless v1.0! Your function executed successfully!'

Step Three: Create a Workflow

You will need to create a workflow with a serverless file. First, you should publicise a Lambda function with the name of “handler.lambda_handler“.

Next, you need to link a task to an Amazon Resources Name (ARN), following this structure arn:aws:lambda:#{AWS::Region}:#{AWS::AccountId}:function:${self:service}-${opt:stage}-lambda_handler.

This helps the state-machine to invoke a task with AWS Lambda. Moreover, you should use two plugins for the deployment of the Step Function. They are:

serverless-step-functions: Supports deploying the Step Function

serverless-pseudo-parameters: Resolves pseudo-parameters such as AWS::AccountId

The below should be copied as the content of serverless.yml:

service: aws-workflow

provider:# Used to publicize specific configurations for Cloud service providers, such as configuring provider names, runtime environments, usage areas, etc.
 aws # This tells Server Framework that you will deploy it to AWS lambda
 runtime: python3.7 # You will use python 3.7 to run your code
 stage: ${opt:stage, 'vn'} # This is the stage of deployment. You usually have dev/qa/prod
 region: ${opt:region, 'ap-southeast-2'} # You will deploy Python at southeast-2 region
 accountId: 201922325580 # Your AWS accountId
 memorySize: 2048 # optional, in MB, default is 1024
 timeout: 900 # optional, in seconds, default is 6
 deploymentBucket:
 name: "201922325580-reports-lambda-ap-southeast-2" # The place you will deploy our code
 deploymentPrefix: appts # Prefix of package deployment
 endpointType: regional
 versionFunctions: false
 role: IamRole

functions: #You will declare specific function logic functions here.
 hello:
 handler: handler.lambda_handler # Lambda function name

stepFunctions: # Definition of workflow
 stateMachines:
 hellostepfunc1:
 definition:
 Comment: "A Hello World example of the Amazon States Language using an AWS Lambda Function"
 StartAt: GetInformation
 States:
 GetInformation:
 Type: Task
 Resource: "arn:aws:lambda:#{AWS::Region}:#{AWS::AccountId}:function:${self:service}-${opt:stage}-hello"
 End: true
 resources: # This section will declare resources so that your Functions can be used. The resource will be declared by an AWS service called CloudFormation.
 Resources:
 IamRole:
 Type: "AWS::IAM::Role"
 Properties:
 RoleName: "#{AWS::Region}-${self:provider.stage}-aws-workflows"
 AssumeRolePolicyDocument:
 Version: "2012-10-17"
 Statement:
 - Effect: "Allow"
 Principal:
 Service:
 - lambda.amazonaws.com
 Action: "sts:AssumeRole"
 Policies:
 -
 PolicyName: "jira-reports-policy"
 PolicyDocument:
 Version: "2012-10-17"
 Statement:
 -
 Effect: Allow
 Action:
 - "logs:*" # Allow to write/read to CloudWatchLog
 Resource:
 - "*" # Allow all resources in VPC access
 plugins:
 - serverless-step-functions
 - serverless-pseudo-parameters

Step Four: Deploy the Workflow Application

You need to run some basic commands with a Command-Line-Interface

sudo npm install -g serverless: Installs a serverless framework package.

sudo npm install: Installs all libraries that are required in this project.

serverless plugin install: Installs a plugin that you need for a specific provider.

Please execute the commands to install and deploy the application.

Step Five: Run a Workflow Application

After deploying the application, serverless generates a Step Functions definition on the AWS console.

{
 "Comment": "A Hello World example of the Amazon States Language using an AWS Lambda Function",
 "StartAt": "GetInformation",
 "States": {
 "GetInformation": {
 "Type": "Task",
 "Resource": "arn:aws:lambda:ap-southeast-2:201922325580:function:aws-workflow-vn-hello",
 "End": true
 }
 }
 }

If you go to Step Functions on the AWS console, you will see the workflow definition and the visualised steps as below.

To execute your Step Function, navigate to Step Functions on the AWS console and choose the Step Function you have created. In the “Executions” tab, click on the “Start Execution” button, specify an execution name and provide the input as below.

{
 "name": "Input your name here"
 }

Once you hit the “Start Execution” button, the workflow calls your Lambda function and the output is displayed. Afterwards, your state machine is executed and the colour of the state changes to blue during execution and then to green for successful completion.

Conclusion

AWS Step Function is a compilation of workflows and Lambda functions, based on the serverless architecture. The serverless framework is a powerful solution for building and maintaining code in one place. These technologies hasten the development and deployment of an application.

 

Ready to roll?

Image for Ready to roll?