Prerequisite:
- GCP Account
- Install Google Cloud SDK
- Terraform Installed on Developer Desktop
- Python on Developer Desktop
In GCP Console:
We will enable the following APIs for our project to work, run the below mentioned commands in Google Cloud CLI
On Developer Desktop:
- Create a simple Flask Application
-
Create an
app.py
file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersfrom flask import Flask app = Flask(__name__) @app.route('/') def hello_world(): return 'Hello, From Jay!' if __name__ == '__main__': app.run(host='0.0.0.0', port=8080) -
Create a
Dockerfile
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters# Use the official Python image from the Docker Hub FROM python:3.8-slim # Set the working directory in the container WORKDIR /app # Copy the current directory contents into the container at /app COPY . /app # Install the required dependencies RUN pip install Flask # Make port 8080 available to the world outside this container EXPOSE 8080 # Define environment variable ENV NAME World # Run app.py when the container launches CMD ["python", "app.py"]
In GCP Console:
- Go to Artifact Registry
- Click on Create Repository
- Give name to repository (
$REPO_NAME
) - Select Docker in Format
- Select Standard in Mode
- Select Regional in Location Type
- Select us-west1 in Region
- Click on Create
On Developer Desktop
In the folder where we created the
Dockerfile
; run the following commands
We will create Terraform project
-
Create
terraform.tf
for configuring the providers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersterraform { required_providers { google = { source = "hashicorp/google" version = "5.37.0" } } } provider "google" { # Configuration options project = var.project_id region = var.location credentials = "keys.json" } -
Create
main.tf
containing the cloud run configuration
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersresource "google_cloud_run_service" "default" { name = "cloudrun-aotask-srv" location = var.location template { spec { containers { image = var.docker_image } } } traffic { percent = 100 latest_revision = true } } resource "google_cloud_run_service_iam_policy" "noauth" { location = google_cloud_run_service.default.location project = google_cloud_run_service.default.project service = google_cloud_run_service.default.name policy_data = data.google_iam_policy.noauth.policy_data } -
Create
variable.tf
containing the variables
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersvariable "project_id" { description = "The ID of the GCP project" } variable "location" { description = "The region to deploy resources" } variable "docker_image" { description = "The Docker image to deploy" } -
Create
data.tf
containing the data
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersdata "google_iam_policy" "noauth" { binding { role = "roles/run.invoker" members = [ "allUsers", ] } } -
Create
output.tf
containing the output
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersoutput "url" { value = google_cloud_run_service.default.status[0].url } -
Create
terraform.tfvars
containing the variable values
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersproject_id = "<PROJECT_ID>" location = "<REGION_ID>" docker_image = "<DOCKER_IMAGE>"
Output
Destroy Infrastructure
To destroy the infrastructure, run the following commands in your terminal
👋👋BYE👋👋
Top comments (4)
Very helpful and informative!
Crisp and perfect! Thanks for sharing this!
Gr8 content 👏
Helpful for beginners 💯