DEV Community

Cover image for Deploy Flask App on Google Cloud Run with Terraform
Jay Sheth
Jay Sheth

Posted on • Edited on

14 3 4 5 5

Deploy Flask App on Google Cloud Run with Terraform

Prerequisite:

  1. GCP Account
  2. Install Google Cloud SDK
  3. Terraform Installed on Developer Desktop
  4. Python on Developer Desktop

In GCP Console:

  1. Create new Project named my-first-project on GCP console
    GCP Console
    GCP Console

  2. We will enable the following APIs for our project to work, run the below mentioned commands in Google Cloud CLI

  • Cloud Run
  • Artifact Registry
  • Cloud Build

    GCP commands

On Developer Desktop:

  1. Create a simple Flask Application
  • Create an app.py file

    from 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)
    view raw app.py hosted with ❤ by GitHub
  • Create a Dockerfile

    # 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"]
    view raw Dockerfile hosted with ❤ by GitHub

In GCP Console:

  1. Go to Artifact Registry
  2. Click on Create Repository Artifact Registry
  • 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

  1. In the folder where we created the Dockerfile; run the following commands
    Docker commands

  2. We will create Terraform project

  • Create terraform.tf for configuring the providers

    terraform {
    required_providers {
    google = {
    source = "hashicorp/google"
    version = "5.37.0"
    }
    }
    }
    provider "google" {
    # Configuration options
    project = var.project_id
    region = var.location
    credentials = "keys.json"
    }
    view raw terraform.tf hosted with ❤ by GitHub
  • Create main.tf containing the cloud run configuration

    resource "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
    }
    view raw main.tf hosted with ❤ by GitHub
  • Create variable.tf containing the variables

    variable "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"
    }
    view raw variable.tf hosted with ❤ by GitHub
  • Create data.tf containing the data

    data "google_iam_policy" "noauth" {
    binding {
    role = "roles/run.invoker"
    members = [
    "allUsers",
    ]
    }
    }
    view raw data.tf hosted with ❤ by GitHub
  • Create output.tf containing the output

    output "url" {
    value = google_cloud_run_service.default.status[0].url
    }
    view raw output.tf hosted with ❤ by GitHub
  • Create terraform.tfvars containing the variable values

    project_id = "<PROJECT_ID>"
    location = "<REGION_ID>"
    docker_image = "<DOCKER_IMAGE>"
  • Now run the following commands in your terminal
    Terraform commands

Output

Flask application

Destroy Infrastructure

To destroy the infrastructure, run the following commands in your terminal
Terraform commands

👋👋BYE👋👋

Image of Timescale

🚀 pgai Vectorizer: SQLAlchemy and LiteLLM Make Vector Search Simple

We built pgai Vectorizer to simplify embedding management for AI applications—without needing a separate database or complex infrastructure. Since launch, developers have created over 3,000 vectorizers on Timescale Cloud, with many more self-hosted.

Read more →

Top comments (4)

Collapse
 
parth_sheth_5acb01208fb25 profile image
Parth Sheth

Very helpful and informative!

Collapse
 
sangya_lalwani_4346680c00 profile image
sangya lalwani

Crisp and perfect! Thanks for sharing this!

Collapse
 
amit_2343141ba488ca45ecd8 profile image
Amit

Gr8 content 👏

Collapse
 
deepa_1354a18fde08d profile image
Deepa

Helpful for beginners 💯

Billboard image

Try REST API Generation for MS SQL Server.

DreamFactory generates live REST APIs from database schemas with standardized endpoints for tables, views, and procedures in OpenAPI format. We support on-prem deployment with firewall security and include RBAC for secure, granular security controls.

See more!