Deploy an Employee Directory App on EC2
Deploy a Node.js and MySQL web app on an Ubuntu EC2 instance.
Introduction
30 Second Summary
Every company reaches a point where their internal tools outgrow the server sitting in the office. Moving those applications to the cloud is one of the most common tasks a Cloud Engineer tackles.
In this project, you will deploy an Employee Directory web application on an EC2 instance. You will first run the app on your Windows machine to create a local baseline, then deploy the same app on a cloud server running Ubuntu.
What You'll Build
A Node.js Employee Directory application backed by MySQL, deployed on an EC2 instance and accessible from any browser.
By the end of this project, you'll have:
- A local version of the Employee Directory running on your Windows machine as the "before migration" baseline.
- A live EC2 instance running Ubuntu Server with the same application accessible via public IP.
- A MySQL database on EC2 populated with test employee records, ready for migration to Amazon RDS in the next project.
- Secret Mission: Set up PM2 to keep the Node.js app running after you disconnect from SSH, with automatic restart on crash and server reboot.
Are there any prerequisites?
This project is designed for Windows users with an AWS account and basic comfort using a terminal. It is Project 1 of 4 in the CloudHR Migration Series.
Set Up Your Local Tools
Before you can run the Employee Directory application, your Windows machine needs three tools installed.
Node.js runs the application code. MySQL stores the employee data. Git downloads the application source code from GitHub. In this step, you will install and verify all three.
In this step, get ready to:
- Install Node.js 22.x LTS on your Windows machine.
- Install MySQL 8.0 and set a root password.
- Install Git and verify all three tools are working.
Install Node.js
Node.js is the runtime that executes your Employee Directory application. This project uses Node.js 22.x LTS (Long Term Support), which is the stable, actively maintained release line.
- Open https://nodejs.org/en/download in your browser.
Don't see Node.js 22?
The download page defaults to the latest LTS version (currently v24). This project requires v22 LTS (Jod).
Look for a version selector or dropdown on the page and choose v22. If you cannot find one, go directly to the Node.js 22 download archive and download the Windows .msi installer for your architecture (x64 for most machines).
- Download the Node.js 22.x LTS installer for Windows (64-bit .msi).
- Run the downloaded installer.
- Accept the default settings on each screen and click Next until the installation completes.
- Open a new terminal window. Press the Windows key, type Terminal or Command Prompt, and press Enter.
- Verify Node.js is installed by running this command:
node -v
- Verify npm (Node's package manager) is installed by running:
npm -v
Both commands should print version numbers.
✔️ I see version 22.x or higher
Node.js is ready to go. Move on to the next substep.
ⓧ I see an older version number
You have an older version of Node.js installed. The Node.js 22.x MSI installer for Windows replaces any previous version automatically.
- Download and run the 22.x LTS .msi installer from the Node.js 22 download archive.
- Close your terminal and open a new one so it picks up the updated PATH.
- Run node -v again. You should now see a v22 version number.
ⓧ Command not found
The installer may not have added Node.js to your system PATH, or you are using a terminal that was open before the installation.
- Close your current terminal window completely and open a new one.
- Run node -v again.
- If it still fails, re-run the MSI installer. Make sure the option to add Node.js to PATH is checked during setup.
What is Node.js?
Node.js lets you run JavaScript outside a web browser. The Employee Directory application is built with Express, a Node.js framework for building web servers.
npm (Node Package Manager) comes bundled with Node.js. You will use it to install the application's dependencies later.
Install MySQL
MySQL is the database that stores employee records. The Employee Directory app reads from and writes to a MySQL database, so you need it running locally before you can start the application.
- Open https://dev.mysql.com/downloads/installer/ in your browser.
- Download the MySQL Installer 8.0.46 for Windows. Choose the larger offline installer (mysql-installer-community-8.0.46.0.msi, ~566 MB) for a smoother experience.
- Run the downloaded installer.
What to expect during MySQL setup
The MySQL Installer is a wizard that walks you through configuration. Keep these details in mind during setup:
- MySQL username: root (this is the default admin user).
- Set a root password that you will remember. You will need it throughout this project.
- Port: 3306 (the default MySQL port).
Accept the defaults for everything else and complete the installation.
- After the installation completes, verify MySQL is running by opening your terminal and running:
mysql -u root -p
- Enter the root password you set during installation when prompted.
You should see the mysql> prompt, which means MySQL is installed and running.
- Type EXIT; and press Enter to leave the MySQL prompt.
✔️ I see the mysql> prompt
MySQL is installed and running. Move on to the next substep.
ⓧ I see 'mysql' is not recognized
MySQL was installed but the mysql command is not in your system PATH.
- Close your terminal and open a new one.
- Try running mysql -u root -p again.
- If it still fails, you need to add MySQL to your PATH manually. Press the Windows key, type Environment Variables, and open Edit the system environment variables.
- Click Environment Variables, then under System variables, find Path and click Edit.
- Click New and add C:\Program Files\MySQL\MySQL Server 8.0\bin.
- Click OK on all dialogs, then close and reopen your terminal.
ⓧ Access denied or another error
If you see an access denied error, double-check the root password you entered. It must match the password you set during the MySQL Installer setup.
If MySQL is not running as a service, open the Windows key, type Services, and press Enter. Find MySQL80 in the list, right-click it, and select Start.
Install Git
Git is the version control tool you will use to download the Employee Directory source code from GitHub. You may already have it installed.
- Check if Git is already installed by running this command in your terminal:
git --version
✔️ I see a version number
Git is already installed. You are all set for this step.
ⓧ Command not found
- Open https://git-scm.com/downloads/win in your browser.
- Download the Git installer for Windows.
- Run the installer with default settings. Click Next through each screen and then Install.
- Close your terminal and open a new one.
- Run git --version again. You should now see a version number.
All three tools are installed and ready. Next up, you will create the MySQL database and run the Employee Directory application on your local machine.
Run the Application Locally
Your development tools are installed and verified. Now it's time to put them to work by setting up the application you'll eventually migrate to the cloud.
In this step, you'll create a MySQL database, clone the Employee Directory app from GitHub, and run it on your local machine. This local version is your "before migration" baseline.
In this step, get ready to:
- Create the MySQL database, application user, and employees table.
- Clone and configure the Employee Directory application.
- Start the application and verify it works in your browser.
Create the database, user, and table
The Employee Directory app needs a MySQL database to store employee records. Before running the app, you need to create the database, a dedicated user account for the app to connect with, and the table that holds employee data.
- Log into MySQL in your terminal by running:
mysql -u root -p
- Enter the root password you set during MySQL installation.
- You should see the mysql> prompt, which means you're inside the MySQL shell.
Not seeing the mysql> prompt?
If you see an error about MySQL not being recognized, make sure MySQL is added to your system's PATH. Try closing and reopening your terminal.
If you see an access denied error, double-check the root password you set during installation.
Help me troubleshoot my MySQL login on Windows.
- Create the database, user, and table by running these SQL commands in the mysql> prompt:
-- Create a database for the Employee Directory app
CREATE DATABASE employee_db;
-- Create a dedicated user for the app to connect with
CREATE USER 'employee_user'@'%' IDENTIFIED BY 'StrongPassword123';
-- Give the user full access to the employee_db database
GRANT ALL PRIVILEGES ON employee_db.* TO 'employee_user'@'%';
FLUSH PRIVILEGES;
-- Switch to the new database
USE employee_db;
-- Create the table that stores employee records
CREATE TABLE employees (
id INT AUTO_INCREMENT PRIMARY KEY,
full_name VARCHAR(100) NOT NULL,
email VARCHAR(100) NOT NULL,
role VARCHAR(100),
department VARCHAR(100),
location VARCHAR(100),
join_date DATE NULL,
photo_url VARCHAR(255) DEFAULT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
What does this SQL do?
- CREATE DATABASE employee_db creates a new database to hold all the app's data.
- CREATE USER creates a dedicated user (employee_user) so the app doesn't connect with the powerful root account.
- GRANT ALL PRIVILEGES gives that user full access to the employee_db database only.
- CREATE TABLE employees defines the columns each employee record will have, including name, email, role, department, and location.
- Verify the table was created by running:
SHOW TABLES;
- You should see employees listed in the output.
- Type EXIT; and press Enter to leave the MySQL prompt.
Seeing a syntax error?
Make sure each SQL statement ends with a semicolon. Missing the semicolon is the most common cause of SQL errors.
If you see an error about a database or user already existing, you may have run part of the script before. You can drop them and start fresh with DROP DATABASE employee_db; and DROP USER 'employee_user'@'%'; before running the script again.
Help me fix my MySQL database setup.
Clone and configure the application
The Employee Directory app code lives in a GitHub repository. You'll clone it to your machine, install its dependencies, and configure it to connect to the database you just created.
- Move to your Desktop by running this command:
cd %USERPROFILE%\Desktop
- Clone the repository and install the project dependencies by running these commands:
git clone https://github.com/techwithlucy/ztc-projects.git
cd ztc-projects\projects\cloud-engineer-projects\project-1
npm install
What did these commands do?
- git clone downloads a copy of the entire project repository to your Desktop.
- cd navigates into the specific project folder (project-1) where the Employee Directory app lives.
- npm install reads the package.json file and downloads all the libraries the app needs to run (like Express and the MySQL driver).
- You should see packages being installed with no error messages. A node_modules folder is created inside the project-1 directory.
Having trouble cloning or installing?
If git clone fails, check your internet connection. Make sure git is installed by running git --version.
If npm install fails, verify Node.js is installed by running node -v. If it's not recognized, close and reopen your terminal.
Help me fix my git clone or npm install error.
The app needs to know how to connect to your MySQL database. You'll create a configuration file that stores these connection details.
- Create the .env configuration file by running:
notepad .env
- If Notepad asks whether to create a new file, click Yes.
- Paste the following content into the file:
# Database connection settings for the Employee Directory app
DB_HOST=localhost
DB_USER=employee_user
DB_PASSWORD=StrongPassword123
DB_NAME=employee_db
PORT=3000
What's in this file?
The .env file stores environment variables that the app reads at startup. This keeps configuration separate from code.
- DB_HOST, DB_USER, DB_PASSWORD, and DB_NAME tell the app how to connect to your MySQL database.
- PORT=3000 sets which port the web server listens on.
- Save the file by pressing Ctrl+S, then close Notepad.
- Confirm the file was saved correctly by running:
type .env
- You should see all five configuration values printed in your terminal.
✔️ Awesome, I've got everything!
Great. Make sure the file is saved before moving on.
ⓧ I'd like to double check the full code
Here is the complete .env file for comparison:
DB_HOST=localhost
DB_USER=employee_user
DB_PASSWORD=StrongPassword123
DB_NAME=employee_db
PORT=3000
Seeing an empty file or extra .txt extension?
If Notepad saved the file as .env.txt instead of .env, rename it by running:
ren .env.txt .env
If type .env shows nothing, the file may be empty. Run notepad .env again to reopen it and paste the content.
Help me fix my .env file on Windows.
Start and test the application
Everything is in place. The database is ready, the code is installed, and the configuration points the app at your local MySQL. Time to start the server and see the Employee Directory in your browser.
- Make sure your terminal is still in the project-1 directory.
- Start the application server by running:
npm start
What should I see?
You should see two lines in your terminal:
Server running on http://localhost:3000
Connected to MySQL successfully
This confirms the app started and connected to your employee_db database. Keep this terminal open. The server runs as long as the terminal is open.
- Open your web browser and go to http://localhost:3000.
You should see the Employee Directory interface with an "Add Employee" form on the left and an empty employees table on the right.
- Add 2-3 test employees using the form. Fill in the Full Name, Email, Role, Department, Location, and Join Date fields for each employee.
- After adding each employee, check that they appear in the employees table below the form.
- Check the Team Overview panel. It should show updated counts for Total Employees, Departments, and Locations based on the employees you added.
Why does this "local" version matter?
This local version simulates an "on-premises" application, the kind of app a company might run on servers in their own office. In the next steps, you'll deploy this exact same application on an AWS EC2 instance.
Having both versions running lets you compare the experience of managing an app locally versus in the cloud. This is the core of a real cloud migration.
App not loading in the browser?
If you see a connection error in the browser, make sure the terminal where you ran npm start is still open and shows the "Connected to MySQL successfully" message.
If the terminal shows a database connection error, double-check the values in your .env file. The DB_USER, DB_PASSWORD, and DB_NAME must match what you used in the SQL setup. Make sure MySQL is still running.
Help me get the Employee Directory app running on localhost.
Your local Employee Directory is live and loaded with test data. Next, you'll launch an EC2 instance in AWS and deploy the same application on a cloud server.
Launch and Configure an EC2 Instance
Your Employee Directory is running locally with test employees in the database. That's your "before migration" baseline.
Now you'll set up the cloud side. You'll launch an EC2 instance on AWS to act as your simulated on-premises server, then connect to it over SSH.
In this step, get ready to:
- Launch an EC2 instance running Ubuntu Server 24.04 LTS.
- Configure a security group for SSH and application access.
- Connect to the instance via SSH from your Windows machine.
Launch the instance
EC2 lets you spin up virtual servers in the AWS cloud. You'll create one running Ubuntu, which gives you a Linux environment to host the Employee Directory app.
- Sign in to the AWS Management Console at https://console.aws.amazon.com.
- In the search bar at the top, type EC2 and select EC2 from the results.
- Click Launch instance.
What is EC2?
EC2 (Elastic Compute Cloud) is a virtual server in the AWS cloud. Instead of buying physical hardware, you rent compute power on demand.
In this project, your EC2 instance simulates an on-premises server. In later projects in this series, you'll migrate the database off this server and into a managed AWS service.
The launch wizard opens with several configuration sections. Fill them in one at a time.
- Under Name and tags, enter onprem-employee-directory-server in the Name field.
- Under Application and OS Images (Amazon Machine Image), select Ubuntu from the Quick Start tabs.
- In the AMI dropdown, select Ubuntu Server 24.04 LTS. Make sure it says Free tier eligible.
- Under Instance type, select t2.micro. This is Free Tier eligible.
What is an AMI?
An AMI (Amazon Machine Image) is a template containing the operating system and pre-installed software for your virtual server. Choosing Ubuntu Server 24.04 LTS means your EC2 instance boots with Ubuntu already installed.
Next, create a key pair so you can securely connect to the instance later.
- Under Key pair (login), click Create new key pair.
- Enter on-prem as the key pair name.
- For Key pair type, keep RSA selected.
- For Private key file format, select .pem.
- Click Create key pair. The on-prem.pem file downloads automatically. Save it somewhere you can find it.
What is a key pair?
A key pair is a set of cryptographic keys used for secure access to your instance. AWS keeps the public key on the server. You keep the private key (the .pem file) on your local machine. Anyone with the private key can connect, so keep it safe.
- Under Network settings, confirm the defaults: Default VPC is selected and Auto-assign public IP is set to Enable.
Configure the security group
A security group acts as a virtual firewall for your instance. You need to open two ports: one for SSH access and one for the Employee Directory app.
- Still in the Network settings section of the launch wizard, click Edit.
- Under Firewall (security groups), select Create security group.
- The first inbound rule is pre-filled for SSH. Set its Source type to My IP. This restricts SSH access to your current IP address.
- Click Add security group rule to add a second rule.
- Set Type to Custom TCP.
- Set Port range to 3000.
- Set Source type to Anywhere (0.0.0.0/0). This lets anyone reach the app on port 3000.
Why two security group rules?
Port 22 (SSH) is for you to remotely manage the server from your terminal. Restricting it to "My IP" means only your machine can connect.
Port 3000 is where the Employee Directory app listens. Opening it to 0.0.0.0/0 lets you access the app from any browser.
- Leave all other settings at their defaults.
- Review the Summary panel on the right side of the page.
- Click Launch instance.
You should see a success message confirming your instance is launching.
- Click View all instances to go to the Instances page.
- Wait until the Instance state column shows Running and the Status check column shows checks passed. This can take 1-2 minutes.
- Select your onprem-employee-directory-server instance and find the Public IPv4 address in the details panel at the bottom. Copy this address. You'll need it to connect.
Instance not showing as Running?
It can take a couple of minutes for the instance to start. Click the refresh icon in the Instances table to update the status.
If the state goes to Terminated instead of Running, check that you selected t2.micro as the instance type and that your AWS region supports the selected AMI.
Help me troubleshoot my EC2 instance that isn't starting up. I selected Ubuntu Server 24.04 LTS and t2.micro.
Connect via SSH
Your instance is running in the cloud. Now you'll connect to it from your Windows terminal using SSH, which gives you a remote command line on the Ubuntu server.
- Open a terminal on your Windows machine. Press the Windows key, type Terminal, and press Enter.
- Navigate to the folder where your on-prem.pem file was downloaded by running this command:
cd Downloads
- Connect to your EC2 instance by running this command:
ssh -i on-prem.pem ubuntu@[[EC2_PUBLIC_IP="your EC2 public IPv4 address"]]
What does this command do?
- ssh opens a secure remote connection to another computer.
- -i on-prem.pem tells SSH to use the private key file you downloaded to authenticate.
- ubuntu is the default username for Ubuntu Server AMIs on EC2.
- The IP address after @ is your instance's public address.
- When prompted with "Are you sure you want to continue connecting?", type yes and press Enter. This happens the first time you connect to a new server.
You should see the Ubuntu welcome message in your terminal. Your command prompt now shows ubuntu@ followed by your instance's private hostname. You're now working inside your EC2 instance.
Connection timed out or permission denied?
- If you see a connection timeout, double-check that your security group allows SSH (port 22) from your IP. Your IP may have changed since you configured the rule. Go back to the EC2 console, select your instance's security group, and update the SSH rule source to your current IP.
- If you see a permission denied error, make sure you're using ubuntu as the username (not ec2-user or root) and that on-prem.pem matches the key pair you created during launch.
- If you see a warning about an unprotected private key file, right-click the .pem file in File Explorer, select Properties, go to the Security tab, click Advanced, disable inheritance, remove all users except yourself, and grant yourself Read permissions.
Help me troubleshoot my SSH connection to an Ubuntu EC2 instance from Windows.
You're now inside your EC2 instance. Next up, you'll install Node.js, MySQL, and Git on this server to recreate the same environment as your local machine.
Set Up the Server Environment on EC2
Your EC2 instance is up and running, and you're connected via SSH. Right now, this server is a clean Ubuntu machine with no development tools installed.
To run the Employee Directory app on EC2, you need the same tools you set up on your Windows machine. In this step, you'll install Node.js, MySQL, and Git on the server and create the database.
In this step, get ready to:
- Install Node.js and Git on the EC2 instance.
- Install and secure MySQL Server.
- Create the database, user, and employees table.
Install Node.js and Git
Before you can run the application, the EC2 instance needs the same runtime tools your local machine has. You'll start by updating the server's packages, then install Git and Node.js.
- In your SSH session, update the system packages by running this command:
sudo apt update && sudo apt upgrade -y
What does this command do?
`sudo apt update` refreshes the list of available packages from Ubuntu's repositories. `sudo apt upgrade -y` installs all pending updates, with `-y` automatically confirming the installation.
On Ubuntu 24.04, the system may automatically restart some services after the upgrade completes. This is normal and expected.
You should see a stream of output as packages are downloaded and installed. This may take a couple of minutes.
Seeing connection errors?
Make sure your EC2 instance has internet access. Instances in the default VPC with a public IP should have outbound access by default. If you see errors mentioning failed downloads, wait a moment and try the command again.
Help me troubleshoot apt update errors on my EC2 instance.
- Install Git by running:
sudo apt install -y git
- Install Node.js 22.x via the NodeSource repository by running these commands:
curl -fsSL https://deb.nodesource.com/setup_22.x -o nodesource_setup.sh
sudo -E bash nodesource_setup.sh
sudo apt install -y nodejs
What do these commands do?
The first command downloads the NodeSource setup script, which configures your system to pull Node.js packages from NodeSource's repository instead of Ubuntu's default (which may have an older version).
The second command runs that script to add the repository. The third command installs Node.js 22.x LTS from the newly added repository.
- Verify all three installations by running:
node -v
npm -v
git --version
Each command should print a version number. node -v should show a version starting with v22.
Seeing command not found for node?
If node -v returns an error, the NodeSource setup script may not have run successfully. Try running sudo -E bash nodesource_setup.sh again, then sudo apt install -y nodejs.
Help me fix Node.js installation on my EC2 instance.
Install and secure MySQL
With Node.js and Git ready, you now need the database server. You'll install MySQL and run a security hardening wizard to lock down the installation.
- Install MySQL Server by running:
sudo apt install mysql-server -y
sudo systemctl start mysql
sudo systemctl enable mysql
What do these commands do?
`sudo apt install mysql-server -y` installs MySQL Server 8.0 from Ubuntu's default repositories.
`sudo systemctl start mysql` starts the MySQL service immediately. `sudo systemctl enable mysql` configures MySQL to start automatically whenever the server reboots.
- Run the secure installation wizard by running:
sudo mysql_secure_installation
What will the wizard ask?
This wizard walks you through several security settings. It will prompt you for a series of choices. Respond to each prompt as follows:
- VALIDATE PASSWORD component: enter No.
- Set a strong root password when prompted. Remember this password for the next substep.
- Remove anonymous users: enter Yes.
- Disallow remote root login: enter Yes.
- Remove test database: enter Yes.
- Reload privilege tables: enter Yes.
Having trouble with the wizard?
If the wizard asks for a current root password and you haven't set one yet, press Enter to submit an empty password. On a fresh Ubuntu MySQL installation, the root account may not have a password set initially.
Help me troubleshoot mysql_secure_installation on Ubuntu.
Create the database, user, and table
Now you'll create the same database structure you set up locally, but with settings tailored to the EC2 environment. The user host and password differ from your local setup because the EC2 app connects to MySQL on the same machine.
- Log into MySQL by running:
sudo mysql -u root -p
Enter the root password you set during the secure installation wizard. You should see the mysql> prompt.
- Create the database, application user, and employees table by running these SQL commands:
CREATE DATABASE employee_db;
CREATE USER 'employee_user'@'localhost' IDENTIFIED BY 'StrongPassword!123';
GRANT ALL PRIVILEGES ON employee_db.* TO 'employee_user'@'localhost';
FLUSH PRIVILEGES;
USE employee_db;
CREATE TABLE employees (
id INT AUTO_INCREMENT PRIMARY KEY,
full_name VARCHAR(100) NOT NULL,
email VARCHAR(100) NOT NULL,
role VARCHAR(100),
department VARCHAR(100),
location VARCHAR(100),
join_date DATE NULL,
photo_url VARCHAR(255) DEFAULT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
EXIT;
What is different from the local setup?
Two things changed compared to the SQL you ran on your Windows machine:
- The user host is 'localhost' instead of '%'. On EC2, the app and database run on the same machine, so restricting connections to localhost is more secure.
- The password is StrongPassword!123 instead of StrongPassword123. Using different credentials across environments is a good security practice.
✔️ Awesome, I've got everything!
Great. Make sure you've exited the MySQL prompt and are back at the Ubuntu command line.
ⓧ I'd like to double check the full SQL
Here is the complete SQL you should have run in the MySQL prompt:
CREATE DATABASE employee_db;
CREATE USER 'employee_user'@'localhost' IDENTIFIED BY 'StrongPassword!123';
GRANT ALL PRIVILEGES ON employee_db.* TO 'employee_user'@'localhost';
FLUSH PRIVILEGES;
USE employee_db;
CREATE TABLE employees (
id INT AUTO_INCREMENT PRIMARY KEY,
full_name VARCHAR(100) NOT NULL,
email VARCHAR(100) NOT NULL,
role VARCHAR(100),
department VARCHAR(100),
location VARCHAR(100),
join_date DATE NULL,
photo_url VARCHAR(255) DEFAULT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
Seeing an access denied error?
If you see an error when running the CREATE USER or GRANT command, make sure you logged in with sudo mysql -u root -p (including sudo). Without sudo, you may not have the necessary privileges.
If a database called employee_db already exists from a previous attempt, run DROP DATABASE employee_db; first, then re-run the SQL commands from the top.
Help me fix MySQL access errors on my EC2 instance.
Your EC2 server now has the full development stack installed. Next up, you'll deploy the Employee Directory application on this server and test it from your browser.
Deploy and Test the Application on EC2
In the last step, you installed Node.js, MySQL, and Git on your EC2 instance and created the database. Your server environment is fully set up and ready to host the application.
Now it's time to deploy the Employee Directory application itself. You'll clone the same code that ran on your local machine, configure it to connect to the EC2 database, and verify the app is accessible from your browser over the internet.
In this step, get ready to:
- Clone the application repository and install its dependencies on EC2.
- Create the environment configuration file for the EC2 database.
- Start the application and verify it loads in your browser.
Clone and configure the application
- In your SSH session on the EC2 instance, clone the repository and install the application's dependencies by running these commands:
cd ~
git clone https://github.com/techwithlucy/ztc-projects.git
cd ztc-projects/projects/cloud-engineer-projects/project-1
npm install
What do these commands do?
- cd ~ navigates to your home directory on the EC2 instance.
- git clone downloads the entire project repository from GitHub.
- cd ztc-projects/projects/cloud-engineer-projects/project-1 moves into the specific project folder.
- npm install downloads and installs all the Node.js packages the application needs to run.
- Wait for npm install to finish. You should see a message like added X packages when it completes.
Seeing an error during clone or install?
- If git clone fails, check that your EC2 instance has internet access. The default VPC configuration should allow outbound traffic.
- If npm install fails, verify Node.js is installed correctly by running node -v. You should see a version number starting with v22.
- Help me troubleshoot errors during git clone or npm install on my EC2 instance.
The application needs a configuration file to know how to connect to the MySQL database on this EC2 instance. This is the .env file, which stores environment-specific settings like the database host, username, and password.
- Create the .env configuration file by opening it in vim:
vim .env
What is vim?
Vim is a text editor built into Linux. It works entirely inside the terminal, so you can edit files on a remote server without needing a graphical interface. It has different "modes" for inserting text versus running commands.
- Press i to enter insert mode. You should see -- INSERT -- at the bottom of the screen.
- Type the following configuration exactly as shown:
DB_HOST=localhost
DB_USER=employee_user
DB_PASSWORD=StrongPassword!123
DB_NAME=employee_db
PORT=3000
- Press Esc to exit insert mode.
- Type :wq and press Enter to save the file and exit vim.
What do these environment variables do?
- DB_HOST=localhost tells the app to connect to MySQL on the same machine.
- DB_USER and DB_PASSWORD are the MySQL credentials you created in the previous step.
- DB_NAME=employee_db points to the database where employee records are stored.
- PORT=3000 sets the port the web server listens on. This matches the port you opened in your security group.
Notice the password here is StrongPassword!123 (with an exclamation mark), which is different from the local version (StrongPassword123). In real deployments, each environment has its own unique credentials.
- Confirm the file was saved correctly by running:
cat .env
You should see the five configuration lines you just typed, starting with DB_HOST=localhost and ending with PORT=3000.
File looks empty or has typos?
- If cat .env shows nothing, you may have exited vim without saving. Run vim .env again and repeat the steps above.
- If you see a typo, open the file again with vim .env, press i to edit, fix the typo, then press Esc and type :wq to save.
- Help me fix my .env file in vim on the EC2 instance.
✔️ Awesome, I've got everything!
Great. Make sure your .env file is saved in the project-1 directory before moving on.
ⓧ I'd like to double check the full code
Here is the complete .env file for the EC2 instance. Compare it against your cat .env output.
DB_HOST=localhost
DB_USER=employee_user
DB_PASSWORD=StrongPassword!123
DB_NAME=employee_db
PORT=3000
Start the application and test
The application is configured and ready to launch. When you start the server, it reads the .env file and connects to the MySQL database automatically.
- Start the application server by running:
npm start
You should see two lines of output in your terminal:
Server running on http://localhost:3000
Connected to MySQL successfully
Why does it say localhost?
The server output shows localhost:3000 because that's the address from the server's own perspective. From the outside world, you access the app using the EC2 instance's public IP address on port 3000. The security group you configured in the previous step allows this external traffic through.
- Switch to your local browser on your Windows machine (not the SSH terminal).
- Navigate to the following URL, replacing the IP address with your EC2 instance's public IPv4 address:
http://[[EC2_PUBLIC_IP="your EC2 instance's public IPv4 address"]]:3000
You should see the Employee Directory application load with an Add Employee form and a Team Overview panel. This is the same application you ran locally, now running on your EC2 instance in the cloud.
Page not loading in your browser?
- Make sure you're using http:// (not https://). Some browsers auto-redirect to HTTPS, which this server does not support.
- Confirm your SSH terminal still shows the server running. If you accidentally closed it or pressed Ctrl+C, the server stopped. Run npm start again from the project-1 directory.
- Verify the public IP address matches what the EC2 console shows. Return to the Instances page in the AWS Console, select your instance, and check the Public IPv4 address field.
- Check that your security group allows inbound traffic on port 3000 from 0.0.0.0/0. You configured this in Step 3.
- Help me troubleshoot why my Employee Directory app isn't loading in the browser from my EC2 instance.
Add test employees
The Employee Directory is live on EC2, but the database is empty. Adding test employees proves the full stack is working, from the browser form to the MySQL database on your EC2 instance.
- In the Add Employee form, enter a full name (e.g. Jane Smith).
- Enter an email address (e.g. jane.smith@cloudhr.com).
- Fill in the Role, Department, Location, and Join Date fields.
- Submit the form to add the employee.
- Repeat this process to add 2-3 test employees total.
After adding employees, you should see them appear in the employees table below the form. The Team Overview panel should update to show the correct total count of employees, departments, and locations.
Why do these test records matter?
These employee records are stored in the MySQL database on your EC2 instance. In Project 2 of this series, you'll migrate this database to Amazon RDS using AWS DMS. The test records you add now become the data you'll migrate.
Employees not showing up in the table?
- Check that the Full Name and Email fields are filled in. These are required fields in the database.
- Verify that DB_PASSWORD in your .env file matches the password you set for employee_user in the previous step (StrongPassword!123).
- Help me figure out why employees aren't saving in my Employee Directory app on EC2.
Secret mission
Keep the App Running with PM2
Right now, closing your SSH session kills the Employee Directory app. Set up PM2 to keep it running after disconnections and configure automatic restarts on server reboot.
Clean Up Your Resources
Clean Up Your Resources
Decide whether to keep your resources running, pause them to come back later, or delete them entirely. The t2.micro EC2 instance is covered by AWS Free Tier credits for new accounts, but if your credits are exhausted, the instance incurs ongoing compute charges.
Cost warning
If left running beyond your Free Tier credits, the t2.micro instance costs approximately $0.0116/hour (~$8.50/month).
If you are not planning to start Project 2 immediately, choose Pause or Delete below to avoid unexpected charges.
Resources you used:
- EC2 instance: onprem-employee-directory-server (t2.micro, Ubuntu Server 24.04 LTS).
- Security group with inbound rules on ports 22 (SSH) and 3000 (application).
- Key pair on-prem (.pem file saved on your local machine).
- Local MySQL database employee_db with test employee records.
- Local project files in the ztc-projects folder.
Keep everything running
No action needed. Choose this if you plan to start Project 2 (migrating the database to Amazon RDS with AWS DMS) soon.
- Your EC2 instance stays running with the Employee Directory app accessible at http://EC2_PUBLIC_IP:3000.
- Your local MySQL database and project files remain on your Windows machine.
- Node.js, MySQL, and Git remain installed on both your local machine and the EC2 instance.
Pause - I'll come back to this later
Stop the EC2 instance to pause compute billing. Your data and configuration are preserved so you can restart when you are ready.
- In the AWS Management Console, navigate to EC2.
- Select the onprem-employee-directory-server instance.
- Click Instance state, then click Stop instance.
- Wait until the instance state shows Stopped.
What happens when you stop an instance?
Stopping an EC2 instance pauses compute charges. Storage charges still apply at approximately $0.10/GB/month for the default 8GB volume (~$0.80/month).
Your instance will receive a new public IP address when you restart it. Update the IP in your SSH command and browser URL when you come back.
Delete - I don't want to use this again
Remove all project resources and start fresh. You will need to recreate everything from scratch if you want to do Project 2 later.
Terminate the EC2 instance
- In the AWS Management Console, navigate to EC2.
- Select the onprem-employee-directory-server instance.
- Click Instance state, then click Terminate instance.
- Confirm the termination when prompted.
Delete the security group
Wait until the instance state shows Terminated before deleting the security group. Security groups cannot be deleted while an instance is still using them.
- In the EC2 sidebar, click Security Groups under Network & Security.
- Select the security group you created for this project (the one with inbound rules for ports 22 and 3000).
- Click Actions, then select the delete option.
- Confirm the deletion when prompted.
Delete the key pair
- In the EC2 sidebar, click Key Pairs under Network & Security.
- Select the on-prem key pair.
- Click Actions, then select the delete option.
- Confirm the deletion when prompted.
- Delete the on-prem.pem file from your local Windows machine.
Remove the local MySQL database
- Open a terminal on your local Windows machine and log into MySQL by running:
mysql -u root -p
- Drop the database and user by running these SQL commands:
DROP DATABASE employee_db;
DROP USER 'employee_user'@'%';
EXIT;
Delete the local project files
- Open File Explorer and navigate to the folder where you cloned the repository.
- Right-click the ztc-projects folder and select Delete.
That's a Wrap!
That's a Wrap!
Nice work! You just deployed the CloudHR Employee Directory application on an EC2 instance, simulating a real on-premises server in the cloud. This is the exact starting point you need for Project 2, where you will migrate the MySQL database to Amazon RDS using AWS DMS.
You've learned how to:
- Set up a local development environment with Node.js, MySQL, and Git, then ran the Employee Directory application as your "before migration" baseline.
- Launched an Ubuntu EC2 instance and configured security groups for SSH access and application traffic on port 3000.
- Deployed the full server environment on EC2 and tested the Employee Directory application live at http://EC2_PUBLIC_IP:3000.
- (Secret Mission) Configured PM2 to keep the app running after SSH disconnection and auto-start on server reboot.
Ready to quiz yourself?