Introduction:
If you’re a developer who’s looking to deploy containerized applications quickly and easily, you may have heard of Google Cloud Run. I recently had the opportunity to work with Cloud Run and was so impressed by its scalability, portability, and ease of deployment that I decided to create a Terraform Cloud Run module.
cloud-run-conf.tf
variables.tf
The cloud-run-conf.tf file is a Terraform configuration file that defines the configuration for a Cloud Run service. At the beginning of the file, local variables are defined to store values that will be used later in the file. These variables can be customized to fit the needs of the user. The file also includes a call to a Cloud Run module that has been previously created. In this case, the path to the module is specified as “./module” .This module is used to define the attributes of the Cloud Run service, such as the container image to be deployed, the maximum number of instances to run, the port to listen on etc. Values for these attributes are passed to the module using the var parameter. For example, to set the container image for the Cloud Run service, the container_image variable is passed
to the module with the desired value. Similarly, other attributes can be customized by passing appropriate values to the corresponding variables. Overall, the cloud-run-conf.tf file provides a convenient way to define the configuration for a Cloud Run service using Terraform, with the flexibility to customize the service as needed.
The variables.tf file defines the variables that will be used in the Terraform configuration for a Cloud Run service. One of the variables defined in this file is called cloud_run. This variable has a type of object and contains several attributes that define the configuration for the Cloud Run service.
main.tf
folder structure
The main.tf file is a Terraform configuration file that defines the resources for a Cloud Run service. In this file, a Cloud Run service is created using the google_cloud_run_service. After creating the Cloud Run service, an IAM binding is created using the google_cloud_run_service_iam_binding resource type. This resource type is used to assign an IAM role to the Cloud Run service account, which controls access to the service.
For this project, a custom Docker image is being used to deploy a simple website to a Cloud Run service. This Docker image contains all of the necessary code and dependencies to run the website. In addition to using a custom Docker image, a custom domain can be mapped to the Cloud Run service URL. This can be done by creating a Cloud DNS zone or use your prefered DNS Provider and adding a CNAME record that points to the Cloud Run service URL. This allows the website to be accessed using the custom domain instead of the default Cloud Run URL.