Deploy Cloud Functions on GCP with Terraform

In this tutorial you are going to deploy a simple Cloud Function triggered by a Cloud Storage event with Terraform

Axel Thevenot 🐣
Towards Data Science

--

Image by geralt | Pixabay

Cloud Functions are scalable β€œpay-as-you-go” Functions as a Service (FaaS) from Google Cloud Platform (GCP) to run your code with zero server management.

Cloud Functions can be written in Node.js, Python, Go, Java, .NET, Ruby, and PHP programming languages. For the sake of readability, this tutorial will contain a Cloud Function written in Python. But please feel free to choose your preferred programming language.

Terraform is an Infrastructure as Code (IaC) development tool that allows you to build, change, and version infrastructure safely, reproducibly, and efficiently. It codifies cloud APIs (GCP, AWS, Azure, …) into declarative configuration files.

At the end of this tutorial you will have a Cloud Function triggered as soon as a file is uploaded to a Google Cloud Storage bucket.

Prerequisites

To follow this tutorial you will need:

Then you can authenticate with GCP on your local machine running gcloud auth application-default login in your terminal.

Objectives

The objective of this tutorial is to use Terraform to deploy in a GCP project:

  • a bucket to upload files to.
  • a bucket to store the source code of the Cloud Function.
  • a Cloud Function that is triggered each time a file is uploaded to the first bucket and whose source code is in the second bucket.

Project Structure

You can create a new folder which I have named cloud_function_project but please feel free to choose a name that is convenient for you.

Then create the files as defined below. Leave them empty for now and you will complete them as the tutorial goes on.

.cloud_function_project/
β”‚
β”œβ”€β”€ terraform/
β”‚ β”‚
β”‚ β”œβ”€β”€ backend.tf
β”‚ β”œβ”€β”€ function.tf
β”‚ β”œβ”€β”€ main.tf
β”‚ β”œβ”€β”€ storage.tf
β”‚ └── variables.tf
β”‚
└── src/
β”‚
β”œβ”€β”€ main.py
└── requirements.txt

Before you start filing the different files, we will quickly go through the role of each of them.

The src folder contains the source code of the cloud function. It is a structure specific to Python.

  • main.py: source code of the Cloud Functions.
  • requirements.txt: the list of python libraries required by main.py to run. (you won’t need it in this tutorial)

The terraform folder contains the configuration files of the environment to deploy.

  • backend.tf: declaration of the Terraform backend.
  • main.tf: main declarations of the environment.
  • variables.tf: definition of the variables.
  • storage.tf: declaration of the Google Cloud Storage bucket.
  • function.tf: declaration of the Cloud Function.

Create the Cloud Function

Writing and running the Cloud Function is not the important part of this tutorial. You will deploy a function that will log some useful information about the file that has been uploaded into the bucket to track.

Note: this function does not have requirements needed so requirements.txt remains empty.
Note: this is a Python Cloud Function so you can change the code source according to the programming language you have chosen.

Create the Terraform Infrastructure

Backend

Start declaring your environment by specifying the Terraform backend. You can choose local which means that all the state files will be stored in the local directory. You could also choose a gcs backend but let’s keep it simple.

Variables

Declare the variables used in the Terraform files. You need to change the project_id variable according to the ID of the project in which you want to deploy the resources. Of course, you also can change the region.

Main

Declare the connection to the google provider.

Google Cloud Storage

Declare the two Google Cloud Storage buckets respectively to store the code of the Cloud Function and to upload files.

Note: The bucket names are prefixed with project_id because the buckets must have unique names.

Google Cloud Function

Last step: declare the Cloud Function. This requires compressing the source code into a zip file and uploading it to a bucket for storage. The source code can then be accessed when creating the Cloud Function with Terraform.

Note: The file is a bit long so feel free to review the comments to understand the logic.

Deploy the environment

Everything is ready to be deployed. Go to the root of the project in your terminal then to the root of the terraform folder.

$ cd ./terraform

Initialize your code to download the requirements mentioned in your code.

$ terraform init

Review changes.

$ terraform plan

Accept changes and apply them against real infrastructure.

$ terraform apply

Test your Cloud Function

To test everything works fine:

  • Open the Google Cloud Console and connect to your project.
  • Go to the Google Cloud Storage browser.
  • You can see the <YOUR-PROJECT-ID>-function and <YOUR-PROJECT-ID>-input buckets. Click on the bucket named <YOUR-PROJECT-ID>-input and upload any file into it to trigger the Cloud Function.
  • To verify it worked, go to your Cloud Functions list. A Cloud Function named function-trigger-on-gcs should be here. Click on its name and go to the LOGS tab to see it was triggered by the file you uploaded.

Knowledge is all about sharing.
Support me and get access of all my articles in one click here.

Going further

I hope you enjoyed this short tutorial to deploy a Cloud Function. Many improvements can be applied. You can:

  • set an IAM policy.
  • add variables and convert this code into a Terraform module to be reusable.
  • integrate the deployment into a Cloud Build process to have a continuous deployment.
  • a lot of more…

--

--