Deploying an application on a VM or EC2 instance in AWS is like setting up a brand-new computer for a friend. Just like a fresh computer, the VM or EC2 instance starts with no application software installed. To prepare it for your specific application, you must install and configure all necessary software and dependencies. The difference is that you're performing this setup remotely over the internet, leveraging the power and flexibility of cloud computing, and then accessing the application which is installed remotely.
Just keep in mind the above thing and read this blog accordingly, you will not be overwhelmed.
Interacting with EC2 instance or server == Interacting with the VM remotely over the internet
As we know that:-
Server is a computer or system that provides resources, data, services, or programs to other computers, known as clients,
Interaction with AWS services
AWS provides a powerful command-line interface (CLI) to interact with its services, including creating, deleting, and modifying resources.
Download AWS CLI: You can download the AWS CLI from:
https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html.
Install the AWS CLI: Run the
.msi
file to install the AWS CLI on your system.Configure AWS CLI: To configure the CLI, use the command:
aws configure
This command will prompt you for your AWS access key ID, secret access key, region, and output format. You can find these credentials in the AWS Management Console:
Click on your username in the top right corner.
Select Security credentials.
Generate and copy your
access key ID
andsecret access key
.
Now you are good to go with accessing the services.
Creating an EC2 Instance
To create an EC2 instance through the AWS console:
Navigate to AWS Console > EC2 > Instances (running) > Launch Instances.
Name your instance, select the OS, and create a Key Pair.
Download the PEM file (e.g.,
abc22.pem
).Click on Launch Instance.
Accessing EC2 Instances
To access your EC2 instances effectively, especially from a Windows environment, you can use tools like MobaXterm. First, ensure you set the correct permissions for your PEM file using the following command:
chmod 400 /path/to/your-file.pem
Setting the permission to 400 ensures only the owner can read the file, securing your private SSH key. Unauthorized access to your PEM file could lead to potential data breaches or unauthorized use of your EC2 instances.
By automating VM creation and access, DevOps engineers can streamline workflows, improve efficiency, and enhance security across their infrastructure.
Copy the Public IPv4 Address: After creating the EC2 instance, copy its public IPv4 address.
Access the VM via SSH: Use the following command to access your VM:
ssh -i "<path/to/your-key.pem>" ubuntu@<Public IP of the VM>
Note: The username is defined by the AMI used to launch the instance. If you didn't define a custom username, use the default username,
ubuntu
.Updating Packages: Once logged in, it's always a good practice to update all packages using:
sudo apt update
By following these steps, you can efficiently manage and access your EC2 instances, ensuring your infrastructure is secure and up-to-date.
Now you can interact with the ec2 instance or VM, through the command prompt.
Setting up the VM
Installing Git on VM
As we have to interact with git in order to get the code inside the VM.
Note:- In some VM the git is pre-installed so you don't have to do it explicitly.
Now if git is not installed on the VM you can set up the git in your VM by following command:-
sudo apt install git
Then clone the repository of the code
git clone <github-repositiory-url>
Installing Node.js on VM
I want to run the react application and node.js application on the VM. So I'll be installing the Node.js. It's up to you which kind of application you want to deploy, you can set your VM environment accordingly.
To do that you can follow the below command.
sudo apt install nodejs
Installing the npm on VM
In order to install the packages of th npm we have to install the npm in the VM by the following command:-
sudo apt install npm
Setting up the application
Go inside the application by the command cd
.
If your application requires the .env
file, then create in the directory.
Then install all the dependencies by the following command.
npm i
Then start the application.
npm start # for the react app
The application is running on the specified PORT inside the Remote VM successfully.
Accessing the application running on the VM
We can access the application through the public IP address of the VM with the port number.
<public IP address>:3000
But we'll not be able to access it because the necessary security group rules might not be configured. To resolve this, we need to:
Ensure that the security group associated with the EC2 instance allows inbound traffic on port 3000. Here’s how to add an inbound rule:
Go to the EC2 Dashboard in the AWS Management Console.
Select 'Instances' and find the instance you want to configure.
Under the 'Security' tab, click on the security group linked to the instance.
-
In the security group details, go to the 'Inbound rules' tab and click 'Edit inbound rules'.
Add a new rule with the following details:
Type: Custom TCP
Protocol: TCP
Port Range: 3000
Source: Custom (or specify the IP range from which you want to allow access, e.g., 0.0.0.0/0 for all IPs)
Save the changes.
- Check that the application is correctly configured to listen on port 3000 and accept connections from external sources.
By updating the security group rules and verifying the application's configuration, we can successfully access the application from the public IP address."Step 1:
Now you access the application from anywhere through the following from the browser.
<public IP address>:3000
You can see in your browser that the application is running on port 3000, as specified. And we can access this application through any device from the above <public IP address>:3000
.
As we know, the Domain Name System (DNS) is responsible for mapping human-readable domain names to their corresponding IP addresses. This process allows users to access websites using easy-to-remember domain names instead of having to remember complex numerical IP addresses.
So, if you want to provide a domain name for the application, you can purchase a domain and configure a DNS record to point to the IP address of this VM. This way, you will be able to access the application via domain-name.xy
instead of using the IP address directly.
Congratulations, now you have successfully deployed your own project on the AWS ec2 instance.
If you like this blog, please do like it.
Check out my Portfolio website for connecting with me or just to say Hi !!.
Happy deploying!