This article covers steps for creating your first AWS Lambda service & functions through using Serveless framework. Before following these steps, ensure that you have already had AWS Account.
Pre-requisites:
- Ensure that you have installed latest LTS version of Node.JS. Visit this link to finding out how to install it in your machine: https://nodejs.org/en/
- Ensure that you have installed Serverless framework. If not, run this command for installing it:
sudo npm install serverless -g
Then, run this command to check whether serverless has been installed successfully or not:
serverless -v
- Create a new or reuse existing IAM User account. Ensure that you have given
AdministratorAccessto the user account. If you are not sure how to do this, follow the guidance of how to do it in this document - Upon created a new IAM User account, take note the displayed
API Key&Secret Keyof the new IAM Account. - Follow the guidance in this document to configure the aws credentials (
API Key&Secret Key) that you have noted in prior step. Serverless framework need this information so that it could deploy.
Create your first service:
- Once we have completed all of required pre-requisites, create a new folder, go into the new folder then run this command to begin creating your 1st service:
serverless create --template --path
. Example:
serverless create --template aws-nodejs --path blog

Creating 1st Service
- When creating a new service is finished, we will see file structure in the project folder, as shown in this following screenshot:
serverless.yml– a YAML file where we will define configurations for our service, such as AWS Resources (S3, DynamoDB, etc), Region, Nodejs Runtime, we want to use and also our service’s functions configurations.handler.js– Initial Javascript file , created by serverless, that is supposed to be the place where we will write our function’s logic. Rename the file’s name with name of entity that our function interacts with (e.g. blog, product, task, etc).

- Open the
serverless.ymlfile and edit these Configuration sections: lambda function’s name, handler method’s name, associated HTTP path & verb.


- Open the handler javascript file. Let’s write code inside the exported function whose the logic is simple – just returning an array of JSON objects


- Before we deploy the lambda function, let’s invoke it in our local machine through executing this command:
serverless invoke local --function
e.g. serverless invoke local --function blogs
Ensure that no error happens and we notice correct result is printed on terminal.
Deploy the service to AWS Lambda
We have implemented simple logic inside Lambda service’s handler and then invoke it locally using serverless invoke command. Now, we need to deploy our lambda through running this command in terminal :
serverless deploy --stage --region .
Example:
serverless deploy --stage dev --region ap-shouteast-1

serverless.yaml (check the events section , if you type it as event, this issue occurs).This is result that you should get when we browse the endpoint or invoke it using Postman

How the Serverless deploys our Lambda function to AWS
When we invoked serverless deploy command, serverless zipped our function file(s) and also created a file for configuring AWS CloudFormation stack setting (cloud formation template). Serverless also created a new AWS S3 bucket using our AWS API Key & Secret Key, then upload the zip file & cloudformation setting file into the created AWS S3 Bucket.

Removing Deployed Service
In case you need to destroy your deployed lambda service, the common way to do this is through destroying the resources that built your service, through AWS Web console page and then do these procedures: Open S3 page and destroy the Bucket which build the service, Destroy CloudFormation stack, Destroy the Lambda & then the related API Gateway. Serverless provides a quickest way for destroying our service along with its AWS resources. We can do this through invoking serverless remove command, inside the serverless project folder.

Conclusion
Serverless has simplified the efforts of writing AWS Lambda Function, deploying & hosting the function as an API Gateway resource endpoint. By hosting our node.js-based API on AWS Lambda, we do not need to setup an EC2 instance or other kind of virtual private server just for hosting our code. Through using Serverless & AWS Lambda, we shift this responsibility to AWS and thus, free us from responsibility of setup our own server. In the future article, we will cover steps of how to integrate our Lambda service with AWS Data storage services such as SimpleDB or DynamoDB.

Awesome article mas wendy, it is very usefull to for starting aws lambda learning
Thank you for your comment, Rik. 😀 (y)