Cloud Run Example
This example demonstrates how to deploy a Go Fiber application to Google Cloud Run.
Description
This project provides a starting point for deploying a Go Fiber application to Google Cloud Run. It includes necessary configuration files and scripts to build and deploy the application using Docker and Google Cloud Build.
Requirements
- Go 1.18 or higher
- Docker
- Google Cloud SDK
- Git
GCP Prerequisites
Before deploying, ensure the following GCP APIs are enabled and IAM roles are granted:
Enable APIs:
gcloud services enable run.googleapis.com \
cloudbuild.googleapis.com \
containerregistry.googleapis.com
IAM roles required for the Cloud Build service account ([PROJECT_NUMBER]@cloudbuild.gserviceaccount.com):
roles/run.admin— deploy Cloud Run servicesroles/iam.serviceAccountUser— act as the Cloud Run runtime service accountroles/storage.admin— push images to Container Registry
Setup
-
Clone the repository:
git clone https://github.com/gofiber/recipes.gitcd recipes/cloud-run -
Install the dependencies:
go mod download -
Build the Docker image:
docker build -t cloud-run-example . -
Run the Docker container:
docker run -p 3000:3000 cloud-run-example
The application should now be running on http://localhost:3000.
PORT environment variable: Cloud Run injects the
PORTenv variable at runtime. The application readsPORTand falls back to3000if unset, so it works both locally and on Cloud Run without any code changes.
Deploy to Google Cloud Run
-
Set up Google Cloud SDK and authenticate:
gcloud auth logingcloud config set project [YOUR_PROJECT_ID] -
Build and push the Docker image using Google Cloud Build:
gcloud builds submit --tag gcr.io/[YOUR_PROJECT_ID]/cloud-run-example -
Deploy the image to Cloud Run:
gcloud run deploy cloud-run-example --image gcr.io/[YOUR_PROJECT_ID]/cloud-run-example --platform managed --region [YOUR_REGION] --allow-unauthenticatedNote:
--allow-unauthenticatedmakes the service publicly accessible. Remove this flag in production and use IAM-based access control instead.
Replace [YOUR_PROJECT_ID] and [YOUR_REGION] with your Google Cloud project ID and desired region.
Cloud Build Configuration
The cloudbuild.yaml file defines the steps to build and deploy the application using Google Cloud Build:
steps:
- name: 'gcr.io/kaniko-project/executor:latest'
id: 'build-and-push'
args:
- '--destination=asia.gcr.io/$PROJECT_ID/$_SERVICE_NAME:$SHORT_SHA'
- '--destination=asia.gcr.io/$PROJECT_ID/$_SERVICE_NAME:latest'
- '--dockerfile=Dockerfile'
- '--context=.'
- '--cache=true'
- '--cache-ttl=120h'
- id: 'Deploy to Cloud Run'
name: 'gcr.io/cloud-builders/gcloud'
entrypoint: 'bash'
args:
- '-c'
- |
gcloud run deploy $_SERVICE_NAME \
--image=asia.gcr.io/$PROJECT_ID/$_SERVICE_NAME:$SHORT_SHA \
--region=$_REGION --platform managed \
--allow-unauthenticated \
--port=3000
# NOTE: --allow-unauthenticated is for demo purposes only.
# Remove this flag in production and use IAM-based access control instead.
options:
substitutionOption: ALLOW_LOOSE
substitutions:
_SERVICE_NAME: cloud-run-example
_REGION: asia-southeast1
Example Usage
-
Open your browser and navigate to the Cloud Run service URL provided after deployment.
-
You should see the message:
Hello, World!.
Conclusion
This example provides a basic setup for deploying a Go Fiber application to Google Cloud Run. It can be extended and customized further to fit the needs of more complex applications.