3

I must be missing something because I cant find this option here: https://cloud.google.com/sdk/gcloud/reference/beta/functions/deploy

I want to package and upload my function to a bucket: --stage-bucket But not actually deploy the function

I'm going to deploy multiple functions (different handlers) from the same package with a Deployment Manager template: type: 'gcp-types/cloudfunctions-v1:projects.locations.functions'

gcloud beta functions deploy insists on packaging AND deploying the function. Where is the gcloud beta functions package command?

Here is an example of the DM template I plan to run:

resources:
  - name: resource-name
    type: 'gcp-types/cloudfunctions-v1:projects.locations.functions'
    properties:
      labels:
        testlabel1: testlabel1value
        testlabel2: testlabel2value
      parent: projects/my-project/locations/us-central1
      location: us-central1
      function: function-name
      sourceArchiveUrl: 'gs://my-bucket/some-zip-i-uploaded.zip'
      environmentVariables:
        test: '123'
      entryPoint: handler
      httpsTrigger: {}
      timeout: 60s
      availableMemoryMb: 256
      runtime: nodejs8

EDIT: I realized I have another question. When I upload a zip does that zip need to include dependencies? Do I have to do npm install or pip install first and include those packages in the zip or does cloud functions read my requirements.txt and packages.json and do that for me?

red888
  • 27,709
  • 55
  • 204
  • 392
  • Does it not work to just gsutil cp a zip file of your code into the bucket, and then set the sourceArchiveUrl of the CloudFunctions object to the zip archive? I don't think "packaging" would do any more than that. What are you currently trying? – robsiemb Oct 13 '19 at 15:46
  • thats not very portable and kind of a pain. Yes I could include a makefile in my repo that does this but it would be nice if all I needed was a gcloud command that would zip, upload and properly name the archive instead of scripting this with local tools – red888 Oct 13 '19 at 15:49
  • At least as far as naming goes, it doesn't need to be named anything in particular. It does need to be a zip file though. – robsiemb Oct 13 '19 at 16:01
  • You don't need to include the dependencies, cloud functions installs them assuming they are in package.json (I have updated my answer below). – robsiemb Oct 13 '19 at 16:38
  • package.json is for node but what about python? – red888 Oct 13 '19 at 16:39

2 Answers2

2

The SDK CLI does not provide a command to package your function.

This link will provide you with detail on how to zip your files together. There are just two points to follow:

  • File type should be a zip file.
  • File size should not exceed 100MB limit.

Then you need to call an API, which returns a Signed URL to upload the package.

Once uploaded you can specify the URL minus the extra parameters as the location.

John Hanley
  • 74,467
  • 6
  • 95
  • 159
  • This the Signed URL required for this to work? I cant just point `sourceArchiveUrl` to a zip file in gcs? – red888 Oct 13 '19 at 15:53
  • There are two methods -- one to take an archive out of an existing storage bucket that you have created (sourceArchiveUrl) and the other for cloud functions to give you a signed url to upload into with a PUT request (by making the API call John outlines) and then placing that into sourceUploadUrl on the [CloudFunctions](https://cloud.google.com/functions/docs/reference/rest/v1/projects.locations.functions#resource-cloudfunction) object. (There's also a third, which pulls directly from a source repository, this uses a SourceRepository object). – robsiemb Oct 13 '19 at 15:58
  • I added an example template to my question. I can just upload a zip manually (without signing anything or doing anything other than just taring and uploading it) and then point `sourceArchiveUrl` to that zip i uploaded? Or will I need to generate a hash or checksum or something too? – red888 Oct 13 '19 at 16:05
  • I am not sure as I use the documented method. Try your method and update your question with the results. My answer provides one of three methods. I have not tested the other two. – John Hanley Oct 13 '19 at 16:11
  • @JohnHanley I realized I have another question, see my last edit- when I upload a zip does that zip need to include dependencies? – red888 Oct 13 '19 at 16:27
  • Yes and no. It depends on what the dependencies are, the language you are using, etc. Example: for python dependencies can be specified in the `requirements.txt`. This means they must be installable by `pip` from public resources. For custom/private dependencies, you will need to prepare your package (zip) to include them. – John Hanley Oct 13 '19 at 16:36
  • The key point is that your package will be sent to a builder and put into a container that Cloud Functions runs. The resources to build that container must be available to the builder. – John Hanley Oct 13 '19 at 16:38
1

There is no gcloud functions command to "package" your deployment, presumably because this amounts to just creating a zip file and putting it into the right place, then referencing that place.

Probably the easiest way to do this is to generate a zip file and copy it into a GCS bucket, then set the sourceArchiveUrl on the template to the correct location.

There are 2 other methods:

  • You can point to source code in source repository (this would use the sourceRepository part of the template).
  • You can get a direct url (using this API) to upload a ZIP file to using a PUT request, upload the code there, and then pass this same URL to the signedUploadUrl on the template. This is the method discussed in @John's answer. It does not require you to do any signing yourself, and likewise does not require you to create your own bucket to store the code in (the "Signed URL" refers to a private cloud functions location).

At least with the two zip file methods you do not need to include the (publicly available) dependencies -- the package.json (or requirements.txt) file will be processed by cloud functions to install them. I don't know about the SourceRepository method but I would expect it would work similarly. There's documentation about how cloud functions installs dependencies during deployment of a function for node and python.

robsiemb
  • 6,157
  • 7
  • 32
  • 46