Deploy Cloud Function 2nd gen with HTTP trigger using Terraform

Full terraform config to deploy a Cloud Function 2nd gen with resources

Explore further

For detailed documentation that includes this code sample, see the following:

Code sample

Terraform

To learn how to apply or remove a Terraform configuration, see Basic Terraform commands. For more information, see the Terraform provider reference documentation.

resource "google_storage_bucket" "bucket" {
  provider = google-beta
  name     = "cloudfunctions2-function-bucket-${local.name_suffix}"  # Every bucket name must be globally unique
  location = "US"
  uniform_bucket_level_access = true
}

resource "google_storage_bucket_object" "object" {
  provider = google-beta
  name   = "function-source.zip"
  bucket = google_storage_bucket.bucket.name
  source = "path/to/index.zip-${local.name_suffix}"  # Add path to the zipped function source code
}

resource "google_cloudfunctions2_function" "terraform-test2" {
  provider = google-beta
  name = "test-function-${local.name_suffix}"
  location = "us-central1"
  description = "a new function"

  build_config {
    runtime = "nodejs16"
    entry_point = "helloHttp"  # Set the entry point 
    source {
      storage_source {
        bucket = google_storage_bucket.bucket.name
        object = google_storage_bucket_object.object.name
      }
    }
  }

  service_config {
    max_instance_count  = 1
    available_memory    = "256M"
    timeout_seconds     = 60
  }
}

What's next

To search and filter code samples for other Google Cloud products, see the Google Cloud sample browser.