google_cloudfunctions2_function not supported in terraform

Viewed 228

I am trying to provision a version 2 cloud function using terraform in gcp. The resource looks like this:

resource "google_cloudfunctions2_function" "function" {
  provider = google-beta
  name = "test-function"
  location = "us-central1"
  description = "a new function"

When I run plan I get this

 Error: Invalid resource type
│
│   on main.tf line 49, in resource "google_cloudfunctions2_function" "function":
│   49: resource "google_cloudfunctions2_function" "function" {
│
│ The provider hashicorp/google-beta does not support resource type "google_cloudfunctions2_function". Did you mean "google_cloudfunctions_function"?

Something I noticed. I can't see a currently existing version 2 cloud function using the gcloud cli:

me@cloudshell:~ (the-project-im-using)$ gcloud functions list
Listed 0 items.
me@cloudshell:~ (the-project-im-using)$ gcloud beta functions list
Listed 0 items.
me@cloudshell:~ (the-project-im-using)$ gcloud alpha functions list
Listed 0 items.
me@cloudshell:~ (the-project-im-using)$

Am I correct in assuming thaat this indicates the version 2 cloud functions are not accessible via rest api?

The reference docs I could find are here: https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/cloudfunctions2_function https://cloud.google.com/functions/docs/tutorials/terraform

Thanks!

2 Answers

I suspect the problem is related to the versions of Terraform and gcloud CLI that are currently running in your Cloud Shell.

With the release 395.0.0 of gcloud, the ability to list 2nd gen Cloud Functions was added:

Updated gcloud functions list to return 2nd gen functions in addition to 1st gen functions.

If you are running a lower version (mine had initially version 394) the 2nd gen functions won't be listed. Keep in mind you can still list the underlying Cloud Run services with gcloud run services list.

As for Terraform, my Cloud Shell had no problem deploying a 2nd gen function (the basic example from the documentation). My local machine has the latest versions of both packages and could also deploy without issue.

I suggest you deploy the function in your local machine with the latest versions of those packages. Otherwise, since Cloud Shell is managed and might reset to the default installation, you could also try customizing the container or waiting for the default installation to be updated. Let me know if this was useful.

I manage to fix it by explicitly forcing the beta tag, google-beta provider in the main.tf file

provider "google-beta" {
   project = local.project
}

and also adding it o every resource I used

resource "google_cloudfunctions2_function" "function" {
  provider = google-beta
  name = "function-v2"
  location = "us-central1"
  description = "a new function"
  ...
resource "google_storage_bucket" "bucket" {
  provider = google-beta
  name     = "${local.project}-gcf-source"  # Every bucket name must be globally unique
  location = "US"
  ...
Related