0

I am configuring a GCP Cloud Function via Terraform using:
https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/cloudfunctions_function

I'll be using source_archive_bucket and source_archive_object to have the function built from source code in a zip archive in a GCS bucket.

Reading the https://cloud.google.com/functions/docs/deploy and https://cloud.google.com/functions/docs/building docs it's not clear to me at what point a new version of the function is actually built and deployed.

Does it happen as soon as I push updated zip file to the GCS bucket? i.e. then next execution of the function will use the new image

Or can I first push an updated zip and then do a terraform apply to get the Cloud Function to pick up the new code?

If I had to guess I'd say the former, but it's not really spelled out explicitly anywhere AFAICT.

Anentropic
  • 32,188
  • 12
  • 99
  • 147

1 Answers1

0

From the best of my understaning, an appearance of a zip file in some bucket won't automatically trigger the deployment. One might need to use either a gcloud functions deploy ... - as described here - Deploy from Cloud Storage, or use Terraform with a terraform apply ... command. In the later case Terraform is to use an API for deployment.

Some time ago the Terraform scripts configuration and the deployment process was described here: How to deploy only modified Cloud Functions using Terraform

On top of that, the successful deployment means that when the new instance of a cloud function is created/launched, it is going to be created from the new code. Meanwhile, all exising/running (in memery) instances of the cloud function under discussion still use the old code. In some time all of them (old/runing cloud functions) are going to be shut down, and new instances (based on the new code) are going to be created instead.

al-dann
  • 2,545
  • 1
  • 12
  • 22
  • I guess I should clarify - in my case the updating of the zip file would happen before `terraform apply` is called (i.e. the cloudfunction tf resource gives path to zip in bucket, but the zip contents are not generated from a tf resource) ... it seemed likely to me that new version of Cloud Function would be generated when I push to the bucket. Is that not the case? – Anentropic Mar 08 '23 at 11:35
  • Or you are saying even though I pushed to the bucket I still have to do a "cloud functions deploy" via `terraform apply` for the new function version to happen? – Anentropic Mar 08 '23 at 11:36
  • From the best of my understaning, an appearance of a zip file in some bucket won't automatically trigger the deployment. One might need to use either `gcloud functions deploy` - as described here - https://cloud.google.com/functions/docs/deploy#from-cloud-storage, or use terraform with `terraform apply ... ` command (and the actual deployment is to be performed by Terraform as it uses inside some relevant API). – al-dann Mar 08 '23 at 11:43
  • There is no automatic deployment when you put file in Cloud Storage. But when you deploy with terraform, you have to package your code before deployment. The ZIP is a way to package your code. When you run your terraform apply, terraform will check if there is a new deployment to perform. How? By checking if the file name is different. If not, nothing is deployed, because it's the 'same thing'. If the file name is different a new deployment (and a new revision of Cloud Functions) is performed. For that, name your zip file differently and provide the new name as parameter of tyour terraform. – guillaume blaquiere Mar 08 '23 at 13:48