A Python Flask application designed to update specific tables in a Google Cloud SQL MySQL database. This project demonstrates connecting to Cloud SQL using multiple connection methods and automates updating database records via API endpoints.
Features
- Flask-based REST API for updating database tables.
- Supports multiple Cloud SQL connection methods: TCP, Unix socket, and Cloud SQL Python Connector with optional IAM authentication.
- Kubernetes deployment manifests for service and deployment.
- Local testing setup with environment variables and service account credentials.
Tech Stack
- Python 3.10
- Flask
- SQLAlchemy
- Google Cloud SQL (MySQL)
- Google Cloud SQL Python Connector
- Kubernetes (GKE) manifests
- Docker
Getting Started
Prerequisites
- Python 3.10+
- Google Cloud project with Cloud SQL instance
- Service account with Cloud SQL Client permissions
- Docker (for container builds)
- Kubernetes cluster (optional, for deployment)
Installation
- Clone the repository:
git clone https://github.com/justin-napolitano/flask-api-updater.git
cd flask-api-updater/src
- Create and activate a virtual environment:
virtualenv --python=python3 env
source env/bin/activate
- Install dependencies:
pip install -r requirements.txt
- Set environment variables (example
.env):
export GOOGLE_APPLICATION_CREDENTIALS='secret.json'
export INSTANCE_CONNECTION_NAME='your-project:region:instance'
export DB_USER='your-db-user'
export DB_PASS='your-db-password'
export DB_NAME='your-db-name'
# Optionally set INSTANCE_HOST or INSTANCE_UNIX_SOCKET depending on connection method
Running Locally
python app.py
Open your browser and navigate to http://127.0.0.1:8080 to verify the app is running.
Project Structure
flask-api-updater/
βββ README.md
βββ index.md
βββ src/
β βββ app.py # Flask app with API endpoints
β βββ main.py # Cloud Functions HTTP handler
β βββ connect_connector.py # Cloud SQL Python Connector connection
β βββ connect_connector_auto_iam_authn.py # Connector with IAM auth
β βββ connect_tcp.py # TCP connection method
β βββ connect_unix.py # Unix socket connection method
β βββ connection_test.py # Pytest tests for app
β βββ service.yaml # Kubernetes Service manifest
β βββ deployment.yaml # Kubernetes Deployment manifest
β βββ app.standard.yaml # App Engine standard environment config
β βββ app.flexible.yaml # App Engine flexible environment config
β βββ service-account.yaml # Kubernetes service account manifest
β βββ cloudbuild.yaml # Cloud Build config for production
β βββ cloudbuild-dev.yaml # Cloud Build config for dev
β βββ README.md # Cloud SQL connection instructions
β βββ secret.json # Service account credentials (example)
Future Work / Roadmap
- Complete and extend API endpoints for updating additional tables.
- Integrate Google Cloud Secret Manager for secure credential handling.
- Add comprehensive unit and integration tests.
- Automate deployment with CI/CD pipelines.
- Improve error handling and logging.
- Add support for additional database engines or cloud providers.
Note: Some files like secret.json contain sensitive information and should be managed securely and excluded from public repositories.