Using various services of AWS to build an end-to-end Web Application...
Tags: devops, aws
Published: 2023-03-15 15:52:04


**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:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oe6ce9yn6giiiay1jsbp.jpg)


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](https://github.com/shravani10-tech/End-to-End_Application_Using_AWS)

Prerequisite: Create a free tier AWS Account [Click here](https://youtu.be/XhW17g73fvY)

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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fucupjsvuk3es1ydnqcx.png)
- Click on Deploy with Git Provider and continue..

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g4ola9h1vny3mush05jz.png)

- Starting with the manual deployment
  1. Name your application
  2. Write environment name as (dev) 
  3. Select Method Drag and Drop
  4. Drag a zip file of (index-ORIGINAL.html) page you want to host ( 
     Code/File uploaded on github ) 
  5. Click save and deploy!!

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h1gzj4gitjqgzorkdg3u.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l4mbciho0uxd1elhwx3t.png)

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..

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cbtyk3lxqx9ppzel3xzj.png)

Congratulations on Successful Deployment...

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0nmtm0ukrj21jc5av4mr.png)

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...

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/td8l28nntnz55obpwq12.png)

1. Click of create function ( top left corner )
2. Select ( Author from Scratch )
3. Give your function a name ( eg. powerofmathfunction )
4. Select Runtime as Python 3.9
5. Keep the rest as default 
6. Click on create function

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ojvprqa2fm80y0s27eod.png)

Replace the code with the ( PowerOfMathFunction-Lambda-Original ) code provided on Github. We will update the code later .

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5dhssgr3yfmalngmpz18.png)
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.
1. Click on the drop down arrow near Test button
2. Select Configure test event

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5p2ywnsn07nevb6g73pp.png)

3. Click on create new event. 
4. Enter the Event Name.
5. Down below pass the values ( base and exponent ) as 2 and 3.
6. Scroll down and click on Save.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/61jg64ckre09h7xsjvum.png)

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..

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/txdw40n5h56huy3xzrsa.png)
1. Click on Create API
2. We will be using REST API, click on Build Button.
3. Give a name to your API.
4. Keep rest as default and click on Create API Button.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/87qveb2rg89je7tpbcuq.png)
This is an empty API created, there are no methods that somebody will call. Let's do that next...

5. In the left select Resources.
6. Select the backslash (\)
7. On the actions menu, select ( Create Method )
8. Select the type of Method as POST( user will be sending the values).
9. Click the checkmark.
10. Enter your Lambda Function name.
11. Click on save.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vdufw90gkf1xl4xer1pl.png)

12. 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)

13. Enable CORS and Click on YES.
14. In the actions, select deploy API.
15. Give Deployment stage as New Stage.
16. Give Stage name as dev.
17. Click on Deploy.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dprg0wwbm18oipm3lj82.png)

Copy the invoke URL and save it in the text file.

To test if the API is working, 
18. Go to Resources, Click on Post and Select Blue lightning bulb below Test.
19. Pass the Base and the Exponent values in the Request Body.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pbnrfb2nuq3t00rl6bgh.png)
20. 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.

1. Search for DynamoDB in the search bar

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/45wa8wnvfge6d94hpqxf.png)
2. Click on Create table.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x2xwa62ytctsd85f69re.png)
3. Enter Table Name and Partition key
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ilj0mcs9qdxuja7y7h4x.png)
4. Leave the rest as default and Click on Create Table.
5. Click on the table which is you have created, in the General information, go to Additional info and copy the ARN provided.
6. 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.

1. Go back to Lambda Function.
2. In the Configuration section, Click on Permissions and the on the Execution Role Provided.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6kmb3yu3rrpgppajlvzl.png)
3. In the Add permissions, click on create inline policy.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lz3j9k78vikq7b866kir.png)
4. 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

5. Click on Create Policy and give name to the policy and again click on Create Policy.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fpjrojpelko4g2po20n0.png)

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.
7. 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.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ygnyk2ec7frlivsrgegn.png)
8. Save and Deploy the code.
9. Test your function again.
10. Now, go back to DynamoDB, click on Explore table Items.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rr0uc2zlw4q9myn8vber.png)
11. Scroll down. You will now see the results stored in our Database.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/iidny9f49oyyrx5bzxw0.png)

Step 6: Let's now connect our AWS Amplify to AWS API Gateway!!

1. We will now update the index.html page 
2. Open the index.html file in notepad++

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mls8tltiuuahmaedr33s.png)
3. Edit the code ( Paste the final code provided on Github as index.html )
4. Enter the API Gateway URL copied earlier when creating the API Gateway.
5. Update the code with your API URL.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t5d4af5yk1p5luvn5vif.png)
6. Create a zip file. 
7. Come back to AWS Amplify, add the updated/new zip file and click on deploy.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2oit8u2xgyj9grphx25q.png)
8. Click on the domain link and there you go....

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tn002yr2xathg8caq9sh.png)

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.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5ksasdxcczd637cfnz8u.png)


Thank you for reading. All suggestions are appreciated!