Task: Setup Cloud Run with Terraform
- Google Cloud Platform account
- Google Cloud SDK
- Terraform
- Create a new project in Google Cloud Platform
- Enable Cloud Run API, Cloud Build API, Cloud Run Admin API, Cloud Build Service Account
- Create a service account and download the key
- Download GCP CLI and authenticate
- Run terraform
main.tf
- Terraform configuration file- main.py - Python Flask application file
- Dockerfile - Docker configuration file
- requirements.txt - Python dependencies file
- hello-world.tar - Docker image tar file
- .gitignore - Git ignore file
- .terraform - Terraform directory
- terraform.tfstate - Terraform state file
- terraform.tfstate.backup - Terraform state backup file
- credentials.json - Service account key file
- README.md - Readme file
- Go to the Google Cloud Console
- Click on
Select a project
and thenNew Project
- Enter a project name and click
Create
- Go to the API Library
- Search for
Cloud Run API
and clickEnable
- Search for
Cloud Build API
and clickEnable
- Search for
Cloud Run Admin API
and clickEnable
- Search for
Cloud Build Service Account
and clickEnable
- Go to the Service Accounts
- Click on
Create Service Account
- Enter a name and click
Create
- Select the role
Cloud Run Admin
and clickContinue
- Click
Done
- Click on the service account you just created
- Click on
Add Key
and selectJSON
- Click
Create
and download the key
- Download the Google Cloud SDK
- Authenticate with
gcloud auth login
- Clone this repository
- Run
terraform init
- Build the Docker image with the following command:
docker build -t us-central1-docker.pkg.dev/${PROJECT_ID}/hello-world-repo/hello-world .
- Push the Docker image to the container registry with the following command:
docker push us-central1-docker.pkg.dev/${PROJECT_ID}/hello-world-repo/hello-world
- Build another Docker image with a different tag using the following command:
docker build -t gcr.io/${PROJECT_ID}/hello-world:latest .
- Push the new Docker image to the container registry with the following command:
docker push gcr.io/${PROJECT_ID}/hello-world:latest
- Save the Docker image to a tar file with the following command:
docker save -o hello-world.tar gcr.io/${PROJECT_ID}/hello-world:latest
- Load the Docker image from the tar file with the following command:
docker load -i hello-world.tar
- Run the Docker container with the following command:
docker run -p 8080:8080 gcr.io/${PROJECT_ID}/hello-world:latest
- Test the Docker container by sending a request to
http://localhost:8080
- Terraform apply
terraform apply
- Delete the project in the Google Cloud Console
- Cloud Run
- Terraform
- Google Cloud SDK
- Google Cloud Console
- Google Cloud API Library
- Google Cloud Service Accounts
- Google Cloud SDK Installation
- Google Cloud SDK Authentication
- Terraform Google Cloud Provider
- Terraform Google Cloud Run
- Terraform Google Cloud Build
- Terraform Google Cloud Service Account
- Terraform Google Cloud IAM Binding
- Terraform Google Cloud IAM Member
- Terraform Google Cloud Project
- Terraform Google Cloud Project Service
- Terraform Google Cloud Project IAM Policy
- Terraform Google Cloud Project Service Identity
- Terraform Google Cloud Project Service Identity Pool