Serverless fundamentals and how to build, test and deploy Lambda function to AWS
Since November 2014 when AWS introduced AWS Lambda, the term “serverless” is becoming more popular, a lot of people are talking about serverless and how to apply it to your software development.
I’ve been working with AWS for a couple of years and successfully delivered a couple of serverless projects for our customers including backend services, complex web app, microservice, data processing, event streaming. I think serverless will be one of the trends of the software development trends 2019 in relation to microservice and automation.
This article will help you understand some fundamentals of serverless and steps of how to build, test, and deploy your code automatically.
What is serverless?
Per wiki
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 we know what is serverless, but do we know when we should use serverless? With my experiences, there are some use cases that we can use serverless:
Web applications: you can 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 will connect to your backend services running on API Gateway that integrate with your Lambda function.
Backends: run you can your backend services (machine to machine service, SAP APIs, or for your mobile services). With AWS you can run 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 the logs such as CloudWatch events. It gives you elastic, scalable event pipelines without the maintenance of the complicated clusters.
- Cron jobs: you can run custom scheduled jobs without having to run and maintain a server.
- Image and video manipulation: you can use serverless to things like dynamically resize images or change the video encoding.
- MapReduce, Batch processes, workflow (i.e. StepFunction), ETL/ELT process.
Chatbots / IoT: you can use Lambda function to powering chatbot logic or voice-enabled app.
DevOps / IT automation
- Extending AWS services: you can use Lambdas function implement your custom CloudFormation resource, Cognito triggers, etc.
- Infrastructure management (CI/CD): you can use serverless to integrate and automate the CI/CD pipelines, it allows you to ship your code incrementally, bug-free and increase your productivity. For example, we can use Lambda function to implement your approval ChatOps process, Lambda function is triggered when code pushed to source control (directly or via PRs), it sends an approval request to Slack, then send back approval response from the Slack channel back to Lambda, and then kick off/reject deployment process.
The following articles provide detailed information that helps easy to understand each scenario and steps on how to implement it:
How to build, run and deploy?
Currently, we have a couple of public cloud vendors that provide service to run serverless function such as Amazon Web Service, Azure, IBM Bluemix. But in this article, we only focus on AWS one of the largest public cloud provider on the market.
When starting designing, and building serverless function, you might wonder how to deploy your function into the cloud. With AWS, there are some ways we can deploy, test and invoke your function:
- Using the AWS Console Management: we can create lambda function, upload code, add triggers, and test your Lambda function manually. You might use this way when first.
- AWS CLI: you also can 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 manual process. But this isn’t good for production and large project.
- Using frameworks: deploy can be easier with Serverless, AWS SAM, AWS Amplify, Zappa, Bref (for PHP function), Claudia, etc.
This is time for coding…
Before getting started, you need an AWS account, you can register a free-tier account.
When you signed with an AWS IAM user (you shouldn’t use the root account unless access to the billing or operations that require root privileges). This user should have IAM permission to create and manage IAM user.
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 to view detail, then select 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 to anyone that you don’t trust.
Step 2: Configure credentials
- Install the latest AWS CLI on your machine, you can follow this instruction to install.
- Configure an AWS named profile using this command
aws configure --profile slsDev
then enter Access Key ID and Secret Key (copied at step #1) and additional properties, and then finish configuring AWS credentials command.
Once you have finished configuring AWS credentials, you now can build, run locally, test and deploy your Lambda function using your desired Framework. In this article, I guide you two frameworks that we are using: Serverless and AWS SAM. We will use Node.js runtime so make sure you 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
Then you can configure serverless credentials or ignore it since we are already done at the above step.
Refer to this guide if you have any issue when installing serverless.
Step 2: Create a hello world project
Run the below command will create a Serverless project that contains a Hello World function with Node.js runtime.
sls create --template hello-world
code . // open VSCode editor
When the above command executed successfully, you should have some files created as below:
- serverless.yml: all service configurations are managed in this file. You can read more details at https://serverless.com/framework/docs/providers/aws/guide/services/
- handle.js: the Lambda function handle where you can write your custom code.
Step 3: Test your function locally
There are some ways to test your function locally before deploying into AWS. They are:
a) Test Lambda function locally using Serverless CLI
You can use Serverless CLI to invoke your Lambda function locally
sls invoke local -f helloWorld
And the result of the above command looks like:
{
"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 does and invokes your handlers.
- Install
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 above comment something like this
And then 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 this commandsls deploy — state prod
The above command using an optionstate
, with serverless, you can deploy multiple states for the same service, you can using this option to 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. You can try to send a request and 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 the AWS using this command sls remove — state prod
If you have deployed multiple states, you can remove all states by running the above comment for each deployed state.
Build and deploy a hello world Lambda function using AWS SAM framework
On the previous section, I have listed 5 steps to build, deploy, invoke/test and remove your Lambda function. Now, we will use another framework that allows us to do the same thing. This is AWS SAM (SAM stands for Serverless Application Model, an open-source framework that you can use 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, we can run below command:
sam init -r nodejs10.x -d npm -n sam-hello-world
The above command will create a SAM project with Nodejs.10x runtime and using NPM as dependencies management. The SAM hello world project structure likes below screenshot:
Likes Serverless, AWS SAM also uses a .yaml file for the service configurations, you can add/remove lambda functions as well as add your custom CloudFormation resources into this file. And another file that we have mentioned for the serverless framework is the Lambda handler file located at PROJECT_DIR/[function-name]/app.js, you will see that that the content of the handler file is completely the same as the Serverless handler file.
Step 3: Test your function locally
SAM provides a command to start your Lambda locally using Docker container exactly the same as on the AWS cloud. To start SAM locally, you can run some commands as below:
- Invoke locally: you can invoke Lambda function locally in the same way as Serverless framework by using below command:
echo '{"message": "Hey, are you there?" }' | sam local invoke HelloWorldFunction
SAM will pull, build, and start docker container and then invoke your Lambda function, you will see the result likes below screenshot:
- Start API: if you are building an API Gateway on AWS that will integrate with your Lambda function, you can run below command to start your API locally that you can send HTTP request in the same way as the
serverless-offline
plugin.
sam local start-api
- Start Lambda locally: if you are building Lambda that supports other triggers rather than HTTP, you can start and invoke it locally using below command:
sam local start-lambda
Then you can use AWS SDK to invoke Lambda function locally, see below code:
Step 4: Deploy your Lambda function to AWS
To deploy your Lambda function to AWS with SAM CLI, run have to run two commands in order as below:
- Build the SAM package: before building the package, you have to create an S3 bucket if you don’t have one. Access to AWS Console Management, then S3 and create a bucket on the region you will deploy your Lambda function. Once you have the bucket you can build the SAM package using below command:
sam package --s3-bucket innomizetech-sam-sample-us-east-1 --profile slsDev --region us-east-1 --output-template-file templat
e-export.yml
- Deploy the SAM package: after building the package, you can deploy into AWS using below command:
sam deploy --template-file template-export.yml --profile slsDev --region us-east-1 --stack-name sam-hello-world --capabilitie
s CAPABILITY_IAM
Once the command executed successfully, access to your AWS Console Management, select us-east-1 region and you will see a CloudFormation stacked has been created and deployed successfully like below screenshot:
You also can see a Lambda function has been created
Go to CloudFormation management, select the stack have been created after deploy successfully, select Outputs tab you can see some outputs that contains the URL of your API Gateway endpoint that allows you to send request.
Step 4: Remove your Lambda function
Currently, there is no available SAM command 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
Now I believe you know the basic flow to build your serverless function. In this article, I just introduced two frameworks for building serverless function, but I recommend you try others to figure out the best one appropriate for your project.
If you have any issue when practicing this article, you can get in touch with me, here is my Twitter handler @hoangleitvn
“Learn to share and Share to Learn”
Hoang Le