![]() |
EC2 stands for Elastic Cloud Computing and is a service that AWS offers for running applications, hosting websites, processing data, and performing other computing operations. How will we deploy our MERN application?We will push our MERN project frontend and backend into two different repositories on GitHub from our local machine. After that, using GitHub actions, we will build a Docker image for both repositories separately. These images will be pushed to DockerHub and saved in two different repositories on DockerHub. On AWS, we will configure a self-hosted runner for both the front end and back end. When our image is pushed to DockerHub, then our AWS self-hosted runner will pull the image from DockerHub. After this, our application will run on EC2. First, we will deploy the backend, and then, in a similar way, we will deploy the frontend. Hurray! Now you have hosted the MERN stack application on EC2. Deploy The Book Store Application On AWS EC2 Using Docker Through GitHub ActionsHere I have my “Book Store,” an application developed using the MERN stack. To deploy any MERN stack application, the process is similar. Step 1: Create A Public Repository On DockerHubLogin to DockerHub and create two public repositories, one for the frontend and the other for the backend. Also, note the username and repository name that we will need when we write the CICD workflow for our frontend and backend applications. Remember, we have to store our DockerHub username and password in GitHub secrets when we create a repository for our project on GitHub. Step 2: Allowing Access To The MongoDB DatabaseSo our first step is to allow access to MongoDB from anywhere on the internet. Sometimes MongoDB allocates our IP to access the database; because of that, we can only access it from our network. This happens because we tested our application locally many times. To access it from anywhere, log in to your MongoDB account and go to the network access of your database. This option is present in the left-site navigation bar when you open your database under the Security section. Now you will see an IP access list for your database. If you see IP “0.0.0.0/0,” then it is accessible from anywhere. If you see any other IP address, delete that and enter the given IP address. Step 3: Push An Application On GitHub And Add GitHub SecretsThere will be two folders for your MERN application. First for the backend, and second for the frontend. Now create two different repositories for frontend and backend, and push frontend folder files into the frontend GitHub repository and backend folder files into the backend GitHub repository of your project. Adding GitHub Secrets: Hardcoding our username, password, and API inside code is not good practice. To secure our credentials in the production environment, GitHub provides a feature to add secrets. These secrets can be our “env” variables that we add at the time of development, connection keys or our credentials.
Step 4: Creating An EC2 InstanceLog in to your AWS account as an IAM user. If you don’t have an IAM user, then create one and login with that user. Search for EC2 in the AWS console and click on the EC2 option appearing in the search result. ![]() Click on EC2.
Step 5: Installing Docker On Our EC2 InstanceIn previous steps, we successfully created our EC2 instance. Let’s connect them. Go to instances, click on our created instance, and click on “Connect.”
Use the below commands to install Docker on AWS EC2: sudo apt-get update After running this command, we will get permission denied as output. To give permission, we have to run the following command: sudo chmod 666 /var/run/docker.sock
![]() Successfully installed Docker Docker Installation EC2 Terminal LogsStep 6: Creating Self-Hosted Runners On Our EC2 InstanceWe have to create a runner on EC2, and that should be run simultaneously. To do this, we have to Configuring the self-hosted runner application as a service. To do this, we have to go to our created repositories in Step 3. So first, we will create a runner for our repository, “BookStore-Backend”. The process to create runners for both runners is the same. Creating a “BookStore-Backend” repository runner: Open our “BookStore-Backend” repository and click on settings. On the left navigation bar, you will see the option “Actions”. Click on that dropdown, and you will see the “Runners” option. Click on that.
What Happens After Running These Commands?Commands under the download section:
Commands under the configure section:
When we run our configuration command, the output terminal looks like this, and it asks for some input. We select some information by default and just add the name of the runner, “aws-ec2”. To run this runner, we have to run the “./run.sh” command every time. Configure A Self-Hosted Runner As A Service: To run this runner without running a command every time, we have to configure Runner as a service. To do this in the terminal, run the following commands: sudo ./svc.sh install After running these commands, our runner will go into an “idle” state. It will run continuously. Creating a “BookStore-Frontend” repository runner:
Note: Our terminal is in our backend folder, so we have to come into the root directory. To do this, run the “cd . . ” command.
When we run our configuration command, the output terminal looks like this, and it asks for some input. We select some information by default and just add the name of the runner, “aws-ec2.”. To run this runner, we have to run the “./run.sh” command every time. Configure a self-hosted runner as a service: To configure Runner as a service in the terminal, run the following commands: sudo ./svc.sh install After running these commands, our runner will go into an “idle” state. It will run continuously. We successfully created a self-hosted runner for both repositories in our EC2 instance. Step 7: Creating A Dockerfile And Workflow For The Backend RepositoryCreating A Dockerfile: To write a Dockerfile for our backend, go to your development IDLE and create a file with the name “Dockerfile” in the root directory of the backend folder. Inside package.json, see how we are starting our backend server if you are configured like this (“start”: “node index.js”), then paste this Dockerfile and change the port number as mentioned in your backend code.
{
FROM node:alpine3.18 Creating A Workflow For Our Backend Application: Now create a “.github” folder under the “backend” folder. Inside the “.github” folder, create the “workflow” folder, and inside this folder, create the file “cicd.yml”. Now we will write a workflow that will trigger GitHub actions when we push these updated files into our GitHub repository. I will keep my application details inside this. I will underline what you have to change inside this “cicd.yml”. At the time of changes, you have to remove details with an underline, update the file with your details, and also remove the underline from a particular section. In the curly bracket, I will mention what details you have to add. name: Deploy BookStore-Backend (GitHub Repository Name) Push These Changes To The Backend Repository
Our GitHub action will look like this: Step 8: Testing Our BackendAfter successfully building and deploying our backend, we have to test it. To do this, we will require our EC2 instance public address. Copy your EC2 instance public address, paste it in the browser, and give your backend deployment port number.
Adding Secrets To The Frontend Repository: Our frontend application needs a backend to run, and we have deployed our backend successfully, so we will add our backend server URL to GitHub secrets. Go to the GitHub secret page and give a name according to your frontend base URL configuration variable name. And in value, paste your backend deployment URL in the format “http://<Your-EC2-Public-IP>:<Allocated-Port-Number>” and save this secret.
Step 9: Creating A Dockerfile And Workflow For The Frontend RepositoryCreating a Dockerfile: To write a Dockerfile for our frontend, go to your development IDLE and create a file with the name “Dockerfile” in the root directory of the frontend folder. Dockerfile: We will select an alpine image for our frontend and build our app. And after building, serve that with Niginx. Copy the following Dockerfile and paste it in your Dockerfile.
FROM node:alpine3.18 as build Creating a Workflow for our frontend: Now create a “.github” folder under the “frontend” folder. Inside the “.github” folder, create the “workflow” folder, and inside this folder, create the file “cicd.yml”. Now we will write a workflow that will trigger GitHub actions when we push these updated files into our GitHub repository. I will keep my application details inside this. I will underline what you have to change inside this “cicd.yml”. At the time of changes, you have to remove details with an underline, update the file with your details, and also remove the underline from a particular section. In the curly bracket, I will mention what details you have to add. Note: If you have not allocated a port for the frontend, then it will by default run on port 5173, so we will write this in the last command as “5173:80.” name: Deploy BookStore-Frontend (GitHub Repository Name) Push These Changes To The Frontend Repository
Our GitHub action will look like this: Step 10: Testing Our FrontendAfter successfully building and deploying our frontend, we have to test it. To do this, we will require our EC2 instance public address. Copy your EC2 instance public address, paste it in the browser, and give your frontend deployment port number.
Note: Remember to add the inbound rule to the EC2 instance and the correct port number that you have configured. Demo For Testing our Book Store ApplicationWe successfully deployed our backend application and frontend application on an EC2 instance, and it is working the same as in the local system. A Recap Of the Steps That We Have Taken To Deploy The MERN Application ConclusionUsing GitHub actions, we first deployed our backend application on our EC2 instance. By using our backend application’s public IP address, we configured our frontend BaseURL inside our frontend repository secrets. Then we deployed our front-end application on our EC2 instance. By performing all these steps, you can successfully deploy your MERN Stack application in AWS EC2 using Docker through GitHub actions How To Deploy MERN Applications On AWS EC2 – FAQ’sHow Do I Allow Access To MongoDB From Anywhere?
How Do I Add GitHub Secrets?
How Do I Create A Dockerfile And Workflow For My Repositories?
How Do You Test Your Deployed Applications?
Tell The Steps Involved In Deploying MERN Application On AWS EC2 Instance Using GitHub Actions?
|
Reffered: https://www.geeksforgeeks.org
Amazon Web Services |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 14 |