My first attempt at Deploying a Backend Service on AWS: A Journey of Learning and Problem-Solving
Joseph Olugbohunmi
Posted on November 30, 2024
I have been developing backend services for about 3 years and have always deployed them on in-house servers. Deploying a backend service on AWS for the first time can be both exciting and intimidating. For me, the experience was filled with moments of triumph, a few roadblocks, and a lot of learning. In this article, I’ll walk you through my journey of deploying a backend service on AWS, including the challenges I faced and how I solved them.
NB: This article is also available on Medium
Why Amazon Web Services (AWS)?
I am currently building both the frontend (Android) and the backend service (SpringBoot) of a product (unfortunately, I can’t give too many details about this). After researching various cloud platforms, I chose AWS for its robust ecosystem and flexibility. For my project, I decided to use the free tier of two key AWS products: Amazon RDS for my PostgreSQL database and Amazon EC2 for deploying the backend service.
Setting Up the Database
The first step was setting up the database using Amazon RDS. I configured the instance with PostgreSQL and ensured it was running properly. To test the database, I used DBeaver, a reliable database management tool, to connect to the RDS instance. After verifying the connection and running some test queries, I was confident that the database setup was solid. I proceeded to provide the database URL and credentials in the application properties file.
Launching the EC2 Instance
Next, I focused on the service deployment. I launched an Amazon EC2 instance running on a Linux machine and configured a security group to allow:
- The service port for external requests.
- The database port for internal communication.
- SSH access for managing the server.
Using CLI commands, I connected to the EC2 instance via SSH from my local terminal:
chmod 400 /path/to/key.pem
ssh -i /path/to/key.pem ec2-user@<EC2-public-DNS>
Once inside the EC2 instance, I installed the necessary dependencies (Java in particular), copied the jar file and deployed the service:
sudo yum install java-17-amazon-corretto
scp -i /path/to/key.pem my-service.jar ec2-user@<EC2-public-DNS>
java -jar my-service.jar
Testing the Service
Once the service was up, I tested it using the public URL provided by AWS. Tools like Postman and the Android app I developed were invaluable. I integrated the URL and verified the service’s functionality by sending and receiving data. I was thrilled to see all endpoints working perfectly, with data being stored correctly in the database.
The Unexpected Roadblock and the Fix
Just when I thought I had everything figured out, an issue cropped up: the service became unreachable whenever my PC was turned off. This was puzzling because I assumed the EC2 instance would remain accessible independent of my local machine. After some troubleshooting and research, I realized that there was a problem with how I had configured the instance’s networking and persistence. By applying the correct adjustments, I ensured that the service was fully reachable regardless of my PC’s status. Here’s how I resolved it:
- Ensured the EC2 instance was running:
aws ec2 describe-instances --instance-ids <instance-id>
- Made the service persistent by creating a systemd service file:
sudo nano /etc/systemd/system/my-service.service #This opens a text editor to create the file
- Added the following content to the file:
[Unit]
Description=My Java Application
After=network.target
[Service]
ExecStart=/usr/bin/java -jar /path/to/my-service.jar
WorkingDirectory=/path/to/my-service-directory
StandardOutput=inherit
StandardError=inherit
Restart=always
User=ec2-user # Change this to the user running the app if different
[Install]
WantedBy=multi-user.target
- Saved the file and exited the editor (
Ctrl+O
,Enter
, thenCtrl+X
in Nano). - Reloaded the systemd daemon to recognize the new service, start the Java application service and enable it to start automatically when the EC2 instance boots:
sudo systemctl daemon-reload
sudo systemctl start my-service.service
sudo systemctl enable my-service.service
- Checked the service status:
sudo systemctl status my-service.service # An "active (running)" status indicates that the application is running successfully
sudo journalctl -u my-service.service # Checking the logs for troubleshooting
- After those changes, I confirmed the service’s availability by using the Android App when my PC was turned off, and it worked as expected!
Lesson learned
Deploying a backend service on AWS for the first time taught me a lot, including:
- The importance of testing every component individually before integrating them.
- Understanding AWS’s networking configurations and security group settings to allow only necessary traffic.
- Ensure critical services restart automatically and don’t rely on a local machine for accessibility.
- Staying calm and persistent when troubleshooting issues.
Conclusion
AWS is a powerful platform for deploying backend services, but like any tool, it has a learning curve. My journey from setting up RDS and EC2 to solving accessibility issues was challenging but was a fulfilling experience that combined technical skills with problem-solving. It also enhanced my understanding of cloud architecture. If you’re just starting with AWS, don’t be afraid to make mistakes — they’re part of the learning process. Here is an additional resource that you’d find helpful in estimating the cost of deploying your solution on AWS.
Feel free to share your thoughts or experiences in the comments. I’d love to hear how others have tackled similar challenges!
Posted on November 30, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
November 30, 2024