Skip to content

Latest commit

 

History

History
123 lines (86 loc) · 4.52 KB

README.md

File metadata and controls

123 lines (86 loc) · 4.52 KB

📈 💰 Expense Tracker (Intermediate)

Backend CRUD APIs for tracking expenses!

expense-tracker application

Before you begin

Code Installation

Download the code for this tutorial.

  1. Clone the repository using Git and change directories to expense-tracker folder.
git clone https://github.com/jackwotherspoon/cloud-sql-fastapi.git
cd cloud-sql-fastapi/expense-tracker

Set up Cloud SQL database

  1. Create a Postgres Cloud SQL Instance by following these instructions.

Note the connection string, database user, and database password that you create.

  1. Create a database for your application by following these instructions.

Note the database name.

  1. Create a service account with the Cloud SQL Client IAM role by following these instructions.

Download the JSON key for the service account to authenticate your connection for local development.

Running locally

To deploy the application locally on your machine:

  1. Install the dependencies

    pip install -r requirements.txt
  2. Fill in the .env file with your Cloud SQL specific values and path to service account JSON key.

    INSTANCE_CONNECTION_NAME="project-id:region:instance-name"
    DB_USER="my-db-user"
    DB_PASS="my-db-pass"
    DB_NAME="my-database"
    GOOGLE_APPLICATION_CREDENTIALS="path/to/keys.json"
    
  3. Run the application

    uvicorn app.main:app --reload

The application is now running locally! Point your web browser at http://127.0.0.1:8000/docs to view the OpenAPI specs for it and to play around with making requests.

Note: Remember to remove the --reload when not in a development environment. It helps a lot during development, but you shouldn't use it in production.

Deploy to Cloud Run

The application can be deployed to Cloud Run through the following steps:

  1. Build the container image

Replace <PROJECT_ID> with your Google Cloud Project ID.

gcloud builds submit --tag gcr.io/<PROJECT_ID>/cloud-sql-fastapi
  1. Deploy the service to Cloud Run Replace environment variables with the correct values for your Cloud SQL instance configuration as well as service account email of previously created service account.
gcloud run deploy cloud-sql-fastapi --image gcr.io/<PROJECT_ID>/cloud-sql-fastapi \
  --service-account='<SERVICE_ACCOUNT_EMAIL>' \
  --set-env-vars INSTANCE_CONNECTION_NAME='<PROJECT_ID>:<INSTANCE_REGION>:<INSTANCE_NAME>' \
  --set-env-vars DB_USER='<YOUR_DB_USER_NAME>' \
  --set-env-vars DB_PASS='<YOUR_DB_PASSWORD>' \
  --set-env-vars DB_NAME='<YOUR_DB_NAME>'

Take note of the URL output at the end of the deployment process. This is the endpoint for your FastAPI application!

Point your browser at the /docs endpoint of your output URL to view the OpenAPI specs and to make requests to your application!

Private IP Cloud SQL Connections

The application can also be deployed to Cloud Run using Private IP Cloud SQL connections. Private IP allows your database to not be accessible from the public internet.

First make sure your Cloud SQL instance is configured to have a Private IP address. (Configure Private IP for Cloud SQL)

Private IP Cloud SQL instance(s) should be connected to a VPC Network which can be accessed securely via Cloud Run using Serverless VPC Access which creates a VPC Connector.

The VPC Connector can be attached to your Cloud Run service to allow Private IP connections to the Cloud SQL instance on the same VPC Network.

gcloud run deploy cloud-sql-fastapi --image gcr.io/<PROJECT_ID>/cloud-sql-fastapi \
  --service-account='<SERVICE_ACCOUNT_EMAIL>' \
  --vpc-connector='<VPC_CONNECTOR_NAME>' \
  --set-env-vars INSTANCE_CONNECTION_NAME='<PROJECT_ID>:<INSTANCE_REGION>:<INSTANCE_NAME>' \
  --set-env-vars DB_USER='<YOUR_DB_USER_NAME>' \
  --set-env-vars DB_PASS='<YOUR_DB_PASSWORD>' \
  --set-env-vars DB_NAME='<YOUR_DB_NAME>' \
  --set-env-vars PRIVATE_IP=True

The application is now deployed using Private IP database connections!