From Code to Cloud: Deploying Your Application on AWS EC2

Amazon Web Services (AWS)

Ritika Singh
5 min readMay 1, 2023

Amazon Web Services (AWS) provides a wide range of cloud computing services to help businesses and individuals scale and manage their applications. One of the most popular services offered by AWS is Elastic Compute Cloud (EC2), which provides resizable compute capacity in the cloud. With EC2, you can quickly and easily launch virtual servers, also known as instances, and run your applications in the cloud.

In this blog, we will discuss what EC2 is and how to get started with deploying your application on an EC2 instance.

What is AWS EC2?

Amazon EC2 is a web service that provides resizable compute capacity in the cloud. It is designed to make web-scale cloud computing easier for developers. With EC2, you can quickly provision and launch virtual servers, also known as instances, and easily scale your capacity up or down based on demand.

EC2 offers a wide range of instance types, each optimized for specific workloads. For example, if you need a high-performance computing environment, you can choose an instance type with specialized hardware such as Graphics Processing Units (GPUs). If you need a balance of compute, memory, and network resources, you can choose a general-purpose instance type.

Choosing the right type of EC2 instance for your application-

Types of EC2 instance

Choosing the right type of EC2 instance is crucial to ensure that your application runs efficiently and effectively. Here are some factors to consider when choosing an EC2 instance type:

  1. Workload Type

The workload type will determine the CPU, memory, storage, and network requirements of your application. EC2 offers a wide range of instance types optimized for different types of workloads. For example, if you are running a memory-intensive application, you should choose an instance type with more RAM. If you are running a compute-intensive application, you should choose an instance type with more CPU cores.

2. Performance Requirements

The performance requirements of your application will determine the instance type that you should choose. EC2 offers instance types with specialized hardware such as Graphics Processing Units (GPUs) for high-performance computing applications. You should choose an instance type that can handle the workload demands of your application without compromising on performance.

3. Budget

The cost of an instance type will depend on its CPU, memory, and storage capacity. EC2 offers a variety of pricing options, including On-Demand Instances, Reserved Instances, and Spot Instances. You should choose an instance type that fits within your budget and meets your performance and workload requirements.

4. Availability Requirements

The availability requirements of your application will determine the instance type that you should choose. EC2 offers instance types that are designed for high availability, such as instances that are part of an Auto Scaling group. You should choose an instance type that meets the availability requirements of your application.

5. Operating System

The operating system that your application runs on will determine the instance type that you should choose. EC2 offers a wide range of instance types that are optimized for different operating systems, including Linux, Windows, and macOS. You should choose an instance type that is compatible with your operating system.

In conclusion, choosing the right type of EC2 instance is crucial to ensure that your application runs efficiently and effectively. By considering the workload type, performance requirements, budget, availability requirements, and operating system, you can choose an instance type that meets the needs of your application.

Deploying Code on EC2 Instance

Once you have an EC2 instance up and running, you can deploy your code onto it. There are several ways to do this, including:

  1. SSH: Secure Shell (SSH) is a network protocol that allows secure communication between two computers. You can use an SSH client to remotely connect to your EC2 instance and transfer files to it using the command-line interface (CLI).
  2. FTP: File Transfer Protocol (FTP) is a standard network protocol used to transfer files from one host to another over the Internet. You can use an FTP client to upload files to your EC2 instance.
  3. AWS CodeDeploy: AWS CodeDeploy is a fully managed deployment service that automates code deployments to any instance, including EC2 instances. It provides a centralized location for your application code, deploys it to your EC2 instances, and tracks the deployment status.

In this blog, we will use the SSH method to deploy code onto our EC2 instance.

Create an EC2 Instance

The first step is to create an EC2 instance. Follow the steps below:

  1. Sign in to the AWS Management Console.
  2. Navigate to the EC2 service.
  3. Click the Launch Instance button.
  4. Choose an Amazon Machine Image (AMI) for your instance. You can choose from various Linux distributions, including Amazon Linux, Ubuntu, and Red Hat Enterprise Linux.
  5. Choose an instance type based on your requirements. You can choose from various types, including General Purpose, Compute Optimized, Memory Optimized, and Storage Optimized.
  6. Configure your instance by specifying the network, subnet, and security group.
  7. Create a key pair to enable SSH access to your instance.
  8. Launch your instance.

Connect to Your Instance Using SSH

Once you have launched your instance, you can connect to it using SSH. Follow the steps below:

  1. Open your terminal.
  2. Navigate to the directory where your key pair is stored.
  3. Use the chmod command to set the permissions on your private key file. For example, chmod 400 my-key-pair.pem.
  4. Use the ssh command to connect to your instance. For example,
ssh -i my-key-pair.pem ec2-user@my-instance-public-ip

Install Required Software

Install the required software to run your application. In this example, we’re installing Node.js and npm.

npm install -y nodejs

Clone Your Code Repository

Clone your code repository onto the instance using Git.

git clone https://github.com/myusername/myproject.git

Install Dependencies

Install any dependencies required by your application using npm.

cd myproject
npm install

Tip- Run your application using pm2 , to keep it running at all times.

Deploying your frontend code to the EC2 instance-

  1. Install web server software

Install a web server software such as Apache or Nginx on the EC2 instance. You can use the package manager of your operating system to install the web server.

2. Configure the web server

After installing the web server, you need to configure it to serve your front-end code. Create a virtual host for your application and specify the root directory where your front-end code is located.

3. Copy your front-end code to the EC2 instance — Copy your front-end code to the root directory that you specified in the previous step. You can use a tool such as SCP or SFTP to copy the files to the EC2 instance.

4. Start the web server

Start the web server software on the EC2 instance. You can use the systemctl command to start the web server and enable it to start automatically on system boot.

5. Access your application

Finally, access your application using the public IP address or DNS name of the EC2 instance in a web browser. You should be able to see your front-end code running on the EC2 instance.

And , tada anyone can access and use your application now!

--

--