-
Template for running FastAPI on Google Cloud Run.
-
Deploying API on Cloud Run with FastAPI in one-click and quickly.
- If you want to store the IMAGE in a container registry, please use
deploy.sh
andconfig.env
(the following instructions are based on this). - If you want to store the IMAGE in an artifact registry, please use
deploy_art.sh
andconfig_art.sh
(recommended option).
- If you want to store the IMAGE in a container registry, please use
gcloud CLI
-
Build Image on Cloud (Container Registry)
gcloud builds submit --tag {REGION}.gcr.io/{PROJECT_ID}/{IMAGE} --ignore-file .gcloudignore
-
Deploy Service to Cloud Run*
gcloud run deploy --image {REGION}.gcr.io/{PROJECT_ID}/{IMAGE} --platform managed --port 8000 --memory {1Gi} --timeout={2m}
-
-
Automation: By using these tools, you can automate the process of deploying resources and infrastructure, which saves time and reduces the potential for human error.
-
Consistency: Automation ensures that the deployment process is consistent across environments, which makes it easier to manage and troubleshoot issues.
-
Reusability: With Cloud Deployment Manager or shell script, you can reuse templates and scripts to deploy the same resources and infrastructure multiple times, making it more efficient to set up new environments.
-
Scalability: These tools enable you to deploy resources and infrastructure at scale, which is especially useful for large-scale projects.
-
Version control: By storing the templates and scripts in version control, you can track changes and roll back to a previous version if necessary.
In our deploy.sh
we will:
- Get ENV from
config.env
source config.env
- Enable GCP services
gcloud services enable cloudbuild.googleapis.com storage-component.googleapis.com containerregistry.googleapis.com run.googleapis.com
- Build image to container registry
gcloud builds submit --tag "$CONTAINER_HOST/$PROJECT_ID/$IMAGE_NAME:$IMAGE_TAG" --ignore-file .gcloudignore
- Deploy service to cloud run
gcloud run deploy $SERVICE_NAME --image "$CONTAINER_HOST/$PROJECT_ID/$IMAGE_NAME:$IMAGE_TAG" --platform managed --port "$PORT" --memory "$MEMORY" --timeout="$TIMEOUT" --region="$REGION"
-
You can use
gcloud run deploy --help
to check allFLAGS
, see -
If you won't any one access this api, you should remove
--allow-unauthenticated
fromdeploy.sh
, see Authenticated
-
-
Define your "ENVIRONMENT VARIABLES" in
config.env
, for example:PROJECT_ID="GCP-PROJECT-ID" IMAGE_NAME="service-template" IMAGE_TAG="latest" CONTAINER_HOST="asia.gcr.io" SERVICE_NAME="service-template" REGION="asia-east1" PORT=8000 MEMORY="1Gi" TIMEOUT="2m"
-
Define your application (FastAPI) in
main.py
androuters/
-
Define the required modules in
requirements.txt
- Here an simple way to collect all modules used from
.py
Execute the following CLI incmd
,powershell
,wsl
,git bash
, ...pip install pipreqs
pipreqs ./
- Here an simple way to collect all modules used from
-
Check your
Dockerfile
-
Deploy your application: Execute the following CLI in
wsl
,git bash
, ..../deploy.sh
-
Clone the project using Git.
-
Open a terminal and navigate to the project directory.
-
Run the
./deploy.sh
script. -
Wait for the deployment to finish and get the Service URL, for example:
https://service-template-xxxxxxxxxx-xx.a.run.app
-
Use the
client.py
script to test your API, for example:import requests host = "Service URL" url = f"{host}/v1/recommend" request_data = { "page": "index", "data": {"ip_address": "e12345"} } res = requests.post(url, json=request_data) if res.status_code == 200: print(res.json()) else: print("Error: ", res.text)
{'code': '0', 'msg': 'success', 'ip_address': 'e12345'}
-
Authenticated is a security option in Cloud Run that controls who can access your application.
-
--no-allow-unauthenticated
: no one can access your api.-
How to grant authorization to specific members?
gcloud run services add-iam-policy-binding my-service --region='us-central1' --member='user:test-user@gmail.com' --role='roles/run.invoker'
-
-
--allow-unauthenticated
: any one can access your api
-