2
votes

I am new to terraform I want to create a Google Cloud Function using Terraform but want to pull the source code from Github.

I managed to do this zipping up the function and copying it into Cloud Storage using Terraform, but I do not like this workflow as I have to run a script to kick things off. I rather just do a PR on Github and see the new code in GCP.

I already setup Google Cloud Source Repositories to source from my Github.

The Terraform doc to use the "source_repository" argument is not clear to me. What I would like to do is just grab the source from HEAD on the master branch.

I just would like to know how to specify the “source_repository” argument in this case.

My cloud source repository URL is https://source.cloud.google.com/projectName/github_offiecDomain_gitRepoName

My cloud function terraform script looks like

resource "google_cloudfunctions_function" "js_function" {
source_repository  {
  url = "https://source.cloud.google.com/projectName/github_offiecDomain_gitRepoName"
   }
}

When I terraform apply it returns an error

google_cloudfunctions_function.js_function: Creating...

    Error: googleapi: Error 400: The request has errors, badRequest
    
      on main.tf line 89, in resource "google_cloudfunctions_function" "js_function":
      89: resource "google_cloudfunctions_function" "js_function" {

EDIT Upon moving my log level to trace

Here is what I have

2020-08-05T11:07:30.722+0500 [DEBUG] plugin.terraform-provider-google_v3.17.0_x5: ff
2020-08-05T11:07:30.722+0500 [DEBUG] plugin.terraform-provider-google_v3.17.0_x5: {
2020-08-05T11:07:30.722+0500 [DEBUG] plugin.terraform-provider-google_v3.17.0_x5:   "error": {
2020-08-05T11:07:30.722+0500 [DEBUG] plugin.terraform-provider-google_v3.17.0_x5:     "code": 400,
2020-08-05T11:07:30.722+0500 [DEBUG] plugin.terraform-provider-google_v3.17.0_x5:     "message": "The request has errors",
2020-08-05T11:07:30.722+0500 [DEBUG] plugin.terraform-provider-google_v3.17.0_x5:     "errors": [
2020-08-05T11:07:30.722+0500 [DEBUG] plugin.terraform-provider-google_v3.17.0_x5:       {
2020-08-05T11:07:30.722+0500 [DEBUG] plugin.terraform-provider-google_v3.17.0_x5:         "message": "The request has errors",
2020-08-05T11:07:30.722+0500 [DEBUG] plugin.terraform-provider-google_v3.17.0_x5:         "domain": "global",
2020-08-05T11:07:30.722+0500 [DEBUG] plugin.terraform-provider-google_v3.17.0_x5:         "reason": "badRequest"
2020-08-05T11:07:30.722+0500 [DEBUG] plugin.terraform-provider-google_v3.17.0_x5:       }
2020-08-05T11:07:30.722+0500 [DEBUG] plugin.terraform-provider-google_v3.17.0_x5:     ],
2020-08-05T11:07:30.722+0500 [DEBUG] plugin.terraform-provider-google_v3.17.0_x5:     "status": "INVALID_ARGUMENT"
2020-08-05T11:07:30.722+0500 [DEBUG] plugin.terraform-provider-google_v3.17.0_x5:   }
2020-08-05T11:07:30.722+0500 [DEBUG] plugin.terraform-provider-google_v3.17.0_x5: }

I suppose that my URL argument is not valid. Any leads how can I write my URL?

3
Did you ever resolve this? Having the same problem.Lukas Batteau
@LukasBatteau there is a pattern for the url that we need to follow. You cannot direct just copy and past the url. Replace '' with your own names in below url source.developers.google.com/projects*/repos/*/moveable-aliases/*/paths/M-sAnNan

3 Answers

0
votes

You Terraform Script for Cloud Function misses an equal sign which is causing the INVALID ARGUMENT error, could you please try the following code:

resource "google_cloudfunctions_function" "js_function" {
source_repository = {
  url = "https://source.cloud.google.com/projectName/github_offiecDomain_gitRepoName"
   }
}

Also note theat, the Terraform Official documentation says to format the url value to be, https://source.developers.google.com/projects/*/repos/*/moveable-aliases/*/paths/*

To get this to work, supoose that your source code and your repository had the following information:

    project: "YOUR PROJECT NAME"
    repo:"YOUR REPOSITORY"
    branch: master
    directory_in_repo_with_src: src/functions/bin

Then you will need to put the URL as following:

source_repository = {
  url = https://source.developers.google.com/projects/YOUR_PROJECT/repos/YOUR REPOSITORY/master/paths/src/functions/bin
}

Please let me know if it works for you.

0
votes

The URL is very specific:

To refer to a moveable alias (branch): https://source.developers.google.com/projects/*/repos/*/moveable-aliases/*/paths/* In particular, to refer to HEAD use master moveable alias.

I made the mistake thinking that 'moveable-aliases' was a placeholder for the branch name, but it actually has to be in the path.

See https://cloud.google.com/functions/docs/reference/rest/v1/projects.locations.functions#SourceRepository

0
votes

There is no option to deploy a Cloud Fuction directly from GitHub. However, you can clone the repository to Google Cloud Source Repository and then deploy the Cloud Function from there

From the error: The cloud function has some mandatory parameters Like entry_point event_trigger