Set Up a Web App in the Cloud
Set up a cloud dev environment and build a Java web app on EC2.
Introduction
30 Second Summary
When you work on a project, your code usually lives on your own computer. In professional DevOps teams, applications are built and deployed from servers in the cloud.
In this project, you will set up a cloud-based development environment on an AWS EC2 instance and generate a Java web application. This web app becomes the artifact you will build, test, and deploy throughout the rest of the 7 Day DevOps Challenge.
What You'll Build
A Java web application running on a cloud server, connected to your local VS Code editor for a seamless remote coding experience.
By the end of this project, you'll have:
- A running EC2 instance with Apache Maven and Java installed, ready to build your web app.
- A Java web app project (nextwork-web-project) with a personalized JSP page displaying your name.
- A VS Code remote development connection to your cloud instance via SSH for full IDE capabilities.
- Secret Mission: Edit your web app using a terminal-based text editor instead of a graphical IDE.
Are there any prerequisites?
This project is part of the 7 Day DevOps Challenge series. You need an AWS account with an IAM user, VS Code installed, a stopped EC2 instance running Amazon Linux 2023, and your key pair (.pem file) saved locally from the previous project.
Start Your EC2 Instance and Connect via SSH
Every CI/CD pipeline needs an application to deploy. Your EC2 instance is where you'll build that application, giving you a consistent Linux environment in the cloud.
Your instance is currently stopped from your last project. In this step, you'll start it back up and connect to it from your local machine using SSH.
In this step, get ready to:
- Start your EC2 instance and note its new Public IPv4 DNS.
- Update the security group to allow SSH access from your current IP address.
- Connect to the instance via SSH from VS Code's terminal.
Start the instance and update security group
Your EC2 instance's public address changes every time you stop and start it. You'll also need to update the security group so it allows connections from your current IP.
- Log in to the AWS Management Console with your IAM user.
- Type EC2 in the search bar at the top and select EC2.
- Click Instances in the left sidebar.
- Select the checkbox next to your stopped instance.
- Click the Instance state dropdown at the top.
- Select Start instance.
- Wait for the Instance state column to show Running. Refresh the page if needed.
- In the details panel below, find and copy the Public IPv4 DNS value. You'll need this to connect.
Why does the Public IPv4 DNS change?
AWS assigns a new public address to your instance every time it starts. The old address from your previous session no longer works.
This means you'll need to grab the new DNS value and update your SSH command each time you stop and restart your instance.
- Click the Security tab in the instance details panel.
- Click the link under Security groups.
- Click Edit inbound rules.
- Find the SSH rule (port 22) and change its Source dropdown to My IP.
- Click Save rules.
What is a security group?
A security group acts as a virtual firewall for your instance. It controls which IP addresses are allowed to connect to specific ports.
By setting the SSH rule's source to My IP, you're telling AWS to only allow SSH connections from the IP address you're using right now. This keeps your instance secure from unauthorized access.
Connect to your EC2 instance via SSH
Now that your instance is running and your security group allows your IP, you can connect to it from VS Code's terminal.
- Open VS Code.
- Open a new terminal by clicking Terminal in the top menu bar, then New Terminal.
- Navigate to your DevOps folder where your .pem file is stored by running this command:
cd ~\Desktop\DevOps
- Connect to your EC2 instance by running this command:
ssh -i nextwork-keypair.pem ec2-user@[[EC2_PUBLIC_DNS="your Public IPv4 DNS"]]
What does this command do?
ssh starts a secure shell connection to a remote machine.
-i nextwork-keypair.pem tells SSH to use your key pair file as the login credential instead of a password.
ec2-user is the default username on Amazon Linux instances. The part after @ is the address of your instance.
- When asked "Are you sure you want to continue connecting?", type yes and press Enter. This is a one-time trust prompt for the host fingerprint.
Verify your connection
Your terminal prompt should now show something like [ec2-user@ip-...]$. This confirms you are inside your EC2 instance.
Connection timed out?
If your SSH connection hangs or times out, your security group likely does not have your current IP address. Your public IP can change between sessions.
Visit http://checkip.amazonaws.com in your browser to see your current IP. Compare it to the IP listed in your security group's SSH rule. If they don't match, update the rule to My IP again.
Help me troubleshoot my SSH connection to EC2
You're connected to your cloud instance. Next up, you'll install the tools you need to build a Java web application.
Install Apache Maven and Java
Your SSH connection is active and your EC2 instance is ready. Now it's time to install the build tools that will power your web application.
You need two pieces of software on your instance. Apache Maven handles project generation and builds. Amazon Corretto 8 provides the Java runtime that Maven depends on.
In this step, get ready to:
- Install Apache Maven 3.5.2 on your EC2 instance.
- Install Amazon Corretto 8 (Java) on your EC2 instance.
- Verify both tools are working correctly.
Install Apache Maven 3.5.2
Maven is a build automation tool for Java projects. It generates project templates, manages dependencies, and packages your code into deployable files.
- Download and install Maven by running these commands in your SSH terminal:
wget https://archive.apache.org/dist/maven/maven-3/3.5.2/binaries/apache-maven-3.5.2-bin.tar.gz
sudo tar -xzf apache-maven-3.5.2-bin.tar.gz -C /opt
echo "export PATH=/opt/apache-maven-3.5.2/bin:$PATH" >> ~/.bashrc
source ~/.bashrc
What do these commands do?
- wget downloads the Maven 3.5.2 archive from Apache's servers.
- sudo tar -xzf ... -C /opt extracts the archive into the /opt directory, which is the standard location for optional software on Linux.
- echo "export PATH=..." >> ~/.bashrc permanently adds Maven's bin folder to your command path.
- source ~/.bashrc reloads your shell configuration so the new path takes effect immediately.
The download takes about 30-45 seconds. Wait until you see the command prompt return before continuing.
Download timing out?
If the wget command hangs or fails, check that your EC2 instance has internet access. Your security group needs an outbound rule allowing HTTPS traffic (port 443). Most default security groups include this already.
Help me troubleshoot my Maven download
Install Amazon Corretto 8 (Java)
Amazon Corretto is Amazon's free, production-ready distribution of the Java Development Kit. Maven needs Java to run, and your web application will be compiled with it.
- Install Java and configure its path by running these commands:
sudo dnf install -y java-1.8.0-amazon-corretto-devel
export JAVA_HOME=/usr/lib/jvm/java-1.8.0-amazon-corretto.x86_64
export PATH=/usr/lib/jvm/java-1.8.0-amazon-corretto.x86_64/jre/bin/:$PATH
What do these commands do?
- sudo dnf install -y uses Amazon Linux's package manager to install the Corretto 8 JDK. The -y flag auto-confirms the installation.
- export JAVA_HOME=... sets an environment variable that tells other tools (like Maven) where Java lives.
- export PATH=... adds the Java runtime to your command path so you can run java from anywhere.
Seeing a permission denied error?
Make sure you included sudo at the start of the install command. The ec2-user account needs elevated permissions to install system packages.
Help me fix this permission error
Verify installations
Both tools should now be ready to use. Let's confirm they are installed correctly.
- Verify Maven is installed by running:
mvn -v
- You should see Apache Maven 3.5.2 in the first line of the output, along with your Java version and OS details.
- Verify Java is installed by running:
java -version
- You should see openjdk version "1.8.0_492" (or a similar Corretto 8 version number) in the output.
What if my version number is slightly different?
The exact patch number (e.g. 492) may differ depending on when Amazon released the latest Corretto update. As long as the output starts with 1.8.0 and mentions Corretto, you're good to go.
java -version not showing Corretto 8?
If you see a different Java version or provider, your instance may have multiple Java versions installed. Run this command to switch to Corretto 8:
sudo alternatives --config java
Select the option that points to Corretto 8 from the list. Then run java -version again to confirm.
Help me pick the right Java version
Your cloud instance now has both build tools ready to go. Next up, you'll use Maven to generate a brand new Java web application project.
Generate and Explore Your Java Web App
You have Maven and Java installed on your EC2 instance. Now you need an actual web application to work with.
Apache Maven can generate project templates called archetypes. You will use the webapp archetype to create a ready-made Java web app in seconds.
In this step, get ready to:
- Generate a Java web app project using Maven's webapp archetype.
- Explore the generated project structure and understand what each file does.
Generate the web app using Maven
Every CI/CD pipeline needs something to build, test, and deploy. This Maven command generates the web application that will travel through your entire DevOps pipeline.
- Generate your Java web app project by running this command in your SSH terminal:
mvn archetype:generate -DgroupId=com.nextwork.app -DartifactId=nextwork-web-project -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false
What does this command do?
This command tells Maven to generate a project from a template. Each flag configures a different part of the project:
- -DgroupId=com.nextwork.app sets the package namespace for your Java code (like a company identifier).
- -DartifactId=nextwork-web-project sets the project folder name and build output name.
- -DarchetypeArtifactId=maven-archetype-webapp tells Maven to use the web application template.
- -DinteractiveMode=false skips interactive prompts and uses the provided values directly.
- Wait for Maven to finish downloading dependencies and generating the project. This takes 30-45 seconds.
- Confirm you see [INFO] BUILD SUCCESS in your terminal output.
Not seeing BUILD SUCCESS?
- If you see a Java-related error, confirm Java is configured by running java -version. You should see openjdk version "1.8.0_492" or similar.
- If Maven cannot download dependencies, check your instance has internet access. A security group with outbound rules set to "All traffic" allows this.
- If the command is not found, run source ~/.bashrc to reload your PATH, then try again.
Still stuck? Ask for help.
Explore the generated project structure
Maven generated an entire project folder with the right structure for a Java web application. Let's see what's inside.
- List all the files Maven created by running:
find nextwork-web-project -type f
- You should see three files listed in the output: pom.xml, index.jsp, and web.xml.
Here is how the full project structure looks, including directories:
nextwork-web-project/
├── pom.xml
└── src/
└── main/
├── webapp/
│ ├── index.jsp
│ └── WEB-INF/
│ └── web.xml
└── resources/
What are these files?
- pom.xml is Maven's project configuration file. It defines your project's dependencies, build settings, and output format.
- index.jsp is the main web page. JSP (JavaServer Pages) is like HTML but can run Java code on the server.
- web.xml configures the web application's behavior, like which page loads first and how requests are handled.
- resources/ is an empty folder where you would add configuration files or static assets later.
Your Java web app project is generated and ready. Next, you will connect VS Code directly to your EC2 instance so you can edit these files with a full IDE instead of the terminal.
Connect VS Code to Your EC2 Instance
Your EC2 instance has Maven, Java, and a freshly generated web app project. Right now, you can only interact with those files through the terminal.
The Remote - SSH extension for VS Code lets you open folders on a remote server directly in your local editor. You get syntax highlighting, a file explorer, and every VS Code feature you are used to, while the code lives on your EC2 instance.
In this step, get ready to:
- Install the Remote - SSH extension in VS Code.
- Add your EC2 instance as a saved SSH host.
- Open your web app project folder in a remote VS Code window.
Install the Remote-SSH extension
VS Code extensions add extra capabilities to the editor. The Remote - SSH extension adds the ability to open folders and run terminals on a remote machine over SSH.
- Switch back to your VS Code window on your local machine.
- Click the Extensions icon in the left sidebar (it looks like four small squares).
- Type Remote - SSH in the search bar at the top of the Extensions panel.
- Find the extension named Remote - SSH by Microsoft and click Install.
After installation, you will see a new indicator appear at the bottom-left corner of the VS Code Status bar.
What is this indicator?
The remote indicator shows whether VS Code is running in a local or remote context. Clicking it opens a menu of Remote-SSH commands. You will use it in the next substep.
Don't see the extension?
Make sure you searched for Remote - SSH (with a dash and spaces). The publisher should be Microsoft. If no results appear, check your internet connection and try again.
help me find and install the Remote - SSH extension in VS Code
Add your EC2 instance as an SSH host
Instead of typing your SSH command every time you want to connect, you can save your EC2 instance as a host in VS Code. This stores the connection details in a config file on your machine.
- Click the remote indicator at the bottom-left corner of the VS Code Status bar.
- Select Connect to Host... from the menu that appears.
- Select + Add New SSH Host... from the list.
- Enter the SSH command below, replacing the placeholder values with your own Windows username and EC2 Public IPv4 DNS:
ssh -i C:\Users\[[WIN_USERNAME="YOUR WINDOWS USERNAME"]]\Desktop\DevOps\nextwork-keypair.pem ec2-user@[[EC2_PUBLIC_DNS="YOUR PUBLIC IPV4 DNS"]]
Where do I find my Public IPv4 DNS?
Return to the AWS Management Console. Go to EC2 > Instances, select your running instance, and copy the Public IPv4 DNS value from the details panel. It looks something like ec2-XX-XX-XX-XX.compute-1.amazonaws.com.
- Press Enter to confirm the command.
- VS Code asks which SSH config file to update. Select the first option (C:\Users\YOURUSERNAME\.ssh\config).
A notification in the bottom-right confirms the host was added.
- Click the remote indicator at the bottom-left corner again.
- Select Connect to Host... from the menu.
- Select your EC2 instance from the list (it shows the DNS address you entered).
- A new VS Code window opens. When VS Code asks for the platform of the remote host, select Linux.
VS Code will take a moment to install its server on the remote machine. Wait until the remote indicator at the bottom-left shows SSH: followed by your host address.
Connection timed out or refused?
This usually means your security group SSH rule does not have your current IP. Go to the AWS Console, navigate to your instance's Security tab, click the Security Group link, and update the SSH inbound rule source to My IP.
Also confirm the .pem file path in your SSH command is correct. It should point to C:\Users\YOURUSERNAME\Desktop\DevOps\nextwork-keypair.pem.
help me troubleshoot my VS Code Remote-SSH connection to EC2
Open your web app project in VS Code
You are now in a remote VS Code window connected to your EC2 instance. The editor is running against the remote file system, so any file you open or edit lives on the server.
- In the new VS Code remote window, click the Explorer icon in the left sidebar.
- Click Open Folder.
- Enter /home/ec2-user/nextwork-web-project in the path field and press OK.
- Click Yes, I trust the authors when the trust prompt appears.
Your Explorer sidebar now shows the project files: pom.xml and the src folder.
Why use Remote-SSH instead of editing locally?
Your Java web app lives on the EC2 instance where Maven and Java are installed. Editing remotely means you can build and run the project on the same machine where your tools live, without copying files back and forth.
This is the same workflow professional developers use when working on cloud servers.
Don't see pom.xml in the Explorer?
Make sure you entered the path as /home/ec2-user/nextwork-web-project (not just /home/ec2-user). If you opened the wrong folder, click File > Open Folder and re-enter the correct path.
help me open my nextwork-web-project folder in VS Code Remote-SSH
Your cloud IDE is ready to go. Next up, you will customize your web app with personalized content.
Customize Your Web App
Your VS Code window is now connected directly to your EC2 instance. Any file you edit and save in VS Code is changed on the remote server instantly.
In this step, you will personalize the default web page that Maven generated. This is the file that becomes your deployed web application across the rest of the DevOps challenge.
In this step, get ready to:
- Edit your web app's main page with personalized content.
- Verify your changes are saved on the EC2 instance.
Edit index.jsp
The index.jsp file is your web app's main page. Right now it only displays the default "Hello World!" text that Maven generated.
- In VS Code's Explorer sidebar, expand src > main > webapp.
- Click on index.jsp to open it in the editor.
- Select all the existing content (Ctrl+A on Windows or Cmd+A on macOS) and replace it with the following code:
<html>
<body>
<h2>Hello [[YOURNAME="your name"]]!</h2>
<p>This is my NextWork web application working!</p>
</body>
</html>
What does this code do?
- The <h2> tag displays a personalized greeting with your name as a heading.
- The <p> tag adds a paragraph confirming your NextWork web application is working.
- JSP files look like HTML but can also run Java code on the server. For now, your page is pure HTML.
- Save the file by pressing Ctrl+S (Windows) or Cmd+S (macOS).
File not saving?
- If VS Code shows a permission error, your Remote-SSH connection may have disconnected. Check the bottom-left corner of VS Code for the green SSH indicator. If it is missing, reconnect to your EC2 host.
- Make sure you opened the correct folder (/home/ec2-user/nextwork-web-project) when you connected via Remote-SSH.
Why can't I save my index.jsp file on the remote server?
Verify your changes
- Look at the index.jsp tab at the top of the editor. The dot next to the filename disappears once the file is saved successfully.
- Confirm your personalized name appears in the <h2> line and the paragraph text reads This is my NextWork web application working!.
Your web app is now customized and ready to be used in the rest of the DevOps challenge. Every CI/CD tool you set up from here will build, test, and deploy this exact application.
✔️ Awesome, I've got everything!
Great. Double check you have saved your file and the dot indicator is gone from the tab.
ⓧ I'd like to double check the full code
Here is the full index.jsp file for comparison:
<html>
<body>
<h2>Hello YOURNAME!</h2>
<p>This is my NextWork web application working!</p>
</body>
</html>
Secret mission
Edit Code Without an IDE
What happens when you need to fix a file on a server that doesn't have VS Code? In this challenge, you'll edit your web app using nano, a terminal-based text editor that works on virtually every Linux server.
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.
Your EC2 instance is covered by the AWS Free Tier (750 hours/month of t2.micro for 12 months). Since your account is about 1 week old, you are well within the free tier window.
Cost warning
If your EC2 instance stays running beyond 750 free hours in a single month, or your 12-month free tier window expires, AWS will charge you for the instance. If you are not continuing the DevOps Challenge immediately, stop your instance to avoid any risk.
Resources you used:
- EC2 instance (t2.micro or t3.micro, running on AWS with Maven, Java, and your web app project installed).
- VS Code Remote - SSH extension (installed locally in VS Code).
Keep everything running
No action needed. Choose this if you plan to continue Day 2 of the 7 Day DevOps Challenge today or tomorrow.
- Your EC2 instance stays running and your web app project remains intact.
- Your VS Code Remote - SSH connection will remain configured for next time.
- No cost impact while you remain within the free tier (750 hours/month for 12 months).
Pause - I'll come back to this later
Stop your EC2 instance to free up free tier hours. Your files and installations are preserved on the instance's storage.
- In the AWS Management Console, navigate to EC2, then select Instances.
- Select your instance, then click Instance state, then Stop instance.
- You will not be charged for a stopped instance. You only pay for its EBS storage, which is free-tier eligible.
- Your Public IPv4 DNS will change the next time you start the instance. You will need to update your SSH command and VS Code SSH config with the new address.
Delete - I don't want to use this again
Permanently delete your EC2 instance and everything on it. Only choose this if you are done with the DevOps Challenge entirely.
- In the AWS Management Console, navigate to EC2, then select Instances.
- Select your instance, then click Instance state, then Terminate instance.
- This permanently deletes the instance, including Maven, Java, and your nextwork-web-project folder.
- To remove the Remote - SSH extension from VS Code, click the Extensions icon in the sidebar, find Remote - SSH, and click Uninstall.
That's a Wrap!
That's a Wrap!
Nice work! You just set up a full cloud development environment on an EC2 instance and generated a Java web application ready for the rest of the 7 Day DevOps Challenge.
You've learned how to:
- Connect to a running EC2 instance via SSH and set up VS Code Remote-SSH for full IDE capabilities in the cloud.
- Install Apache Maven 3.5.2 and Amazon Corretto 8 (Java) on a cloud server to support building Java web applications.
- Generate and customize a Java web application using Maven's webapp archetype, creating the deployable artifact for the rest of the DevOps challenge.
- (Secret Mission) Edit files directly in the terminal using nano without any graphical IDE.
Ready to quiz yourself?