Make API calls from AWS Lambda to access backend database

Shawn Shi
3 min readSep 3, 2018

If you use AWS Lambda and need to access your backend database, API calls would be very useful!

I have recently built a chatbot that triggers AWS Lambda to interact with the backend database. This approach works nicely as the chatbot and Lambda are separate components from the actual application, making things cleaner and easier for maintenance.

In this article, I’d like to share a template that you can use to make HTTP GET request to your application REST APIs and retrieve data from the database. You can follow similar logic to make POST, DELETE, PUT requests too.

Step 1 — Login to your AWS account and create a Lambda function. For demo purpose, we can just use “Author from scratch”. Note Lambda asks for an IAM role, you can create a new role, or pick an existing one (lambda_basic_execution role in this example)

AWS Lambda homepage
Lambda function creation page

Step 2 — Copy code below and replace existing code in your lambda_function. Pay attention to indent.

What the code below does is to:
- make an HTTP GET request to a public API and retrieve a list of items in Json format
- load the json data into a python dictionary

--

--

Shawn Shi
Shawn Shi

Written by Shawn Shi

Senior Software Engineer at Microsoft. Ex-Machine Learning Engineer. When I am not building applications, I am playing with my kids or outside rock climbing!