ds_Abc
ds_Abc

Reputation: 183

How to invoke a Google Cloud Function using Terraform

I am deploying a Google cloud function(written in Python) 2nd gen using Terraform. I want to automatically invoke/run this cloud function once immediately after deployment, but couldn’t find any way to do it. So is there a way to invoke the deployed cloud function using Terraform script? Thanks.

Upvotes: 0

Views: 431

Answers (2)

Filip
Filip

Reputation: 293

On Terraform 1.4 and later, use the terraform_data resource type instead of null_resource. You can use the terraform_data resource without requiring or configuring a provider.

resource "terraform_data" "invoke_gcf" {
  provisioner "local-exec" {
    command = "gcloud functions call ${google_cloudfunctions2_function.GCF_RSOURCE_NAME.name} --region=REGION --gen2 --data '{"key":"value"}'"
  }
}

If you want to invoke a function on every terraform apply you can use the triggers_replace argument with a value that would change every time.

But consider using another step in your CI/CD pipeline which might be a more suitable way to invoke Cloud Function.

Upvotes: 0

ha36d
ha36d

Reputation: 1127

you can use null_resource to execute it. Make sure that gcloud is installed and authenticated in the local machine. It would be good if you use the depends_on.

resource "null_resource" "invoke" {

  provisioner "local-exec" {
    command = "gcloud functions call YOUR_FUNCTION_NAME --region=REGION --gen2 --data '{"variable":"value"}'"
  }
depends_on = [google_cloudfunctions_function.function]
}

Upvotes: 1

Related Questions