API, AWS, Cloud Computing, Tutorials

7 Mins Read

Integrating AWS API Gateway, Lambda and DynamoDB

In the current world of API, every mobile application and website have to communicate using dedicated API servers. These dedicated servers are explicitly set to handle the API calls for an application. API servers act as an intermediary between the application and the database. The bottleneck of this setup is that the API server has to be maintained to handle all the API calls. The increase in the number of API calls, increases the load of the API server which may require auto-scaling, which is cost-consuming.

1

 

The latest approach of the best architects is to utilize a new AWS service that explicitly replaces the need for a dedicated API Server. AWS API Gateway provides the ability to act as an interface between application and database which uses AWS Lambda function as the backend.

 

2

 

To get the essence of AWS API Gateway, we need to get hands-on with it. The next part of the blog is a detailed tutorial on how to use AWS API Gateway along with AWS Lambda & DynamoDB.

People who are familiar with DynamoDB, API Gateway and Lambda can proceed with the high-level instructions. Also for people who are new to these services, there are detailed instructions which can be followed for step-by-step guidance.

The first step will be to create a DynamoDB table which stores the data. Next a Lambda function which inserts the data into DynamoDB table. An API Gateway is setup to trigger the Lambda function. Finally a REST Client will be used to call the API.

Step 1: Create a DynamoDB table:

3

Create a DynamoDB table named Employee with the attributes of primary key as emp_id. Accept the rest of attributes as default and review the table details.

 

[showhide type=”dynamodb” more_text=”For Detailed Instructions..” less_text=”Hide Details” hidden=”yes”]

  1. Go to console.aws.amazon.com and sign into your account. On the services dashboard select DynamoDB. Click on Create Table.Create Table
  2. On the next screen
    • Enter Employee as the Table Name.
    • Select Primary Key Type as Hash.
    • Provide the Hash attribute Name (like an Primary key) as emp_Id then click Continue.Table Detail
  3. Skip the Global indexes settings on the next screen.
  4. On the Provision Throughput Capacity screen, leave the read and write capacity as 1 then click Continue.Provision Throughput
  5. Finally review the settings and click Create table.Review Table

[/showhide]

 

Step 2: Create a Lambda Function:

 

4

 

Create a node.js Lambda function called addEmployee. Use code from the following location, https://s3-ap-southeast-1.amazonaws.com/cloudthatcode/addEmployee.zip. Replace IAM access and secret keys of a user with access to write into the DynamoDB table.

 

[showhide type=”lambda” more_text=”For Detailed Instructions..” less_text=”Hide Details” hidden=”yes”]

  1. On the Lambda service page click on Get Started Now.Lambda Getting Started
  2. On the next screen, click Skip.skip Blueprint
  3. On the next screen
    • Enter the name of the function as  addEmployee.
    • Enter a description for the function.
    • From the dropdown select the runtime as Node.js.lm3
  4. Scroll down to the bottom of the screen and do the following
    • Select code entry type as Edit Code Inline.
    • Download the sample code by visiting this URL  https://s3-ap-southeast-1.amazonaws.com/cloudthatcode/addEmployee.zip.
    • Paste the sample code provided into the editor as shown below.
    • Create a user in IAM with access to write into the DynamoDB table.
    • Replace IAM access and secret keys of that user into the code and save.lm4
  5. Next scroll down further and on the following
    • Leave the handler as default.
    • For IAM Role, select Create New IAM Role, select DynamoDb event stream Role, which will redirect you to the IAM Management Console.
    • Enter the name as dynamodb-Access and click Allow.lm5
  6. Under Advanced settings leave everything as default and click Continue.lm6
  7. Finally review the function click Create function.lm7
  8. Now we will test the function.
    • Click on Test .
    • Provide the parameters for the Lambda Function as given below and submit.lm9
  9.  The Execution result  will show as succeeded and the record will be inserted in the Employee table.lm10

[/showhide]

 

Step 3: Create an API in API Gateway :

 

6

Next create an API called Employee_API, a resource called employee and create a POST Method. Use Lambda as the integration type and select the Lambda function you created earlier.

Test the API with the following values from the console.

 

 

You should see the entries in the DynamoDB table.

For creating an API we need to create Resource, create POST method under the resource and utilize the Lambda function as integration type and the Deployment of API is up for launch.

 

[showhide type=”apigateway” more_text=”For Detailed Instructions..” less_text=”Hide Details” hidden=”yes”]

For creating API we will Create Resource, create POST method under the resource and we will provide the Lambda function as integration type and then we will Deploy the API.

  1. Go to the API Gateway service page and click on Get Started.ag1
  2. Enter the API name as Employee_API.
  3. Provide a Description and Create API.ag2
  4. On the top right of the screen click on Create Resource.ag3
  5. Provide the Resource Name as employee and click Create Resource.ag4
  6. Click on Create Method.ag5
  7. Select POST Method .ag6
  8. Next do the following
    • Select Lambda Function as the Integration type.
    • Select the Lambda Region and the Lambda Function you created earlier.
    • Click Save.ag7
  9. Grant the Permission changes for the API lambda function by clicking OKag8
  10. The API has been created.ag9
  11. Next Test the API.
  12. Click on TEST in the console as shown below.ag10
  13. Provide payload as given and click Test.ag11
  14. You should will get a success response and check if the record has been inserted into the table.ag12
  15. We will now Deploy the API.
  16. Click on the Deploy API.ag13
  17. Next do the following
    • Select New Stage.
    • Provide Stage Name and Description and click Deployag14
  18. Once the Stage has been deployed and you will get the invoke URL.ag15

[/showhide]

 

Step 4: Call the API using REST Client

 

7

Test the API setup. For this Install a REST client. There are extensions for Mozilla firefox and Chrome which can be used which can be use to make REST calls. Install the one specific to your Browser. Invoke the API URL with the following values

 

 

Check the DynamoDB table again for the entries.

The response from the DynamoDB is processed by the Lambda function and routed via the API Gateway.

[showhide type=”restclient” more_text=”For Detailed Instructions..” less_text=”Hide Details” hidden=”yes”]

  1. Install the REST client for Mozilla/Chrome. Go to Add-ons/extensions and search for RESTClient and Install.
  2. Select the http POST Method
  3. Enter the Invoke URL as the URL. Add your Resource Name i.e. employee to the end of the URL.
  4. Enter the Payload as provided.
  5. Send the Request.ag16
  6. You should see the a Successful response and the final employee entry added to the Employee table.ag17

[/showhide]

 

With minimal effort a REST API has been created which accepts data,  which in turn gets processed by Lambda function and finally stored in a DynamoDB table. This API is ready for use and can be used in any application.

The API gateway frees the developer from maintaining infrastructure for APIs. On the whole AWS API Gateway is a beneficial package for the developer. The API gateway provides nifty features where we can create different stages for development. It also has a useful testing platform which can be used to test the calls. It also provides caching and monitoring.

In my next blog I have talked about securing your API’s in API gateway, passing Query string & headers, transforming response type and returning the custom error code.The hands on is included. If you have any queries on API gateway or you have trouble getting it setup, feel free to drop a question into the comment section below or you can also ask the questions to https://forum.cloudthat.com

Thank you.

 

 

Voiced by Amazon Polly

WRITTEN BY CloudThat

CloudThat is a leading provider of cloud training and consulting services, empowering individuals and organizations to leverage the full potential of cloud computing. With a commitment to delivering cutting-edge expertise, CloudThat equips professionals with the skills needed to thrive in the digital era.

SHARE

Comments

  1. Ashish

    May 14, 2019

    Reply

    S3 Bucket where lambda code is uploaded is not accessible. Can you please share it.

    https://blog.cloudthat.com/api-gateway-lambda-dynamodb/

  2. shubham

    Sep 3, 2018

    Reply

    no able to access s3 bucket for lambda code

  3. Lekha

    Aug 13, 2018

    Reply

    Cannot download the code from the URL!

  4. sidu

    Mar 2, 2018

    Reply

    awesome one.. very clear. need more such topic pls.

  5. Akshata

    Oct 18, 2017

    Reply

    Can fetch data from two tables(Dynamodb) in lambda function

  6. Umesh

    Dec 3, 2016

    Reply

    Is it a good idea to have ELB, just to ensure heavy traffic is equally distributed to API Gateway URL/Endpoint/URI?

  7. mounika

    Jun 2, 2016

    Reply

    can we connect mongoDB to lambda function in aws api gateway instead of dynmodb

    • Bhavik Joshi

      Aug 5, 2016

      Reply

      Yes, you can actually do that but you need to create package with all the dependencies and upload the zip file of the package to lambda. If you need any help post question to https://forum.cloudthat.com or comment here.

      Thank you.

  8. JohnA

    Oct 23, 2015

    Reply

    Can you integrate the RDS as you have done with the DynamoDB to use Lambda w/API Gwy?

    • Bhavik Joshi

      Oct 26, 2015

      Reply

      Yes, you should definitely be able to do it. Give it a try and let me know in case you have any questions, also feel free to post your questions on https://forum.cloudthat.com

  9. Théo

    Oct 5, 2015

    Reply

    Hello,

    I’m following exactly your tutorial but with my db fields( course_id and title ), bu t i have a error message when I test the function : Here is the error with this request body : {
    “course_id”: 3,
    “title”: “hello”
    }. And the error message is : ” “errorMessage”: “One or more parameter values were invalid: Missing the key course_id in the item” . Can you help me please ? Have you an idea ?

    • Bhavik Joshi

      Oct 5, 2015

      Reply

      have you tried giving double quotation to 3 like “3”, if it works.

  10. Mousumi

    Sep 16, 2015

    Reply

    How to send apiKey in rest client request?

    • Bhavik Joshi

      Oct 5, 2015

      Reply

      Hi Mousami, I would be writing a blog on advanced topic related to API Gateway soon so you can find the instructions there.

    • Bhavik Joshi

      Oct 26, 2015

      Reply

      Hey Mausami,

      I have written a blog on how can you use API Keys, you can have a look at it here https://goo.gl/mRQUsG

  11. Agam Shah

    Aug 20, 2015

    Reply

    very helpful and well described each details with images…Thanks Bhavik Joshi…

  12. Heer

    Aug 20, 2015

    Reply

    Nice

  13. Heer

    Aug 20, 2015

    Reply

    Good job Mr.Bhavik joshi

  14. Nirmal

    Aug 19, 2015

    Reply

    awsome ..thanks for sharing something good and interesting..

  15. Jayesh Thanki

    Aug 18, 2015

    Reply

    Great work.

  16. Mansi Halani

    Aug 18, 2015

    Reply

    Nice job Bhavik

  17. Sagarika

    Aug 18, 2015

    Reply

    Hey you’ve described it well.

  18. Nikunj Panchal

    Aug 18, 2015

    Reply

    Good job…

  19. Jack

    Aug 18, 2015

    Reply

    Thanks buddy..

    You makes it quite easy..

    • Jack

      Aug 18, 2015

      Reply

      Also..awesome salary assumption for jack. Kidding 🙂 thanks.

  20. Sandhya Gor

    Aug 18, 2015

    Reply

    Nicely written in descriptive manner.. Anyone can easily understand.

  21. Click to Comment

Get The Most Out Of Us

Our support doesn't end here. We have monthly newsletters, study guides, practice questions, and more to assist you in upgrading your cloud career. Subscribe to get them all!