AWS Serverless API to get data from DynamoDB without Lambda

Harshal kondke
5 min readJan 12, 2023

Introduction:

(Skip this if you are in hurry)

I wanted to fetch some data from DynamoDB for one of the applications using REST API. My obvious choice was to use API gateway as it integrates well with the rest of the AWS services. But the architecture was looking somewhat like this.

API Gateway + Lambda + DynamoDB

Having AWS Lambda in the pipeline makes perfect sense if you want to add some sort of filtration or logic on how your response is generated. But in my case and in most cases we just want to query the data from DynamoDB. That’s it. Having considered this, using AWS API Gateway’s inbuild functionality which is Integration requests we can easily eliminate the lambda and still achieve the desired outcome. This way we reduce the architectural complexity, operational overhead, and costs that come with the lambda functions.

Current Architecture.

As we eliminated Lambda, it's much more easier to understand and build.

Downsides of not using the Lambda:

  • Not able to add application-specific restrictions
  • Cannot add logic to responses
  • Cannot create custom/automated response patterns
  • Not able to have additional control over the API requests

But if you are building a simple API and do not want all the benefits which come with having a Lambda function then we are good to go.

Let's get our hands dirty:

  • Login to your AWS console and open DynamoDB Dashboard
  • Click on Create Table > add Name and partition key
DynamoDB Create Table
  • In the Table settings section select Customize setting option
  • Select Table class as DynamoDB standard and On-demand Read/write capacity.
    Using On-demand capacity is ideal in terms of costs and capacity handling.
  • Keep the rest of the settings default and hit Create Table
  • Your DynamoDB dashboard will look something like this
  • Let’s add some data to our table so that we can cross-check the API gateway API calls
  • Click on table name > Click on Explore table items button
  • Now click on Create Item button
  • Click on Add new attribute button and select the number and add sample data as shown in the picture
  • Make sure your table has data entries as follows
  • View the table and select the Overview tab and click on Additional info And copy the ARN for further use.
  • In the IAM dashboard create a new IAM policy
  • Select the service as DynamoDB and Action as Query under the read tab and in the resource, section add DynamoDB Table ARN that we copied in the previous step
  • Now create a new IAM service role with API Gateway as a service.
    once the role is created select the role and attach the game_policy that we created. In the end, it should look something like this

AWS API Gateway

  • So far we have completed all the required setup for our API gateway
  • Now, open the API gateway dashboard and select the build button under REST API.
    Note: Make sure you are not selecting the REST API private option. as it is only accessible within VPC. and in our case, we want to access this API from the Internet.
  • Select REST > select New API and fill required details as per the image and hit Create API
  • In actions, the button creates a new resource and gives the name as an id
  • Now select the /id and in the actions button create a new sub-resource as {id}
  • Click on {id} and click on the actions button and select create Method and select GET method
  • Now on the GET — Setup page fill in the details as per the below image and add the role ARN from the role that you just created and click on SAVE
  • Now click on Integration Request > Mapping Templates > and add this template
{
"TableName": "game_score",
"PrimaryKey": "id",
"KeyConditionExpression": "id = :v1",
"ExpressionAttributeValues": {
":v1": {
"S": "$input.params('id')"
}
}
}
  • Now we can test the API using the TEST button and add id and click the test
  • As you see we are getting a valid response from the API
  • We can deploy the API now
  • Click on the Actions button and select deploy and create a new deployment stage as shown in the image
  • once deployed, you will get an Invoke URL for this API copy it paste it in your browser, and add /id/gamer3 at the end
  • There you go. now we are getting data from the DynamoDB directly using API from the Internet.

A few add-on points:

  • Eliminating lambda is not the best solution out there. but it depends on what exactly you are expecting from your solution. there is some level of filtering you can do with integration templates as well. You can also add authentication to your API or you can make your API restricted to your VPC only. there are a lot of different configuration changes that one can do
  • Having solution easy to understand and implement makes your life easy in long run. it is easy to maintain and debug as well.
  • After auto-scaling and performance of a solution costs is the major part that can make it or break it over time. While designing your solution these are the factors one should consider.

Use cases of the above solution:

  • Sharing scores or games
  • Adding and displaying comments on a public forum
  • Setting up a counter for some applications online
  • and anything that you can think of.

Thanks and have a great day

--

--