Make API calls from AWS Lambda to access backend database
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)


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 logginglogger = 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
