Build Serverless REST API on AWS from Scratch — Part II

James Murithi
Sep 23 · 4 min read

We will use AWS Amplify Framework to build our REST API, to get started install aws-cli and amplify

npm install -g @aws-amplify/cli

Create a directory and change to that dir

mkdir todo-app && cd  todo-app

Configure amplify, for this you will need IAM credentials, you should have these if you completed Part I of this tutorial. Follow screen prompts to select a suitable AWS region, paste in your IAM credentials and choose a profile name to associate with the IAM credentials

amplify configure

Run amplify init to initialize a new amplify project in the directory. Follow screen prompts. When asked if you want to use an existing profile, choose yes and specify the profile name you created in configure step above

amplify init
Use an AWS profile

If everything goes well amplify syncs your new project in the cloud

Create an API and Lambda Function

With an amplify project in place we now use the framework to add cloud resources, we will add an API and attach a lambda function to it in one step. We will tweak the function later so that it can access storage

amplify api add todoapi

Follow screen prompts to add a REST api, name it todoapi, provide a path /todo, when asked; opt to create a new lambda and name it todofunction, choose “serverless express” function template

Add DynamoDB storage

amplify storage add

Add NoSQL storage, let’s call the database tododb, follow prompts to add two string columns to the new storage

todoNumber: string

sk: string (secondary key)

The partition key should be todoNumber and secondary key sk, choose N to skip adding more columns or global secondary indexes finish this step

Associate the Lambda function with the new DynamoDB table

For this step we need to manually edit a cloud formation template. Open backend-config.json, this file should be under the path amplify/backend with your favorite editor. The folder structure at this point should look similar to what’s shown below

Add a DynamoDB dependency to the todofunction block in backend-config.json. Your todo function should look like below

Add DynamoDB dependency to todo lamba function

Push updates to the cloud, this will create our new resources to the cloud

amplify push
Summary of changes that will be pushed

Test new API from a browser

The sample code generated by amplify should have a few end points which we can invoke to test if everything is fine, to do this we first need to get our API gateways Invoke URL from console

Head to console and select API gateway service, your new API should be listed. Select todoapi, and click stages, the invoke URL should be displayed as below.

Copy invoke URL

Copy the invoke URL, paste into a browser and append /todo at the end and enter. You should receive a JSON response from your Lambda Function.

Test execution of AWS Lambda function

Grant the Lambda function DynamoDB permissions

Find todofunction-cloudformation-template.json under amplify/backend/function/todofunction and look for a property named Policy, it should contain an array named Statement that already has permissions related to logging. Add the permission object below to the array


"Effect": "Allow",

"Action": [





"Resource": [


"Ref": "storagetododbArn"



"Fn::Sub": [



"tododbRef": {

"Ref": "storagetododbArn"







At the top of the same file is a Parameters property, add two template references to this section, it should look as below

"Parameters": {   "env": {      "Type": "String"   },   "storagetododbName": {     "Type": "String",     "Default": "storagetododbName"   },   "storagetododbArn": {     "Type": "String",     "Default": "storagetododbArn"   }},

Push updates

amplify push

Part III

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