DEV Community

Cover image for My First AWS EC2 Hosting Experience: A Step-by-Step Guide -- Nodejs
Ikenna Iheanaetu
Ikenna Iheanaetu

Posted on

1

My First AWS EC2 Hosting Experience: A Step-by-Step Guide -- Nodejs

Hey there, fellow developers! I wanted to share my recent experience setting up my first application on AWS EC2. I've been working on becoming an AWS Community Builder, and this project was an important step in that journey. If you're considering using EC2 for hosting, hopefully my experience will help make your process smoother.

What is EC2 and Why I Chose It

Amazon Elastic Compute Cloud (EC2) is basically AWS's virtual server offering. I chose EC2 because I wanted full control over my environment while still leveraging the reliability of AWS infrastructure. Plus, the free tier option was perfect for my initial testing phase.

Let's Get Started

Prerequisites

  • An AWS account (obviously!)
  • Basic command line knowledge
  • A simple application to deploy (I used a Node.js app)
  • About 30-45 minutes of your time

Step 1: Launching Your EC2 Instance

  1. Log into the AWS Management Console and navigate to EC2
  2. Click on "Launch Instance"
  3. Pick your Amazon Machine Image (AMI) - I went with Amazon Ubuntu Server 24.04 LTS because it's optimized for EC2 and has good support
  4. Choose an instance type - t2.micro is free tier eligible and works well for small apps
  5. Configure your instance details - I kept most settings at default
  6. Add storage - the default 8GB was enough for my app
  7. Add tags - I just tagged it with Name: MyFirstEC2
  8. Configure security group - This is crucial! I created a new security group and:
  9. Allowed SSH (port 22) from my IP address only
  10. Allowed HTTP (port 80) from anywhere
  11. Allowed HTTPS (port 443) from anywhere
  12. Review and launch - This is when AWS prompts you to create a key pair. Download it and keep it safe.

Step 2: Connecting to Your Instance

Wait for your instance to initialize (it'll show "running" status). Then:

# Connect via SSH
ssh -i your-key-file.pem ubuntu@your-instance-public-dns
Enter fullscreen mode Exit fullscreen mode

For the public dns use your public IPV4 address

Step 3: Setting Up Your Server

Once connected, I ran:

# Update and Upgrade linux machine
sudo apt update && sudo apt upgrade

# Install node version manager (nvm) by typing the following at the command line.
sudo su -
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.34.0/install.sh | bash

# Activate nvm by typing the following at the command line.
. ~/.nvm/nvm.sh

# Use nvm to install the latest version of Node.js by typing the following at the command line.
nvm install node

# Test that node and npm are installed and running correctly by typing the following at the terminal:
node -v
npm -v

# To install git, run below commands in the terminal window:
sudo apt-get update -y
sudo apt-get install git -y

# Just to verify if system has git installed or not
git — version
Enter fullscreen mode Exit fullscreen mode

Step 4: Deploying Your Application
I cloned my repo, installed dependencies, and started my app:

git clone https://github.com/your-username/your-repo.git
cd your-repo
npm install
npm run dev
Enter fullscreen mode Exit fullscreen mode

After installing your project dependencies, enter your start script, for me it is npm run dev.

You should also know that once you close your ssh session the app would stop running, for my own project I am using pm2 as my process manager which is setup in the project. If you don't have it setup use the instructions below

# Install PM2
sudo npm install -g pm2

# Start your app with PM2
pm2 start app.js

# Make PM2 restart on server reboot
pm2 startup
Enter fullscreen mode Exit fullscreen mode

Challenges I Faced
For the most part I didn't face any challenges, spinning up an ec2 instance for the first time was relatively easy.

Best Practices I Learned

  1. Security is paramount: Only open the ports you need, restrict SSH access to your IP
  2. Automate everything: Learn basic bash scripting to automate your setup
  3. Monitor your resources: AWS has great monitoring tools - use them!

Next Steps
Next I'll be sharing how to add environment variables to your project and domain configuration.
If you're just starting with AWS, I highly recommend their documentation - it's surprisingly readable and has great examples.
That's it for now! Feel free to comment with any questions or share your own EC2 experiences. I'm still learning and would love to hear your tips too!

Image of Datadog

The Future of AI, LLMs, and Observability on Google Cloud

Datadog sat down with Google’s Director of AI to discuss the current and future states of AI, ML, and LLMs on Google Cloud. Discover 7 key insights for technical leaders, covering everything from upskilling teams to observability best practices

Learn More

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay