# AWS 2-tier architecture

### **Deploying a 2-Tier Application on AWS EC2**

**1\. Login to AWS**

* Access your AWS Management Console.
    

**2\. Infrastructure Setup**

**Provisioning an EC2 Instance:**

* Launch an EC2 instance to host your application.
    
    \-ubuntu image
    
    \-allow http, https and ssh access
    
    \- remaining, default settings
    
* Instance is created
    

**3\. Database Layer**

**Creating a DynamoDB Table:**

* Set up a DynamoDB table (in this case," `Users`") for storing user data.
    
* ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1724003149302/7beb90e2-5fd1-4e23-aefe-3f3fe4b488b6.png align="center")
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1724003217032/e392d62e-4eed-4e26-8a01-492efc95f508.png align="center")
    
    Rest, default. Create Table
    
* ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1724003277973/978c99d0-6c73-4f98-86ec-741852b5e756.png align="center")
    

**Configuring IAM Role for DynamoDB Access:**

* Create and attach an IAM role to your EC2 instance with DynamoDB full access.
    
* Navigate to IAM SERVICES
    
* ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1724003347242/7e00029f-d787-4d30-910e-1537eab333e1.png align="center")
    
    \- Select ROLES&gt; CREATE ROLES
    
    \-Select EC2 Services
    
* ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1724003395160/c8479623-e874-4a1c-a3d1-318f6c02f535.png align="center")
    
    \-Select dynamodbfullaccess role
    
* ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1724003425197/6783f690-a464-438e-b072-ffc6b85bb23b.png align="center")
    
    \- Role Successfully Created
    
    \-Assign Role to EC2 instance
    
    \-ACTIONS &gt;SECUIRTY &gt; MANAGE ROLES
    
* ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1724003504694/d9bf11f7-3df4-4f0e-8757-f6b5781b3809.png align="center")
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1724003527214/01624a14-35e0-4524-b8ae-84ee30fcd55a.png align="center")
    
    Select the role u have created.
    
* **4) Application Layer**
    
    **SSH into EC2 Instance:**
    
    * Connect to your EC2 instance using SSH.
        
    * **Update and Upgrade Packages:**
        
        `sudo apt-get update`
        
        `sudo apt-get upgrade`
        
    * **Install Node.js:**
        
        ```plaintext
        sudo apt install nodejs
        ```
        
        **Clone the Project Repository:**
        
        ```plaintext
        git clone <repo-url>
        ```
        
        Repo Url:-[https://github.com/ahannan08/ReactPro.git](https://github.com/ahannan08/ReactPro.git)
        
    * **Navigate to Project Directory:**
        
        * Change to the directory where the project files are located.
            
        * Since my project is located in root directory
            
        * ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1724003782712/9e369f44-e037-4391-ac55-26a19cd71196.png align="center")
            
    * **5)Configuration:**
        
        * **A) Backend Configuration:**
            
        * **Server Code (**`server.js`): Handles DynamoDB interactions and defines registration/login endpoints.
            
            ```plaintext
            cd backend
            
            //install libraries for backend
            npm install express aws-sdk body-parser bcryptjs  
            
            //make changes to code 
            nano server.js 
            ( to make changes to vpc and table name)
            //if u named the dynamo table u created "Users" and
            //u launched ur ec2 in ap-south-1a ,
            //no need to make changes to server.js.
            //but if any one is different, u need to rectify server.js
            ```
            
            * ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1724003902073/dfaa0bcd-f17f-4d22-bca6-aa06b9101ed0.png align="center")
                
            * ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1724004265222/70c7b3fc-91a7-4dba-99dd-bfa599099305.png align="center")
                
                **Run Backend with PM2:**
                
                ```plaintext
                sudo apt install pm2
                pm2 start server.js
                
                //to check for logs ( Reason why  error encountered during 
                Login or Regis in Real time )
                pm2 logs
                ```
                
                **b) Frontend Configuration:**
                
    
    * **Code for Authentication:** (Update with your AWS public dns and port in Routes in Register.js and login.js)
        
    * **Product and Cart Functions**
        
    * **Checkout Functionality**
        
        ```plaintext
        //come out of backend dir
        cd ../
        cd src/pages
        nano Register.js
        //make changes in the Routes . paste ur awspublic dns
        nano Login.js
        //same
        ```
        
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1724004767080/227f96eb-3301-47e7-b931-b6c84dad1753.png align="center")
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1724004865722/7f33e22a-f1d7-4663-a0e5-2444ee6f858c.png align="center")
    
* //same for handleRegister
    

**7\. Configure Security Groups**

\-http from 80 , https from 443, ssh from 22 ( this will be created at the time of instance creation)

\-custom tcp for 3000 and 5000 from anywhere (frontend and backend respectively)

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1724004970902/be055c43-3da2-4020-a4ef-98b2ca2c6e31.png align="center")

**6\. Accessing the Application**

```plaintext
//Go back the main folder Reactpro to install npm
npm install
npm start
```

Use the public DNS of your EC2 instance to access your application on port 5000 (e.g., [`http://ec2-public-dns:5000`](http://ec2-public-dns:5000)).
