In my previous article, I talked about what Serverless is. In case you missed it, I suggest you to read it in here. 😄
So in this article I’m going to tell how to create a simple serverless function with AWS Lambda in a simple way. 😉 If you are familiar with AWS Lambda Console, you know the pain of creating and managing the Lambda function. Fortunately there are now so many frameworks that we can use to reduce our workload. When you start to use a framework you will know how simple it is.
Let me list down some of the framework that we can use.
From all of the above, I have been using the Serverless Framework for like a year to deploy my Lambdas. I found it’s really easy to use and configure. Also, there are lot a plugins for that framework by its’ great community. 🙇♂️
If you want to use another provider instead of AWS, it’s really easy to migrate. You just need to modify few lines and you are good to go. It’s that simple. 😉
So, in this article I’m going to show you how to create and deploy a simple NodeJs Lambda function with Serverless Framework. Let’s get started!
Firstly, we need to install the serverless framework using the following command.
npm install -g serverless
After installing the npm package, we can either use serverless
or sls
which are equivalent commands.
Now we need to configure the AWS IAM User credentials for Serverless Framewrok. To do that run the following command. If you don’t know how to get those, you can refer this guide.
serverless config credentials --provider aws --key AWS_ACCESS_KEY --secret AWS_SECRET
To create a serverless project, they have given us boilerplates for each language and the providers they support. You can find all of them here. In this case we are going to initiate the project with the following command for NodeJs.
serverless create --template aws-nodejs --path simple-lambda
Now we have two files named handler.js
and serverless.yml
inside the directory. The handler.js file basically contains our lambda function code. Here we can add more functions as much as we need for our project.
1 2 3 4 5 6 7 8 9 10 11 |
'use strict'; module.exports.hello = async (event) => { return { statusCode: 200, body: JSON.stringify({ message: 'Go Serverless v1.0! Your function executed successfully!', input: event, }, null, 2), }; }; |
The serverless.yml is where the magic happens. This file contains the configuration of our project where it defines the details of the service provider, environment variables, functions, resources, serverless plugins etc.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
service: simple-lambda provider: name: aws runtime: nodejs8.10 stage: dev region: us-east-1 functions: hello: handler: handler.hello events: - http: GET / |
For every function that you write in the handler.js file, you need to configure here. Basically that configurations are responsible for creating API Gateway routes for the functions.
So what if we want to test the code that we wrote is working? For that we can use the following command;
serverless invoke --function hello --log
This command will run our hello function and give us the logs.
Okay, then what about if we need a local API for all the functions?
This is where a plugin comes handy. We can use a plugin called serverless-offline
which will help us to run a local API for our project. Installing a plugin is simple. You just need to install the npm package and add the plugin name to the serverless.yml.
npm install serverless-offline
After adding the plugin to the serverless.yml, it should look like this;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
service: simple-lambda provider: name: aws runtime: nodejs8.10 stage: dev region: us-east-1 functions: hello: handler: handler.hello events: - http: GET / plugins: - serverless-offline |
Now you can run the following command to create the local API for your project.
serverless offline start Response: Serverless: Starting Offline: dev/us-east-1. Serverless: Routes for hello: Serverless: GET / Serverless: POST /{apiVersion}/functions/simple-lambda-dev-hello/invocations Serverless: Offline [HTTP] listening on http://localhost:3000 Serverless: Enter "rp" to replay the last request
Now you can make a GET request to http://localhost:3000/
and see the response.
Once we are done with testings, we can use the following command to deploy the our code to AWS.
serverless deploy Response: Serverless: Packaging service... Serverless: Excluding development dependencies... Serverless: Creating Stack... Serverless: Checking Stack create progress... ..... Serverless: Stack create finished... Serverless: Uploading CloudFormation file to S3... Serverless: Uploading artifacts... Serverless: Uploading service simple-lambda.zip file to S3 (386 B)... Serverless: Validating template... Serverless: Updating Stack... Serverless: Checking Stack update progress... ........................... Serverless: Stack update finished... Service Information service: simple-lambda stage: dev region: us-east-1 stack: simple-lambda-dev resources: 9 api keys: None endpoints: GET - https://oujlmpmbt1.execute-api.us-east-1.amazonaws.com/dev functions: hello: simple-lambda-dev-hello layers: None
This will deploy all the functions, resources, events that you defined in the serverless.yml.
Yey! Now you have an endpoint for your deployed Lambda function.
If you want to see the logs of your deployed Lambda function you can use;
serverless logs --function hello Response: START RequestId: 7ffe81b5-3221-46cd-aaab-61ce7921148e Version: $LATEST END RequestId: 7ffe81b5-3221-46cd-aaab-61ce7921148e REPORT RequestId: 7ffe81b5-3221-46cd-aaab-61ce7921148e Duration: 2.46 ms Billed Duration: 100 ms Memory Size: 1024 MB Max Memory Used: 59 MB Init Duration: 1.68 ms
Visit the official docs if you want to find out more about the logs.
Okay, what if you want to remove the functions which you deployed? Again, it’s just one simple command.
serverless remove Response: Serverless: Getting all objects in S3 bucket... Serverless: Removing objects in S3 bucket... Serverless: Removing Stack... Serverless: Checking Stack removal progress... .......... Serverless: Stack removal finished...
Look how simple was that. Now you can really focus on your code and make your life easier with AWS Lambda.
That’s all folks. Happy Coding! ☺️