« All News

Serverless Fundamentals: How to build, test and deploy Lambda functions to AWS

Hoang Le in Code


  • SHARE

Since November 2014 when AWS introduced AWS Lambda, the term “serverless” is mounting in popularity. Many IT professionals are talking about serverless and how to apply it to software development.

I’ve been working with AWS for a couple of years and successfully delivered several serverless projects for our customers; including backend services, complex web app, microservice, data processing, event streaming. I believe serverless is shaping up to be one of the top software development trends in 2019, in relation to microservice and automation.

This article is designed to help you understand some fundamentals of serverless and the steps required for building, testing, and deploying your code automatically.

What is serverless?

According to Wikipedia, Serverless computing is a cloud-computing execution model in which the cloud provider runs the server, and dynamically manages the allocation of machine resources. Pricing is based on the actual amount of resources consumed by an application, rather than on pre-purchased units of capacity.[1] It can be a form of utility computing.

The suffix “less” doesn’t mean your code can run without a server. Serverless is a methodology for planning, building and deploying software that maximizes value by minimizing operation tasks such as provisioning servers, OS updates, software patches.

If you wanted to know more details about serverless, this article will help you.

Serverless use cases

So now that we understand serverless, when should we use it? Given my experience, there are several common use cases for serverless.

Web applications: use serverless to build your static websites or even complex web apps. For example, you can use AWS S3 to host your static websites that connect to your backend services running on API Gateway and integrate with your Lambda function.

Backends: run your backend services (machine to machine service, SAP APIs, or mobile services) serverless. With AWS you can run a Lambda function using API Gateway, or integrate with your existing Application Load Balancer.

Event / Data processing

  • Event streaming: serverless functions can be triggered by the public/subscribe topics; from logs, such as CloudWatch events. It gives you elastic, scalable event pipelines without the maintenance of the complicated clusters.
    Image Description Here
    Event Handling in AWS using SNS, SQS, and Lambda
  • Cron jobs: run custom scheduled jobs without having to run and maintain a server.
    Image Description Here
    CloudWatch Event Rules
  • Image and video manipulation: you can use serverless to dynamically resize images or change the video encoding.
  • MapReduce, Batch processes, workflow (i.e. StepFunction), ETL/ELT process.

Chatbots / IoT: you can use a Lambda function to power chatbot logic or a voice-enabled app.

DevOps / IT automation

  • Extending AWS services: you can use Lambda functions to implement your custom CloudFormation resource, Cognito triggers, etc.
  • Infrastructure management (CI/CD): you can use serverless to integrate and automate the CI/CD pipelines; this allows you to ship your code incrementally, bug-free and increase your productivity. For example, we can use Lambda functions to implement your approval ChatOps process. The Lambda function is triggered when code is pushed to source control (directly or via PRs), it then sends an approval request to Slack, which sends an approval response from the Slack channel back to Lambda, and kicks off/rejects the deployment process.

Image Description Here
Use Slack ChatOps to Deploy Your Code — How to Integrate Your Pipeline in AWS CodePipeline with Your Slack Channel

The following articles provide detailed information to easily understand each scenario, and provides steps on how to implement them.


Build, Run and Deploy

Currently, there are several public cloud vendors that provide serverless functions; companies such as Amazon Web Service, Azure and IBM Bluemix. I am going to focus on AWS; one of the largest public cloud providers on the market.

When designing and building serverless functions, you may wonder how to deploy your function into the cloud. AWS provides services to deploy, test and invoke your function.

  • Using the AWS Console Management: we can create a Lambda function, upload code, add triggers, and test the Lambda function manually. This may be the first method to use.
  • AWS CLI: alternatively, you may use AWS Lambda CLI to create, deploy, invoke, manage, monitor your Lambda function. You can use existing commands to deploy and test your Lambda function automatically without a manual process. This option however, is not ideal for production environments or large-scale projects.
  • Using frameworks: deployment can be easier with the use of Serverless, AWS SAM, AWS Amplify, Zappa, Bref (for PHP function), Claudia, etc.

Time to Code

Before getting started, you need an AWS account. If you do not already have an AWS account, you can utilize their free-tier accounts.

Sign in with an AWS IAM user. You should not use the root account unless access to the billing or operations require root privileges. This user should have IAM permission to create and manage IAM users.

Step 1: Prepare credentials

  • Create a Programmatic access IAM user and assign Administrator policy.

For your real project, you should restrict permissions using AWS managed or custom policy instead of Administrator privileges.

  • Go to IAM and select the above user you just created and view details; then select the Security credentials tab and create an Access Key. Store and copy your access key and secret key for the next step.

DO NOT share your credentials with anyone you don’t trust.

Step 2: Configure credentials

  • Install the latest AWS CLI on your machine, you can follow these instructions.
  • Configure an AWS named profile using this command: aws configure --profile slsDev. Next, enter the Access Key ID and Secret Key (copied in step #1) and additional properties, then finish configuring the AWS credentials command.

Once you have finished configuring the AWS credentials, you can build, run locally, test and deploy your Lambda function using your desired Framework. For this article, I will detail two frameworks: Serverless and AWS SAM. We will use Node.js runtime, so be sure to have Node.js ≥ 8 installed on your machine.


Build, test and deploy a hello world Lambda function using Serverless framework

Step 1: Install Serverless

npm install serverless -g

sls --version // print the installed verion i.e. 1.45.1

At this point, you can configure the serverless credentials, or if you have already done this from our previous steps, you can continue.

If you experience issues installing serverless, you may refer to this guide.

Step 2: Create a hello world project

The command below will create a Serverless project that contains a Hello World function with Node.js runtime.

sls create --template hello-world

code . // open VSCode editor

The following files will be created once the command has been successfully executed.

Image Description Here

Step 3: Test your function locally

There are several ways to test your function locally before deploying into AWS.

a) Test Lambda function locally using Serverless CLI

sls invoke local -f helloWorld

The results of this command are:

{
   "statusCode": 200,
   "headers": {
       "Access-Control-Allow-Origin": "*"
   },
   "body": "{\"message\":\"Go Serverless v1.0! Your function executed successfully!\",\"input\":\"\"}"
}

Refer to this guide for more available options of the invoke command.

b) Run API locally using serverless-offline
The serverless-offline plugin emulates your AWS Lambda function and AWS API Gateway on your machine. It starts an HTTP server that handles the request’s lifecycle, like APIG, and invokes your handlers.

  • Install the serverless-offline plugin
npm install serverless-offline -D
  • Update serverless.yml file: add the following lines to the bottom of the serverless.yml file:
plugins:
- serverless-offline
  • Then, run the serverless offline command.
sls offline

The results of the command are:

Image Description Here

You can send a request to your API running at http://localhost:3000/hello-world (port 3000 default port of the serverless-offline).

Step 4: Deploy your Lambda function to AWS

To deploy your Lambda function to AWS, run the command

sls deploy — state prod

Image Description Here

Using an option state, with the above command and serverless, you can deploy multiple states for the same service and deploy multiple environments of your Lambda function such as Dev/Staging/Prod environments.

The output of the deploy command will contain the list of endpoints of your API. Send a request to check the output.

Step 5: Remove your Lambda function
If you don’t want to run your Lambda function anymore, you can remove it automatically out of AWS using command sls remove — state prod

Image Description Here

If you have deployed multiple states, you can remove all states by running the above command for each deployed state.

Build and deploy a hello world Lambda function using AWS SAM framework

Now that you understand how to build, deploy, invoke/test and remove your Lambda function using Serverless, we will explore doing the same process with AWS SAM - Serverless Application Model; an open-source framework used to build serverless applications on AWS.

Step 1: Install AWS SAM CLI

Follow this article to install the latest version of the AWS SAM CLI on your machine.

Step 2: Create a hello world project

SAM provides a command to initialize the SAM project from a built-in template or Cookiecutter project template on GitHub or local system. To create a Hello World SAM project, run

sam init -r nodejs10.x -d npm -n sam-hello-world

This command will create a SAM project with Nodejs.10x runtime, using NPM as dependencies management. The SAM hello world project is structured as seen below.

Image Description Here
SAM Hello World project structure

As with Serverless, AWS SAM also uses a .yaml file for the service configurations. You can add or remove Lambda functions, as well as add your custom CloudFormation resources into this file.

The Lambda handler file for AWS SAM is PROJECT_DIR/[function-name]/app.js. The content of the handler file is completely the same as the Serverless handler file.

Image Description Here

Step 3: Test your function locally

  • Invoke Locally: You can invoke the AWS SAM Lambda function locally in the same way as the Serverless framework by using this command:
echo '{"message": "Hey, are you there?" }' | sam local invoke HelloWorldFunction

SAM will pull, build, and start a Docker container and then invoke your Lambda function. Below is an example of the results.

Image Description Here

  • Start API: if you are building an API Gateway on AWS that will integrate with your Lambda function, run the command sam local start-api. This will start your API locally and you can send an HTTP request in the same way as the serverless-offline plugin.

Image Description Here

  • Start Lambda locally: if you are building a Lambda that supports other triggers than HTTP, you can start and invoke it locally using:
sam local start-lambda

Then, use AWS SDK to invoke the Lambda function locally as seen below.

Image Description Here
Original Code from AWS

Step 4: Deploy your Lambda function to AWS

To deploy your Lambda function to AWS with SAM CLI, run the following two commands in order.

  • Build the SAM package: before building the package, if you don’t already have an S3 bucket, you will need to create one. To do this, access the AWS Console Management, select S3, and create a bucket in the region you will deploy your Lambda function. Once you have the bucket you can build the SAM package using the command:
sam package --s3-bucket innomizetech-sam-sample-us-east-1 --profile slsDev --region us-east-1 --output-template-file template-export.yml
  • Deploy the SAM package: after building the package, you can deploy into AWS using the command:
sam deploy --template-file template-export.yml --profile slsDev --region us-east-1 --stack-name sam-hello-world --capabilities
CAPABILITY_IAM

Once the command is executed successfully, go to your AWS Console Management, select us-east-1 region, and you will see a CloudFormation stack has been created and deployed successfully.

Image Description Here
Lambda Function

You will also see a Lambda function has been created.

Image Description Here
CloudFormation Stack

Go to CloudFormation management, select the newly created stack, then select the Outputs tab. You will see the outputs that contain the URL of your API Gateway endpoint that allows you to send the request.

Image Description Here

Image Description Here

Step 5: Remove your Lambda function
Currently, there is no SAM command available to remove your Lambda resource on AWS. However, you can use CloudFormation CLI to delete your stack. The result of the delete stack command is all associated resources created from the stack will be deleted as well.

aws cloudformation delete-stack --profile slsDev --region us-east-1 --stack-name sam-hello-world

Summary

We have covered the basic flow to build your serverless function, and I introduced two frameworks for building serverless function. However, I recommend you also try other frameworks to determine which is appropriate for your project.

Should you run into issues while practicing the steps outlined in this article, I encourage you to reach out to me. You can get in touch with me through my Twitter handle @hoangleitvn

Wikipedia contributors. (2019, August 13). Serverless computing. In Wikipedia, The Free Encyclopedia. Retrieved 14:39, August 21, 2019, from https://en.wikipedia.org/w/index.php?title=Serverless_computing&oldid=910660059


« Back to All News