This document provides instructions on how to set up, run, and deploy a simple Flask application. This application includes basic authentication and role-based access control. I have already uploaded it to an EC2 instance, and you can access it through http://3.137.167.63:80/. The username and password can be found in auth.py.
These instructions will get your copy of the project up and running on your local machine for development and testing purposes.
To run this application, you'll need:
- Python 3.8 or higher
- pip (Python package installer)
- Docker installed on your AWS EC2 instance
- An AWS account and a configured EC2 instance
First, clone the repository to your local machine:
git clone https://github.com/victelepa/zeni-test
cd your-project-directory
Install the required Python packages
pip install -r requirements.txt
To run this application, you need to configure several environment variables that the application uses to determine its behavior and how it connects to external resources.
FLASK_APP
: Specifies the entry point of your Flask application; set this toapp.py
.FLASK_ENV
: Sets the environment in which the Flask app is running. Common values aredevelopment
,production
, ortesting
.DEVELOPMENT_DATABASE_URI
,PRODUCTION_DATABASE_URI
,TESTING_DATABASE_URI
: Specifies the database URIs for different environments.SECRET_KEY
: Used by Flask to keep sessions secure. Ensure this key is kept secret and never exposed in public code repositories.
Open your terminal and type the following commands to set up the environment variables for development:
export FLASK_APP=app.py
export FLASK_ENV=development
export DEVELOPMENT_DATABASE_URI='your_development_database_uri_here'
export SECRET_KEY='your_secret_key_here'
Open Command Prompt and use the following commands to set up the environment variables for development:
set FLASK_APP=app.py
set FLASK_ENV=development
set DEVELOPMENT_DATABASE_URI=your_development_database_uri_here
set SECRET_KEY=your_secret_key_here
This will start the Flask development server, and the application will be accessible at http://localhost:5000.
Once the environment variables are set, you can run the application using the following command:
flask run
To simplify development and deployment, you can run this application using Docker. Follow these steps to build and run the Docker container locally.
- If you haven't already, clone the repository to your local machine.
- Build the Docker image using the Dockerfile provided in the project. Run the following command in the root directory of the project:
docker build -t your-app-name .
- Run the container
docker run -p 5000:5000 your-app-name
- To run the Docker container with necessary environment variables, use the -e option to set each variable. For example:
docker run -p 5000:5000 -e FLASK_ENV=development -e SECRET_KEY=your_secret_key_here your-app-name
Follow these steps to deploy the application to an AWS EC2 instance using Docker:
- Prepare your EC2 instance
sudo yum update -y
sudo amazon-linux-extras install docker
sudo service docker start
sudo usermod -a -G docker ec2-user
mkdir app
cd app
- Add your PEM file to your local project directory, set the correct permissions, and securely copy your project files to your EC2 instance:
chmod 600 your_key_pair.pem
scp -i your_key_pair.pem Dockerfile app.py auth.py config.py schemas.py requirements.txt ec2-user@your_public_ip:/home/ec2-user/app
- Build and run your Docker container on EC2:
sudo docker build -t aws-zeni-docker-test:latest -f Dockerfile .
sudo docker run -d -p 80:5000 aws-zeni-docker-test:latest
- Access the application
- Open your browser and navigate to http://your_public_ip:80.
- Enter the username and password as defined in auth.py to see the welcome page.
Make sure you replace placeholders like your_project_directory, your_key_pair.pem, and your_public_ip with actual values specific to your AWS setup and project.
The development and usage of this Flask application are based on several key assumptions:
- Cross-Platform Compatibility: The application is developed to be compatible across Linux, MacOS, and Windows.
- Basic Security Practices: The application assumes a secure operating environment. Users are responsible for securing sensitive data, including the
SECRET_KEY
and database connections, particularly in production environments.
- Technical Proficiency: Users are assumed to have basic knowledge of Flask environment management and database setup.