1

I have a Terraform project that allows to create multiple cloud functions. I know that if I change the name of the google_storage_bucket_object related to the function itself, terraform will see the difference of the zip name and redeploy the cloud function.

My question is, there is a way to obtain the same behaviour, but only with the cloud functions that have been changed?

resource "google_storage_bucket_object" "zip_file" {
  # Append file MD5 to force bucket to be recreated
  name   = "${local.filename}#${data.archive_file.source.output_md5}"
  bucket = var.bucket.name
  source = data.archive_file.source.output_path
}

# Create Java Cloud Function
resource "google_cloudfunctions_function" "java_function" {
  name    = var.function_name
  runtime = var.runtime

  available_memory_mb   = var.memory
  source_archive_bucket = var.bucket.name
  source_archive_object = google_storage_bucket_object.zip_file.name
  timeout               = 120
  entry_point           = var.function_entry_point

  event_trigger {
    event_type = var.event_trigger.event_type
    resource   = var.event_trigger.resource
  }

  environment_variables = {
    PROJECT_ID = var.env_project_id
    SECRET_MAIL_PASSWORD = var.env_mail_password
  }

  timeouts {
    create = "60m"
  }
}

By appending MD5 every cloud functions will result in a different zip file name, so terraform will re-deploy every of them and I found that without the MD5, Terraform will not see any changes to deploy. If I have changed some code only inside a function, how can I tell to Terraform to re-deploy only it (so for example to change only its zip file name)?

I hope my question is clear and I want to thank you everyone who tries to help me!

GiuxG97
  • 11
  • 1
  • I'm guessing (based on my experience with AWS) that you need only to update the function based on the change in the source argument. So, the name should stay the same across deploys, but the `source` should probably be equal to something like `source = data.archive_file.source.output_base64sha256`. – Marko E Aug 06 '22 at 14:35
  • If I understand your question - you might like to get something like this: https://stackoverflow.com/questions/68488277/how-can-i-deploy-google-cloud-functions-in-ci-cd-without-re-deploying-unchanged/68488770#68488770 – al-dann Aug 06 '22 at 18:36
  • Thank you both, now I understand better the terraform behaviour. I find out that my cloud functions, for which I want to rebuild only if some code changed, are inside the same java folder because there are lots of services that are usefull for every of them, so I think that with this solution I can't have the possibility to let terraform to rebuild only a cloud function because the java folder path is the same for every one. Or every cloud functions will be re-deployed (by using md5 or sha in the name) or no one I suppose at this point.. Tell me maybe if I understood correctly – GiuxG97 Aug 07 '22 at 11:08
  • This is a good first question. Thank you for taking the time on that. – Matthew Schuchard Aug 07 '22 at 11:31

0 Answers0