Learning theory is simple, but putting it into practice is difficult and essential to really grasp the topic.
In this blog, we will be building a simple Web Application using various services of AWS.
Workflow of the Project:
Understanding the services which we will be using for building web application.
AWS Amplify: To create/host webpage.
AWS IAM: To handle various permissions and accessing request across services.
AWS Lambda: To run your code for any type of application.
AWS DynamoDB: NoSQL Database used to store and retrieve the data
AWS API Gateway: To route HTTP request to Lambda/To invoke the Lambda Functionality.
Github: Repository for the Code
Prerequisite: Create a free tier AWS Account Click here
Let's get started!!
We are going to make a simple "Power of Math" Application to understand these services
Step 1: Let's deploy our code to AWS Amplify for building and hosting our webpage
- Search for Amplify in the search bar
- Click on Deploy with Git Provider and continue..
- Starting with the manual deployment
- Name your application
- Write environment name as (dev)
- Select Method Drag and Drop
- Drag a zip file of (index-ORIGINAL.html) page you want to host ( Code/File uploaded on github )
- Click save and deploy!!
Now you can see the Domain Link which you can share with your friends once the project is completed!!
This is how your live page should look where the users can navigate to..
Congratulations on Successful Deployment...
Step 2: We will now use Lambda Function which will help us to do calculations for our application.
Lambda function is a bit of code that runs and responds on some trigger.
In our project, the input values given by the user will trigger the lambda function to return the results for the inputs.
Lambda Functions are serverless (you don't have to manually setup any server) AWS manages everything behind the scenes.
We will use Python "math" Library to do the necessary calculations.
For setting up Lambda Function,
Search for Lambda in the search bar...
- Click of create function ( top left corner )
- Select ( Author from Scratch )
- Give your function a name ( eg. powerofmathfunction )
- Select Runtime as Python 3.9
- Keep the rest as default
- Click on create function
Replace the code with the ( PowerOfMathFunction-Lambda-Original ) code provided on Github. We will update the code later .
Importing json utility library and python math library to do the calculations.
User will be passing two numbers ( base and exponent ) we will grab those values through the event object and plug in them into ( math.pow) for power which will give us the result , later we will return the result down in the json object.
Save the code!!
Click on Deploy.
Now let us Test our Function.
- Click on the drop down arrow near Test button
- Select Configure test event
- Click on create new event.
- Enter the Event Name.
- Down below pass the values ( base and exponent ) as 2 and 3.
- Scroll down and click on Save.
We have successfully configured the Test Event. Now to run the test, Click on Test button and check the results. It should now return "Your test result is 8.0".
Step 3: To invoke the Lambda Function we need a public endpoint or URL that can be called to trigger that function to run.
API Gateway provides tools for creating and documenting web APIs that route HTTP requests to Lambda functions.
An API gateway is an API management tool that sits between a client and a collection of backend services.
Let's build our own API!!
Go ahead search for API Gateway in the search bar..
- Click on Create API
- We will be using REST API, click on Build Button.
- Give a name to your API.
- Keep rest as default and click on Create API Button.
This is an empty API created, there are no methods that somebody will call. Let's do that next...
- In the left select Resources.
- Select the backslash ()
- On the actions menu, select ( Create Method )
- Select the type of Method as POST( user will be sending the values).
- Click the checkmark.
- Enter your Lambda Function name.
- Click on save.
- Select Post and click on Enable CORS( Cross origin resource sharing ).
Web application running in one origin or domain(Amplify) will be able to access resources from different origins or domains(Lambda, Dynamodb etc)
- Enable CORS and Click on YES.
- In the actions, select deploy API.
- Give Deployment stage as New Stage.
- Give Stage name as dev.
- Click on Deploy.
Copy the invoke URL and save it in the text file.
To test if the API is working,
- Go to Resources, Click on Post and Select Blue lightning bulb below Test.
- Pass the Base and the Exponent values in the Request Body.
- Click on Test. You should get the result as 16.0.
Step 4: Creating a Database to store/return the math results.
We will be using AWS DynamoDB.
Amazon DynamoDB is a fully managed, serverless, key-value NoSQL database designed to run high-performance applications at any scale.
- Search for DynamoDB in the search bar
- Click on Create table.
- Enter Table Name and Partition key
- Leave the rest as default and Click on Create Table.
- Click on the table which is you have created, in the General information, go to Additional info and copy the ARN provided.
- Save the ARN in a text file.
Step 5: Now we have to give permissions to our Lambda Function to write to this table using AWS IAM.
- Go back to Lambda Function.
- In the Configuration section, Click on Permissions and the on the Execution Role Provided.
- In the Add permissions, click on create inline policy.
- Click on the JSON tab and add the below policy
{ "Version": "2012-10-17", "Statement": [ { "Sid": "VisualEditor0", "Effect": "Allow", "Action": [ "dynamodb:PutItem", "dynamodb:DeleteItem", "dynamodb:GetItem", "dynamodb:Scan", "dynamodb:Query", "dynamodb:UpdateItem" ], "Resource": "YOUR-TABLE-ARN" } ] }
With this policy, the Lambda Function will have the permissions to Put, Delete, Get, Scan, Query, and Update Item in DynamoDB.
DONOT FORGET TO ADD THE TABLE ARN WHICH YOU COPIED EARLIER
- Click on Create Policy and give name to the policy and again click on Create Policy.
Now let us update the Lambda Function to go and write to the database!!
6 Copy the (PowerofMathFunction-Lambda-Final) code from the file provided on Github.
- Go to the Lambda Function and paste the new code.
MAKE SURE YOU CHECK THE TABLE NAME OF YOUR DATABASE AND UPDATE IT IN THE CODE.
- Save and Deploy the code.
- Test your function again.
- Now, go back to DynamoDB, click on Explore table Items.
- Scroll down. You will now see the results stored in our Database.
Step 6: Let's now connect our AWS Amplify to AWS API Gateway!!
- We will now update the index.html page
- Open the index.html file in notepad++
- Edit the code ( Paste the final code provided on Github as index.html )
- Enter the API Gateway URL copied earlier when creating the API Gateway.
- Update the code with your API URL.
- Create a zip file.
- Come back to AWS Amplify, add the updated/new zip file and click on deploy.
- Click on the domain link and there you go....
Your very own end-to-end application using different AWS services is ready!!
On our html page, as soon as the user inputs the values and clicks on results that script is going to call the API Gateway that triggers the Lambda Function to do the calculations and the result gets written to the database and we will get the message returned to us in the browser showing us the results through API Gateway.
Thank you for reading. All suggestions are appreciated!
Top comments (1)
Wow what a coincidence looks quite similar to what she did in this video