AWS Console : how to create a lambda microservice

AWS Console : how to create a lambda microservice

By |2019-01-03T17:55:03+00:00April 5th, 2018|Categories: Lambda, Step By Step|Tags: , |0 Comments

One of the most common use-cases of lambda is performing CRUD operation. The CRUD is exposed using API gateway as a lambda microservice.

In this step by step guide, we will be using the AWS Management Console to create a lambda microservice. We will use a User object for this guide.

OperationFunctionality
POSTCreate new users
PUTUpdate user details based on the unique id
GETGet user details based on the unique id
GET AllGet all users
DELETEDelete user based on the unique id

Pre-requisites

  • AWS Account, If you do not have an account follow this guide to create one
  • NodeJs, If you do not have NodeJs configured use this guide to install nodejs on your machine.

Following steps will guide us in creating a lambda microservice

1. Login to your AWS account and select a region (e.g. us-east-1)

Make sure this region will not change when working for all other services

Select region

2. Create a DynamoDB table

  1. Go to DynamoDB service.
  2. Click create table
  3. Enter table name and primary id field name

In this guide will are using an autogenerated id field to identify different users. Based on your requirements you can use any other field like “email” or “username”

DynamoDB create table homeDynamoDB create table attributesDynamoDB create table final

3. Create IAM role for lambda

3.1 Open IAM Service Console and create a new role

Create role

3.2 Select trust entity type

Create role - select trust entity

3.3 Assign DynamoDB and CloudWatch Permissions

Create role - select permissionCreate role - select permision 2

3.4 Give a name to the role and complete role creation

Create role - set nameCreate role complete

4. NodeJS code for lambda function

4.1 Configure code directory using following steps

Copy to Clipboard

4.2 copy following code to each file

create.js

Copy to Clipboard

update.js

Copy to Clipboard

list.js

Copy to Clipboard

get.js

Copy to Clipboard

delete.js

Copy to Clipboard

4.3 zip all code

Copy to Clipboard

5. Create lambda functions

5.1 Open Lambda Console and click “create function”

Create New Lambda

5.2 Specify name, runtime and IAM role for lambda

Create Lambda Parameters

5.3 Repeat step 5.1 and 5.2 to create 4 more functions

Create Lambda repeat

5.4 Open each lambda function one by one and Upload code.zip (step 4.3) as shown in the image.

Please make sure to change the handler as following

  1. create lambda => create.create
  2. update lambda => update.update
  3. list lambda => list.list
  4. get lambda => get.get
  5. delete lambda => delete.delete
create lambda - set handler

6. Configure API Gateway

6.1 Open API Gateway Console

create lambda - set handler

6.2 Create a new API

API gateway 2

6.3 Add API Post method for “users-create” lambda

API gateway 3API gateway 4API gateway 5

6.4 Add API GET method for “users-list” lambda

Repeat step 6.3 to add a new method. This time method type will be “GET” and lambda function to select is “users-list”

API gateway 6

6.5 Create a child resource for id parameter path

API gateway 7API gateway 8

6.6 While selecting the child resource create GET method for “users-get”

Repeat step 6.3 to add a new method. This time method type will be “GET” and lambda function to select is “users-list”

API gateway 8

6.7 While selecting the child resource create PUT method for “users-update”

Repeat step 6.3 to add a new method. This time method type will be “PUT” and lambda function to select is “users-update”

API gateway 10

6.8 While selecting the child resource create DELETE method for “users-delete”

Repeat step 6.3 to add a new method. This time method type will be “DELETE” and lambda function to select is “users-delete”

API gateway 11

6.9 Deploy API for use

Click Deploy API

API gateway 12

Fill in the stage details and Deploy

API gateway 12

Note down the URL

API gateway 12
Now you can test the lambda microservice API using the URL generated in step 6.9
 
To test the API you can use POSTMAN or any other API client.
 
Please post comments for clarification or any other step by step guide AWS services.

Leave A Comment

This website uses cookies and third party services. Ok