Serverless Deployment on Cloud Run using Terraform

Timothy
Timothy
Jan 23, 2020 · 4 min read
Image for post
Image for post

In this article, we’ll deploy a serverless Flask web application to Cloud Run by building its Docker image into Container Registry and use Terraform to provision our deployment as code.

Terraform is a infrastructure as code tool for building, changing, and versioning infrastructure safely and efficiently across various cloud providers.

Cloud Run is a managed compute platform that enables you to run stateless serverless containers that automatically scales.

Container Registry is a private container image registry that runs on Google Cloud.

Flask is a micro web framework written in Python.

Prerequisites

For this guide, I would stick to using Cloud Shell which has Terraform enabled by default.

If you are using the Cloud SDK on your local PC, you need a service account to use Terraform, do create one here.

Note that terraform-cr is my GCP project ID and you should replace that with yours.

Pushing Docker Image to Container Registry

We need to build the Docker image and push it to the project’s container registry so Terraform can access it.

You can clone the source codes into Cloud Shell and execute the following commands within it’s directory.

$ docker build -t gcr.io/terraform-cr/webapp .
$ docker push gcr.io/terraform-cr/webapp
Image for post
Image for post
Output

If this didn’t work for you, do check out other steps on the official documentation here.

Cloud Run will only retrieve containers hosted in Container Registry and not from other sources.

If your Docker Image is on Docker Hub, I made a short video that narrates how to push a Docker Image to Container Registry. Do subscribe 😉

Deploying Docker Images to Google Cloud Run

Deploying to Cloud Run using Terraform

Terraform is enabled on Cloud Shell, you can verify by executing the terraform -v command. The version of Terraform installed is v0.12.9.

Image for post
Image for post
Terraform version

If you want to find out more details on this section, do checkout Cloud Run - Terraform resource documentation.

Next step is to create a main.tf file which is a Terraform configuration file, you can find mine here:

# Filename: main.tf# Configure GCP project
provider "google" {
project = "terraform-cr"
}
# Deploy image to Cloud Run
resource "google_cloud_run_service" "mywebapp" {
name = "mywebapp"
location = "us-central1"
template {
spec {
containers {
image = "gcr.io/terraform-cr/webapp"
}
}
}
traffic {
percent = 100
latest_revision = true
}
}
# Create public access
data "google_iam_policy" "noauth" {
binding {
role = "roles/run.invoker"
members = [
"allUsers",
]
}
}
# Enable public access on Cloud Run service
resource "google_cloud_run_service_iam_policy" "noauth" {
location = google_cloud_run_service.mywebapp.location
project = google_cloud_run_service.mywebapp.project
service = google_cloud_run_service.mywebapp.name
policy_data = data.google_iam_policy.noauth.policy_data
}
# Return service URL
output "url" {
value = "${google_cloud_run_service.mywebapp.status[0].url}"
}

Now let’s initialize Terraform on Google Cloud Shell.

$ terraform init
Image for post
Image for post
Output

Then let’s plan the code in our main.tf file and see what the infrastructure looks like:

$ terraform plan
Image for post
Image for post
Truncated Output

Finally, we can apply to execute, make sure to enter ‘yes’ to approve.

$ terraform apply
Image for post
Image for post
Truncated Output
Outputs:url = https://mywebapp-mxxduub7tq-uc.a.run.app

Next Steps

Terraform allows you delete every resource used (including the GCP project), to do this, execute the following command, make sure to enter ‘yes’ to approve.

$ terraform destroy

So far we have been able to use Terraform to provision our deployment to Cloud Run as code. This could help you deploy applications much faster.

If you’re looking to automate more of your workflow on Google Cloud, the following resources would come in handy.

Google Cloud - Community

Google Cloud community articles and blogs

Thanks to Rosemary Wang and Ray Tsang

Timothy

Written by

Timothy

Software Engineer @DigitalMercurie I Google Developer Expert for Cloud | timtech4u.com.ng | http://fullstackgcp.com

Google Cloud - Community

A collection of technical articles and blogs published or curated by Google Cloud Developer Advocates. The views expressed are those of the authors and don't necessarily reflect those of Google.

Timothy

Written by

Timothy

Software Engineer @DigitalMercurie I Google Developer Expert for Cloud | timtech4u.com.ng | http://fullstackgcp.com

Google Cloud - Community

A collection of technical articles and blogs published or curated by Google Cloud Developer Advocates. The views expressed are those of the authors and don't necessarily reflect those of Google.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store