Set Up a Web App in the Cloud
Project 1 of the 6 Day DevOps Challenge - Kick off CI/CD pipeline with this foundational project!
Project work by Pooja Das on NextWork.
Introducing Today's Project!
This is the first day of the DevOps challenge. In this project, I will start building the foundation for a CI/CD pipeline by creating a simple web application. I will launch an AWS EC2 instance and connect to it using VS Code to set up and run the web application.
This project is part one of a series of DevOps projects where I'm building a CI/CD pipeline. I'll be working on the next project in the coming days as I continue learning more about DevOps tools and practices.
I did this project to learn how to set up and manage a web application on a cloud server using AWS EC2. This project helped me understand how DevOps engineers connect to servers, install tools like Java and Maven, and manage application files using both the terminal and VS Code.
Key tools and concepts
Services I used were Amazon EC2 for creating and running a cloud server, and Visual Studio Code for editing and managing the application files. I also used SSH to securely connect to the EC2 instance.
Key concepts I learnt include launching and managing an EC2 instance, connecting to a server using SSH, installing tools like Java and Maven, generating a Java web application with Maven, and editing application files using both VS Code and the terminal.
Project reflection
This project took me approximately a few hours to complete. The most challenging part was setting up the SSH connection and connecting VS Code to the EC2 instance. It was most rewarding to successfully create and edit a Java web application running on a cloud server.
One thing I didn’t expect in this project was how easy it was to generate a web application using Maven. I was surprised that with a single command, Maven could automatically create the entire project structure and required files for a Java web application.
Launching an EC2 instance
What I did in this step
In this step, I will configure the network settings for my EC2 instance by allowing SSH access and setting up security group rules. This is important because it allows me to securely connect to the server and manage my web application from my local computer.
I started this project by launching an EC2 instance because I need a virtual server in the cloud to host and run my web application. The EC2 instance provides computing resources like CPU, memory, and storage, which allows me to develop and test the application in a cloud environment.
I also enabled SSH
SSH (Secure Shell) is a protocol used to securely connect to and manage a remote server. It ensures that only authorized users can access the server by using key-based authentication. I enabled SSH so that I can securely connect to my EC2 instance from my local computer and run commands on the server.
Key pairs
A key pair is a set of two cryptographic keys used to securely access an EC2 instance. It includes a public key stored on the server and a private key that is downloaded and kept by the user. The private key is used to securely connect to the EC2 instance through SSH.
Downloaded key pair file
Once I set up my key pair, AWS automatically downloaded the private key file (.pem) to my local computer. This file is used to securely connect to the EC2 instance using SSH.
Set up VS Code
What I did in this step
In this step, I will install Visual Studio Code and set up a terminal so I can connect to my EC2 instance using SSH. This allows me to access the server, create and edit my web application files, and manage the project directly from my computer.
What is VS Code?
Visual Studio Code (VS Code) is a lightweight code editor developed by Microsoft that is used to write, edit, and manage source code. It supports many programming languages and extensions, and it allows developers to connect to remote servers using SSH to develop applications.
I installed VS Code to connect to my EC2 instance using SSH and to create and edit the web application files. It helps me write code, run commands in the terminal, and manage my project directly from my computer.
My first terminal commands
A terminal is a command-line interface that allows users to interact with the computer by typing commands instead of using a graphical interface. The first command I ran for this project is `cd ~/Desktop/DevOps`, which helped me navigate to the DevOps folder where my .pem file is stored.
Updating file permissions
I also updated my private key's permissions by using the `chmod 400` command on the .pem file. This restricts access so that only the file owner can read the private key, which is required for securely connecting to the EC2 instance using SSH.
SSH connection to EC2 instance
What I did in this step
In this step, I will connect to my EC2 instance using SSH and the private key (.pem) file from my terminal. This allows me to securely access the server so I can start setting up and running my web application inside the EC2 environment.
Connecting to EC2
To connect to my EC2 instance, I ran the command `ssh -i devops-key.pem ubuntu@ec2-13-200-255-84.ap-south-1.compute.amazonaws.com` in the terminal. This command uses SSH and the private key file to securely access the EC2 server.
This command required an IPv4 address
A server’s IPv4 DNS is the public domain name that points to the server’s IPv4 address. It allows users and systems to connect to the server easily without needing to remember the numeric IP address.
Maven & Java
What I did in this step
In this step, I will install Apache Maven and Amazon Corretto 8 on my EC2 instance because these tools are required to build and run Java web applications. Maven helps manage project dependencies and build the application, while Java provides the environment needed to run the application.
Why I'm using Maven
Apache Maven is a build automation and project management tool used for Java applications. It helps developers compile code, manage project dependencies, and organize the structure of software projects. .
Maven is required in this project because it helps build and manage the Java web application. It automatically downloads project dependencies and organizes the project structure, making it easier to develop and run the web application.
Why I'm using Java
Java is a widely used programming language that developers use to build applications such as web applications, mobile apps, and enterprise software. It allows programs to run on different operating systems through the Java Virtual Machine (JVM).
Java is required in this project because the web application is built using Java. It provides the runtime environment needed to compile and run the application, and it is also required for Apache Maven to build the project.
Create the Application
What I did in this step
In this step, I will use Maven commands to generate a Java web application on my EC2 instance. This allows me to quickly create the project structure and necessary files needed to start building the web application.
Creating the Java web app
I generated a Java web app using the command `mvn archetype:generate -DgroupId=com.nextwork.app -DartifactId=nextwork-web-project -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false`. This command used Maven to create a new Java web application project with a predefined project structure.
Installing Remote - SSH
I installed Remote - SSH, which is a VS Code extension that allows me to securely connect to a remote server using SSH. I installed it to connect VS Code to my EC2 instance so I can view, edit, and manage the files of my Java web application directly from the editor.
SSH configuration details
Configuration details required to set up a remote connection include the Host name, HostName (the EC2 instance's public DNS), the User for the server, and the IdentityFile which specifies the path to the private key (.pem file) used for authentication.
Create the Application
Exploring the project structure
Using VS Code's file explorer, I could see the structure of my Java web application project. It included folders such as src/main/webapp and files like index.jsp, web.xml, and pom.xml, which are part of the web application created by Maven.
Two of the project folders created by Maven are src and webapp, which store the source code and web application files. The src folder contains the main project code and resources, while the webapp folder contains the web files such as JSP pages, configuration files, and other resources used to run the web application.
Using Remote - SSH
What I did in this step
In this step, I will connect Visual Studio Code to my EC2 instance using the Remote-SSH extension. This allows me to view, edit, and manage the files of my Java web application directly on the server using VS Code’s IDE features.
Updating the web app
The index.jsp is the main web page file in a Java web application. It contains markup similar to HTML to display content in a browser, but it can also include Java code to generate dynamic content.
I edited the index.jsp file by opening it in VS Code using the file explorer, modifying the HTML content in the editor, and saving the changes using Command + S.
Using nano
Verifying my work
To verify my editing work in the terminal, I reopened the index.jsp file and checked that the changes I made were saved correctly. It was possible to see my changes in VS Code right away because both the terminal and VS Code were connected to the same EC2 instance and project folder.