How to Deploy Application in AWS EC2 (What to Follow)
Amazon EC2 allows users to launch and manage virtual machines in the cloud, known as instances. Among other benefits, deploying your application on AWS EC2 can provide scalability, reliability, and flexibility.
In this article, we shall provide the details on how to deploy an application on AWS EC2 using Amazon CodeDeploy. It is a powerful and automated deployment service that simplifies releasing new application versions to your EC2 instances.
How Did I Deploy Application in AWS EC2 Using Amazon Codedeploy
Below is a step-by-step guide on how to use Amazon CodeDeploy to deploy your application from GitHub.
Step 1: Launch EC2 Instance
Open your ec2 dashboard and click Launch instance.
Choose your preferred AMI. For this example, we shall choose a Linux AMI.
Select an instance type and go to Next. On the Configure Instance Details page, enter the number of instances you want to launch. Specify the IAM role as EC2CodeDeploy.
In the Advanced Details section, choose to enter the User data as text and paste the following into the textbox.
#!/bin/bash
sudo yum -y update
sudo yum -y install ruby
sudo yum -y install wget
cd /home/ec2-user
wget https://aws-codedeploy-ap-south-1.s3.ap-south-1.amazonaws.com/latest/install
sudo chmod +x ./install
sudo ./install auto
sudo yum install -y python-pip
sudo pip install awscli
Add storage as needed and input the Key and Value on the Add Tags page. Next, go to the Configure Security Group step and click Add Rule. Select HTTP as the port type and enter 80 as the port range. Then, click on Review and Launch.
Check the details and select Launch. When the popup window appears, choose Proceed without a key pair and click Launch Instances.
Step 2: Create an Application In CodeDeploy
From the AWS console, click on Services from the navigation menu on the top, search for CodeDeploy, and open it once the result appears.
Select Applications from the side bar and click Create application. Name your application and choose a compute platform. Here, we selected EC2/On-premises. Now, click on Create application.
After successfully creating the application, a popup message should appear.
As you can see, it tells us to create a deployment group. So, select Create deployment group and fill in the details like name of deployment group, service role, and deployment type.
Choose the environment configuration according to your requirements. For this demo, we shall select Amazon EC2 Instances.
Since we won’t be needing the Load Balancer for this demo, we shall disable the Enable load balancing option. You can enable it if you want. Finally, click create deployment group.
Step 3: Create a Code Pipeline
From the left navigation pane, expand Pipeline, select Pipelines and click on the Create pipeline button.
Specify the Pipeline name and leave the rest of the settings as they are. Click Next to continue.
For the Source provider, we shall choose GitHub since we shall deploy the application from a GitHub repository. You can, however, choose Amazon S3 if you want.
Click on Connect to GitHub, enter the address to your repository, and select Master for the Branch option. Then, press Next and choose Skip build stage.
Choose AWS CodeDeploy as the Deploy provider, select the region, and fill in the Application name and Deployment group textboxes.
Review the information you just provided and click on Create Pipeline. And your application will start getting deployed. You can check the deployment status by selecting the pipeline you just created. It might take a few seconds to update.
Additional Questions
How do I keep apps running in EC2?
To keep apps running in EC2, create an ECS cluster and publish your service. Also, create a docker container so that ECS can automatically adjust the number of running instances.
How do I host a node app on EC2?
After you provision an EC2 instance, install Node.js and your application dependencies. Then, deploy and run your Node.js app on the instance.
Conclusion
The demo deployment demonstrated in this article should give you a clear idea about launching an AWS EC2 instance, connecting to it, and deploying your application. Make sure to monitor your instance and keep it up to date with recent security patches for your application to run smoothly on EC2.