This site runs best with JavaScript enabled.

Getting started with AWS Lambda using Serverless Framework

Mugilan Ragupathi

September 29, 2020


There are many ways to create serverless applications or services. 'Serverless Framework' is one of most widely used framework for creating services. In this article, we're going to use serverless framework to create 'hello-world' service in AWS Lambda.

AWS Lambda lets you to run your code without provisioning servers. The lambda functions would get executed when an event happens and you'll be billed only for the time that your code is executed and for the memory that you allocate for your function.

Below picture shows execution workflow of our lambda.

AWS Lambda Serverless

We're going to create a lambda function which would get executed when an HTTP event happens. It is like building an micro API where you get an endpoint and when you access that endpoint, your lambda function would get executed and return the response from your lambda function.

Enough theory. Let's start.

Pre-requisites for using serverless framework

  • Have NodeJS (preferably latest stable version) installed on your machine.
  • Install 'Serverless CLI'

Installation of serverless CLI:

npm install -g serverless

'Serverless framework' provides many templates which you can use. The templates are available at https://www.serverless.com/framework/docs/providers/aws/cli-reference/create/

We're going to use nodejs template

serverless create --template aws-nodejs --path <name-of-the-service>

We're going to create a service named dynamodb.

serverless create --template aws-nodejs --path dynamodb

When you execute above command, a folder by name 'dynamodb' would be created with 2 file - handler.js and serverless.yml

handler.js contains a 'hello' function which would return a welcome message along with the input event. 'serverless.yml' is the configuration file for your service in yaml language. By default, serverless.yml will have some commented out config examples. We can uncomment the config that we want. However, we're going to remove all the comments and add any new config as needed.

After removing the comments, we'll have below config

service: dynamodb
frameworkVersion: '1'
provider:
name: aws
runtime: nodejs12.x
functions:
hello:
handler: handler.hello

service:

'dynamodb' is the name of the service that we gave when creating

provider:

serverless can be used to create services in any cloud environment. Hence, we need to specify name of the cloud provider(aws in our case) and runtime (nodejs 12) that we would like to have for our service.

functions:

This node will have the functions that we use in our service. In our case, we've a single function by name 'hello' and it is being handled by 'handler.hello'. The code for this function resides in 'handler.js'

We're going to make below updates to the configuration

  • Memory size: You can specify memory size for every lambda function or all of your lambda functions in your service. This will impact your cost. You should try to allocate as much memory as you need for the function. In this service, we're going to use 128mb. We would change as needed.
  • Region: AWS region at which your lambda would get executed. We're going to use 'us-east1' (US East - N.Virginia)
  • Stage: Represents the environment for your service. For example, you can deploy the service in 'dev' stage. You can also set this stage when deploying the service
  • Events for our handler function: As of now, we've declared 'hello handler' but it will not be executed until we specify the trigger event for the function. AWS Lambda is an event driven service and AWS Lambda functions would be executed when associated event happens. Here we're going to use http event

Below is the updated config file

service: dynamodb
frameworkVersion: '1'
provider:
name: aws
runtime: nodejs12.x
memorySize: 128
stage: "dev"
region: "us-east-1"
functions:
hello:
handler: handler.hello
events:
- http:
method: GET
path: /hello

You can deploy above serverless function by executing following command

serverless deploy -v

When you execute above command, serverless framework would create CloudFormation stack and thus creating all the necessary resources

  • API Gateway
  • Lambda
  • IAM Permissions

Finally, you'll get output something like below

Service information

When you copy the endpoint url (https://4xzba3niwc.execute-api.us-east-1.amazonaws.com/dev/hello) into the browser, you would get the following output

Browser output

Hope you like this article.

Share article

Join the Newsletter



Mugilan Ragupathi © 2020