Learn to set up a production server on AWS for deploying your application and configuring a CI/CD pipeline.
In this guide, you’ll learn how to set up a production server on AWS to deploy your application. Later, this server will serve as the target when configuring your CI/CD pipeline. Follow these steps to launch an Amazon EC2 instance, configure SSH access, and set up a systemd service for your Flask app.
Open your terminal and connect via SSH using your key (e.g., main.pem) and the default username ec2-user:
Copy
Ask AI
ssh -i main.pem ec2-user@<Your_EC2_Instance_IP>
Upon connecting, you might see a prompt similar to:
Copy
Ask AI
The authenticity of host '3.89.97.104 (3.89.97.104)' can't be established.ED25519 key fingerprint is SHA256:MD1c0F1cn0pMsnd7TRAe1q3egJomo01RpuGpga4j3ARc.This key is not known by any other names.Are you sure you want to continue connecting (yes/no/[fingerprint])? yes.Warning: Permanently added '3.89.97.104' to the list of known hosts.Amazon Linux 2023https://aws.amazon.com/linux/amazon-linux-2023[ec2-user@ip-172-31-16-211 ~]$
The main.pem file is essential for authentication. Your Jenkins server will also use this key later to automatically copy your code and restart your application.
Step 4: Configuring a systemd Service for the Flask App
Next, configure a systemd service so that your Flask application is automatically managed by the server. Create the service file in the /etc/systemd/system directory using sudo with your preferred editor (e.g., vi):
Ensure that the paths in WorkingDirectory, Environment, and ExecStart correctly reflect your server’s configuration.After saving the file, reload systemd to register your new service:
Copy
Ask AI
sudo systemctl daemon-reload
Enable the service to start automatically on boot:
Copy
Ask AI
sudo systemctl enable flask-app.service
You should see an output like:
Copy
Ask AI
Created symlink /etc/systemd/system/multi-user.target.wants/flask-app.service → /etc/systemd/system/flask-app.service.
Start the service:
Copy
Ask AI
sudo systemctl start flask-app.service
Check its status to ensure it is running:
Copy
Ask AI
sudo systemctl status flask-app.service
At this stage, the service may fail because the app.py file has not yet been copied to /home/ec2-user/app. Once your application code is deployed, restart the service with:
This guide completes the initial setup of your production server. In the next tutorial, you will learn how to configure your CI/CD pipeline so that every time you push code to Git, the updated code is automatically copied to your server and the service is restarted.Happy deploying!