Let’s Go Serverless!! — Part II (Building Serverless Mobile Application)





  • Front End


Front end application resides on the mobile application. It’s the one that we install from the Google Play store or Apple Appstore. So that part doesn’t need the serverless concept. Back end part resides in a server hosted by the mobile application owner. Whenever API calls are made from the mobile application, those are sent to the API endpoints of the hosted server and the server sends the response. Following image shows this clearly.


Creating DynamoDB table




Let’s first create the DynamoDB table. DynamoDB is a non-relational DB service provided by AWS. If you want to know more information about DynamoDB you can go to the official documentation provided by AWS on DynamoDB. First, you need to create a table. Give the table a name and a primary key and hit on “Create”


Then you will be redirected to a page where you can manage the things related to the table that you just created. Then go to the “Items” tab and click on “Create Item”. Then you will be given the option to add new items to the table that you created. Add some sample data so that we can use in our application. So I added two items to the table.





Creating the API endpoint to be accessed from Mobile Application



So we have created the DynamoDB table and inserted some data to it. Now let’s create the API endpoint. This endpoint is accessed by the mobile application. Mobile application sends a request to fetch the next question from the DB and this endpoint calls a Lambda function (not created yet) and sends the response back to the mobile application.






Since I’m planning to send the question ID to the backend and needs to fetch the question related to that Id, I will create a child resource for that previous resource. It will look like following.




Since we have created the resources, we can now create the methods. Click on “Actions” and select “Create Method”. It will give a small grey color box where you can select the HTTP method. I will select the “GET” method and confirm it.







Creating the Lambda Function to Fetch Data from Database








Then I’ll update the existing “index.js” file to match our scenario.




Mapping Lambda Function to API Gateway

Now since we have created our Lambda function we can map it to the API Gateway. For that, we have to again go to the API Gateway Service. Select the API that we created earlier and Select the Lambda function from the Dropdown and “Save” it.



Now if you want to test your API, you can do it from here. Click on the “Test” button and you can run a test to verify whether your API endpoint is working correctly.





Deploying

Now we have to deploy the API endpoint that we just created. For that Click on “Actions” button and select “Deploy API”. When deploying you need to specify the stage of deployment. Specify a stage name and deploy the API.



After deploying, you will get an invoke URL. you can use that invoke URL as the Endpoint URL to access the data.



Following is an API call is done from Postman to verify the API endpoint.





Comments