Make API calls from AWS Lambda to access backend database

Shawn Shi
Shawn Shi
Sep 3, 2018 · 3 min read

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
- use python Set to get all the unique userId from the response data

import json
from botocore.vendored import requests
import logging
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
def lambda_handler(event, context):
''' Demonstrates a simple HTTP request from Lambda '''
response = requests.get('https://jsonplaceholder.typicode.com/posts')
posts = json.loads(response.text) #load data into a dict of objects, posts
logger.debug('posts is a = {}'.format(type(posts)))
# Let's get the unique userId, there should only be 1-10
unique_ids = set()
for post in posts:
unique_ids.add(post['userId'])
logger.debug('unique_ids = {}'.format(unique_ids)) return True

Step 3 — Config a test event and run a test on your Lambda function. It does not really matter what goes in the test event data, as we don’t use it. Thus, we just use the Hello World template.

Congratulations! You have created a Lambda function and made an HTTP GET request from it! On a side note, you have also used Python Set to filter unique userIds from the response data. Algorithm-wise, this is O(N) and very efficient way to retrieve unique values!

More things to consider before you decide to deploy on production:
- protect APIs if they are only designed to be called from your Lambda
- config Lambda Trigger event
- return proper data instead of simply True in the demo above

Shawn Shi

Written by

Shawn Shi

Machine Learning Engineer, Web App Developer, Big AWS fan! When I am not dived into data and code, I am outside rock climbing!

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade