<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Volodymyr Viedienieiev on Medium]]></title>
        <description><![CDATA[Stories by Volodymyr Viedienieiev on Medium]]></description>
        <link>https://medium.com/@volodymyrviedienieiev?source=rss-fa44e07f41a5------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*-fk8Xr9L-MhNNSC7</url>
            <title>Stories by Volodymyr Viedienieiev on Medium</title>
            <link>https://medium.com/@volodymyrviedienieiev?source=rss-fa44e07f41a5------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 21 Nov 2026 07:00:06 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@volodymyrviedienieiev/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Scalable Cloud Run Deployments with Terraform: A Complete Guide]]></title>
            <link>https://medium.com/@volodymyrviedienieiev/cloud-functions-factory-automating-google-cloud-run-deployments-with-terraform-bbd022fce998?source=rss-fa44e07f41a5------2</link>
            <guid isPermaLink="false">https://medium.com/p/bbd022fce998</guid>
            <category><![CDATA[serverless]]></category>
            <category><![CDATA[terraform]]></category>
            <category><![CDATA[gcp]]></category>
            <category><![CDATA[cloud-computing]]></category>
            <category><![CDATA[devops]]></category>
            <dc:creator><![CDATA[Volodymyr Viedienieiev]]></dc:creator>
            <pubDate>Tue, 01 Apr 2025 18:52:49 GMT</pubDate>
            <atom:updated>2025-04-01T19:02:21.059Z</atom:updated>
            <content:encoded><![CDATA[<p>This guide outlines a scalable system for deploying multiple serverless functions to Google Cloud Run using Terraform. The approach is designed for teams managing growing infrastructure and seeking simplicity, automation, and full control over function lifecycle.</p><h3>Why Terraform and Cloud Run?</h3><p>Google Cloud Platform (GCP) is one of the leading platforms offering robust cloud services. As project scale increases, so does complexity, making effective management essential. Terraform serves as a key solution by enabling infrastructure to be defined and maintained as code. This approach facilitates automation, consistency, and control across cloud environments.</p><p>Terraform allows cloud infrastructure to be created, modified, and deleted in an automated and predictable manner. Configuration is defined as code, which supports version control, repeatability, and streamlined team collaboration. This reduces human error and aligns infrastructure management with modern software development practices.</p><h4>Terraform enables:</h4><ul><li><strong>Provision Compute Resources</strong> — Automate VM instances, instance groups, and GKE clusters.</li><li><strong>Configure Networking</strong> — Define VPCs, subnets, firewall rules, load balancers, and Cloud NAT.</li><li><strong>Manage IAM</strong> — Assign roles and permissions to users, service accounts, and resources.</li><li><strong>Deploy Storage and Databases</strong> — Automate setup of Cloud Storage, Cloud SQL, and BigQuery.</li><li><strong>Automate Multi-Environment Setups</strong> — Replicate dev, staging, and production environments consistently.</li></ul><h3>Cloud Run and Function Management</h3><p>Deployment of Cloud Run services can be automated with Terraform. This includes defining and managing serverless functions through infrastructure-as-code. An example is available in the <a href="https://cloud.google.com/functions/docs/samples/functions-v2-basic">official Cloud Functions v2 documentation</a>.</p><p>As the number of Cloud Functions grows, a structured system becomes necessary for simplified management. A scalable and maintainable configuration ensures consistency and reduces operational overhead.</p><p>This guide demonstrates how such a system can be implemented using Terraform with minimal complexity.</p><h3>Step 1: Initialize the Project</h3><p>Begin by establishing a minimal Terraform project structure and configuring the GCP provider. This includes setting up provider requirements and defining project-wide variables.</p><p>Follow <a href="https://developer.hashicorp.com/terraform/tutorials/gcp-get-started/infrastructure-as-code">Terraform’s GCP</a> guide for full setup.</p><h4>Project Structure:</h4><pre>terraform/<br>├── main.tf<br>└── variables.tf</pre><p>The <strong>variables.tf</strong> file contains the essential variables used at the beginning of the project initialization. These variables define core configuration settings such as the GCP project ID, region, and zone:</p><pre>variable &quot;project_id&quot; {<br>  type    = string<br>  default = &quot;your-project-name&quot;<br>}<br><br>variable &quot;region&quot; {<br>  type    = string<br>  default = &quot;us-central1&quot;<br>}<br><br>variable &quot;zone&quot; {<br>  type    = string<br>  default = &quot;us-central1-c&quot;<br>}</pre><p>The <strong>main.tf</strong> file contains the core configuration required to initialize the project. It specifies the necessary provider settings and links them to the previously defined variables:</p><pre>terraform {<br>  required_providers {<br>    google = {<br>      source  = &quot;hashicorp/google&quot;<br>      version = &quot;6.8.0&quot;<br>    }<br>  }<br>}<br><br>provider &quot;google&quot; {<br>  project = var.project_id<br>  region  = var.region<br>  zone    = var.zone<br>}</pre><h3>Step 2: Create Function Source Code</h3><p>Function code must be created and organized. Each function resides in its own subfolder under a dedicated functions/ directory.</p><p>For this example project, two simple functions are created: <strong>test1 </strong>and <strong>test2</strong>.</p><h4>main.py (test1)</h4><pre>def main(event):<br>    return &quot;hello world!&quot;</pre><h4>main.py (test2)</h4><pre>def main(event):<br>    return &quot;Hello another world!&quot;</pre><p>At this stage, the project structure looks like this:</p><pre>terraform/<br>├── functions/<br>│   ├── test1/<br>│   │   └── main.py<br>│   └── test2/<br>│       └── main.py<br>├── main.tf<br>└── variables.tf</pre><h3>Step 3: Define the Function Template</h3><p>At this stage, a template needs to be created to define the configuration required for each function to work properly. This template will act as the base structure for deploying Cloud Run services using Terraform.</p><p>To begin, add the following code to <strong>variables.tf</strong> to define the necessary inputs:</p><pre>variable &quot;http_functions&quot; {<br>  type = list(object({<br>    name         = string<br>    description  = string<br>    entry_point  = string<br>    runtime      = string<br>    source_dir   = string<br>  }))<br>}</pre><p>This code defines a variable called http_functions, which holds a list of the configs of the Cloud Run functions to be deployed. Each function in the list must include the fields mentioned earlier: <em>name</em>, <em>description</em>, <em>source_dir</em>, <em>entry_point</em>, and <em>runtime</em>.</p><h3>Step 4: <strong>Adding Functions</strong></h3><p>After creating the template, the next step is to define the actual list of functions and store them in a variable. This is done by creating a <strong>terraform.tfvars</strong> file and adding the following configuration:</p><pre>http_functions = [<br>    {<br>        name         = &quot;test1&quot;<br>        description  = &quot;first testing function&quot;<br>        entry_point  = &quot;main&quot;<br>        runtime      = &quot;python311&quot;<br>        source_dir   = &quot;functions/test1&quot;<br>    },<br>        {<br>        name         = &quot;test2&quot;<br>        description  = &quot;second testing function&quot;<br>        entry_point  = &quot;main&quot;<br>        runtime      = &quot;python311&quot;<br>        source_dir   = &quot;functions/test2&quot;<br>    }<br>]</pre><p>Function definitions are stored in terraform.tfvars, which is Terraform’s default file for providing variable values.</p><p>If a different filename is used for storing variable values, it must be explicitly specified when running Terraform commands, for example:</p><pre>terraform apply -var-file=&quot;custom.tfvars&quot;</pre><p>At this stage, the project structure should look like this:</p><pre>terraform/<br>├── functions/<br>│   ├── test1/<br>│   │   └── main.py<br>│   └── test2/<br>│       └── main.py<br>├── main.tf<br>├── terraform.tfvars<br>└── variables.tf</pre><h3>Step 5: Archive and Upload Functions</h3><p>At this point, the functions and their configurations are defined, but the logic for deploying them is still missing. To handle multiple functions dynamically, an additional file will be created — <strong>http_function.tf</strong>.</p><p>This file will contain the logic that iterates over the list of functions and provisions them accordingly.</p><p>The updated project structure now looks like this:</p><pre>terraform/<br>├── functions/<br>│   ├── test1/<br>│   │   └── main.py<br>│   └── test2/<br>│       └── main.py<br>├── http_function.tf<br>├── main.tf<br>├── terraform.tfvars<br>└── variables.tf</pre><h4>Create a Cloud Storage Bucket</h4><p>Before setting up the logic for the functions it is needed to provision a Google Cloud Storage bucket to store the archived function source code. This bucket serves as a centralized location for storing .zip archives prior to deployment to Cloud Run.</p><pre>resource &quot;google_storage_bucket&quot; &quot;functions_bucket&quot; {<br>  name     = &quot;${var.project_id}-gcf-source-bucket&quot;<br>  location = &quot;US&quot;<br>}</pre><h4>Archive Function Code</h4><p>This <strong>archive_file </strong>data block creates a ZIP archive for each function listed in the <strong>http_functions </strong>variable. These archives will later be uploaded to the configured Cloud Storage bucket as part of the deployment process.</p><pre>data &quot;archive_file&quot; &quot;function_archive&quot; {<br>  for_each = { for f in var.http_functions : f.name =&gt; f }<br><br>  type        = &quot;zip&quot;<br>  source_dir  = each.value.source_dir<br>  output_path = &quot;${path.module}/.terraform/tmp/${each.key}.zip&quot;<br>}</pre><p>In this block, each function’s code is compressed into a .zip file and stored locally in the <strong>.terraform/tmp</strong> directory.</p><h4>Upload to GCS Bucket</h4><p>The next step involves transferring the ZIP archives from the local machine to the designated Cloud Storage bucket in GCP. This ensures that the function source code is accessible to Google Cloud Run during deployment.</p><pre>resource &quot;google_storage_bucket_object&quot; &quot;function_sources&quot; {<br>  for_each = { for f in var.http_functions : f.name =&gt; f }<br><br>  name   = &quot;${each.value.name}-${data.archive_file.function_archive[each.key].output_md5}.zip&quot;<br>  bucket = google_storage_bucket.functions_bucket.name<br>  source = data.archive_file.function_archive[each.key].output_path<br>}</pre><p>Note that a hash is appended to the object name when uploading the function archive. This ensures that Google Cloud Functions detect changes in the function code and trigger an update during redeployment. By default, such updates are not applied automatically unless a change in the source archive is explicitly recognized.</p><h3>Step 6: Deploy Functions to Cloud Run</h3><p>Each Cloud Run service is provisioned based on the archived function code.</p><pre>resource &quot;google_cloudfunctions2_function&quot; &quot;http_functions&quot; {<br>  for_each = { for f in var.http_functions : f.name =&gt; f }<br><br>  name        = each.value.name<br>  location    = var.region<br>  description = each.value.description<br><br>  build_config {<br>    runtime     = each.value.runtime<br>    entry_point = each.value.entry_point<br>    source {<br>      storage_source {<br>        bucket = google_storage_bucket.functions_bucket.name<br>        object = google_storage_bucket_object.function_sources[each.key].name<br>      }<br>    }<br>  }<br><br>  service_config {<br>    available_memory = &quot;256M&quot;<br>    timeout_seconds  = 60<br>  }<br><br>  depends_on = [google_storage_bucket_object.function_sources]<br>}</pre><p>Once applied, the functions will appear in the Google Cloud Console.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/717/0*OZuLcBYLdAuOpvwa" /></figure><h3>Step 7: Assign IAM Permissions</h3><p>Public access is configured using IAM bindings.</p><pre>resource &quot;google_cloudfunctions2_function_iam_member&quot; &quot;invoker&quot; {<br>  for_each = { for f in var.http_functions : f.name =&gt; f }<br><br>  project        = var.project_id<br>  location       = var.region<br>  cloud_function = google_cloudfunctions2_function.http_functions[each.key].name<br><br>  role   = &quot;roles/cloudfunctions.invoker&quot;<br>  member = &quot;allUsers&quot;<br>}<br><br>resource &quot;google_cloud_run_service_iam_member&quot; &quot;public_invoker&quot; {<br>  for_each = { for f in var.http_functions : f.name =&gt; f }<br><br>  location = var.region<br>  project  = var.project_id<br>  service  = google_cloudfunctions2_function.http_functions[each.key].name<br><br>  role   = &quot;roles/run.invoker&quot;<br>  member = &quot;allUsers&quot;<br>}</pre><p>This configuration allows public HTTP access to the deployed functions.</p><p>The complete configuration for <strong>http_functions.tf</strong> should now look like this:</p><pre>resource &quot;google_storage_bucket&quot; &quot;functions_bucket&quot; {<br>  name     = &quot;${var.project_id}-gcf-source-bucket&quot;<br>  location = &quot;US&quot;<br>}<br><br><br>data &quot;archive_file&quot; &quot;function_archive&quot; {<br>  for_each = { for f in var.http_functions: f.name =&gt; f}<br><br><br>  type        = &quot;zip&quot;<br>  source_dir  = &quot;${each.value.source_dir}&quot;<br>  output_path = &quot;${path.module}/.terraform/tmp/${each.key}.zip&quot;<br>}<br><br><br>resource &quot;google_storage_bucket_object&quot; &quot;function_sources&quot; {<br>  for_each = { for f in var.http_functions : f.name =&gt; f }<br><br><br>  name   = &quot;${each.value.name}-${data.archive_file.function_archive[each.key].output_md5}.zip&quot;<br>  bucket = google_storage_bucket.functions_bucket.name<br>  source = &quot;${path.module}/.terraform/tmp/${each.key}.zip&quot;<br>}<br><br><br>resource &quot;google_cloudfunctions2_function&quot; &quot;http_functions&quot; {<br>  for_each = { for f in var.http_functions : f.name =&gt; f }<br><br><br>  name        = each.value.name<br>  location    = var.region<br>  description = each.value.description<br><br><br>  build_config {<br>    runtime     = each.value.runtime<br>    entry_point = each.value.entry_point<br>    source {<br>      storage_source {<br>        bucket = google_storage_bucket.functions_bucket.name<br>        object = &quot;${each.value.name}-${data.archive_file.function_archive[each.key].output_md5}.zip&quot;<br>      }<br>    }<br>  }<br><br><br>  service_config {<br>    available_memory = &quot;256M&quot;<br>    timeout_seconds  = 60<br>  }<br><br><br>  depends_on = [google_storage_bucket_object.function_sources]<br>}<br><br><br>resource &quot;google_cloudfunctions2_function_iam_member&quot; &quot;invoker&quot; {<br>  for_each = { for f in var.http_functions : f.name =&gt; f }<br><br><br>  project        = var.project_id<br>  location       = var.region<br>  cloud_function = google_cloudfunctions2_function.http_functions[each.key].name<br><br><br>  role   = &quot;roles/cloudfunctions.invoker&quot;<br>  member = &quot;allUsers&quot;<br>}<br><br><br>resource &quot;google_cloud_run_service_iam_member&quot; &quot;public_invoker&quot; {<br>  for_each = { for f in var.http_functions : f.name =&gt; f }<br><br><br>  location = &quot;us-central1&quot;<br>  project  = var.project_id<br>  service  = google_cloudfunctions2_function.http_functions[each.key].name<br><br><br>  role   = &quot;roles/run.invoker&quot;<br>  member = &quot;allUsers&quot;<br>}</pre><h3>Final Thoughts</h3><p>With this system in place, adding new functions requires only a new folder and an entry in terraform.tfvars. Terraform takes care of the rest — archiving, uploading, deploying, and managing permissions. This infrastructure-as-code approach ensures consistency, reliability, and efficiency in managing serverless workloads on GCP.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=bbd022fce998" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>