Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cloud function fails to update when migrated from gcloud #7921

Closed
afafara opened this issue Dec 2, 2020 · 6 comments · Fixed by GoogleCloudPlatform/magic-modules#4292, hashicorp/terraform-provider-google-beta#2780 or #8010
Assignees
Labels

Comments

@afafara
Copy link

afafara commented Dec 2, 2020

Community Note

  • Please vote on this issue by adding a 👍 reaction to the original issue to help the community and maintainers prioritize this request.
  • Please do not leave +1 or me too comments, they generate extra noise for issue followers and do not help prioritize the request.
  • If you are interested in working on this issue or have submitted a pull request, please leave a comment.
  • If an issue is assigned to the modular-magician user, it is either in the process of being autogenerated, or is planned to be autogenerated soon. If an issue is assigned to a user, that user is claiming responsibility for the issue. If an issue is assigned to hashibot, a community member has claimed the issue already.

Terraform Version

Terraform v0.13.5

  • provider registry.terraform.io/hashicorp/google v3.49.0

Affected Resource(s)

  • google_cloudfunctions_function

Terraform Configuration Files

resource "google_cloudfunctions_function" "function" {
  name        = "function-test"
  runtime     = "nodejs10"
  region      = "europe-west1"

  source_archive_bucket = "some-bucket"
  source_archive_object = "some-object.zip"

  trigger_http          = true
  entry_point           = "helloWorld"
}

Expected Behavior

Terraform updates cloud function.

Actual Behavior

Terraform fails with error:

Error: Error while updating cloudfunction configuration: googleapi: Error 400: Invalid value at 'function' (oneof), oneof field 'source_code' is already set. Cannot set 'sourceUploadUrl', invalid

Steps to Reproduce

  1. Create cloud function with gcloud and point to local folder as source:
    gcloud functions deploy function-test --region europe-west1 --entry-point helloWorld --trigger-http --runtime nodejs10 --source function-source
  2. Terraform import the cloud function:
    terraform import google_cloudfunctions_function.function some-project/europe-west1/function-test
  3. terraform apply with above config.

Important Factoids

Upgrading function that was created with gcloud using local source, importing and applying with changing source to bucket object fails, because patch method tries to set both sourceArchiveUrl (GCS object) and sourceUploadUrl (local upload).
This was introduced with PR #7723, "read before patch".

References

@ghost ghost added the bug label Dec 2, 2020
@edwardmedia edwardmedia self-assigned this Dec 2, 2020
@edwardmedia
Copy link
Contributor

@afafara Can you share the imported state, and specify what did you change before apply?

@afafara
Copy link
Author

afafara commented Dec 3, 2020

@edwardmedia debug log and statefile: https://gist.github.com/afafara/2aa85a4bc65efe7088c5fc3d4052e537

Steps:

  • create a simple function through gcloud functions deploy with --source param pointing to local folder with code (that deployed function with sourceUploadUrl parameter)
  • run terraform import
  • run terraform apply

According to API reference it can be either sourceUploadUrl, sourceArchiveUrl or sourceRepository - but it looks like Terraform tries to set both archive and upload urls.

@ghost ghost removed waiting-response labels Dec 3, 2020
@edwardmedia
Copy link
Contributor

@afafara Looked at the state, it seems sourceUploadUrl has already been set after you ran the gcloud function deploy .... In the Terraform config, you are adding more source(s) which might trigger the exception against the cloudfunction rule. This is not error from Terraform. You might need to play around with the settings.

@afafara
Copy link
Author

afafara commented Dec 7, 2020

@edwardmedia gcloud function deploy with local source code does that - it zips the content and sets sourceUploadUrl - nothing else was done with cloud function except these 3 steps (gcloud deploy, terraform import, terraform apply).

Terraform Google provider currently does not use that method (see #3085), and sourceUploadUrl is not exposed as parameter (nor can it be changed to something else), it's also not visible in statefile - that means importing Cloud Function created with gcloud function deploy --source . and changing its source code to either source repo or storage bucket fails.
This type of change works through both API and Console, and used to work with Terraform before #7723 change.

@slevenick
Copy link
Collaborator

Hm, ok so the easy solution would be to remove the sourceUploadUrl when updating the cloudfunction. That should work for this use case

@ghost
Copy link

ghost commented Jan 14, 2021

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.

If you feel this issue should be reopened, we encourage creating a new issue linking back to this one for added context. If you feel I made an error 🤖 🙉 , please reach out to my human friends 👉 [email protected]. Thanks!

@ghost ghost locked as resolved and limited conversation to collaborators Jan 14, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.