Newer
Older
gcp_docs_scrape / raw_soup / deploy_a_function_RAW.txt
<div class="devsite-article-body clearfix devsite-no-page-title">
<h1 data-text="Deploy a function" id="deploy-a-function" tabindex="-1">Deploy a function</h1>
<p>This guide shows you how to deploy a function from source code using the
`gcloud functions` command. To learn how to deploy a function using the
`gcloud run` command, see
<a href="/run/docs/quickstarts/functions/deploy-functions-gcloud">Deploy a Cloud Run function using the gcloud CLI</a>.</p>
<p>The deployment process takes your source code and configuration settings and
<a href="/functions/docs/building">builds a runnable image</a> that Cloud Run functions
manages automatically in order to handle requests to your function.</p>
<h2 data-text="Deployment basics" id="basics" tabindex="-1">Deployment basics</h2>
<p>For an introduction on the type of functions you can deploy, see
<a href="/run/docs/write-functions">Write Cloud Run functions</a>.</p>
<p>Users deploying functions must have the
<a href="/functions/docs/reference/iam/roles#cloudfunctions.developer">Cloud Functions Developer</a>
IAM role or a role that includes the same permissions. See also
<a href="/functions/docs/reference/iam/roles#additional-configuration">Additional configuration for deployment</a>.</p>
<ol>
<li>
<p>In the Google Cloud console, activate Cloud Shell.</p>
<p><a class="button button-primary" href="https://console.cloud.google.com/?cloudshell=true" target="console" track-metadata-end-goal="launchCloudShell" track-name="consoleLink" track-type="commonIncludes">Activate Cloud Shell</a></p>
<p>
      At the bottom of the Google Cloud console, a
      <a href="/shell/docs/how-cloud-shell-works">Cloud Shell</a>
      session starts and displays a command-line prompt. Cloud Shell is a shell environment
      with the Google Cloud CLI
      already installed and with values already set for
      your current project. It can take a few seconds for the session to initialize.
    </p>
</li>
<li><p>Use the <a href="/sdk/gcloud/reference/functions/deploy">`gcloud functions deploy`</a>
command to deploy a function:</p>
<div></div><devsite-code>```gcloud functions deploy YOUR_FUNCTION_NAME \
   --region=YOUR_REGION \
   --runtime=YOUR_RUNTIME \
   --source=YOUR_SOURCE_LOCATION \
   --entry-point=YOUR_CODE_ENTRYPOINT \
   TRIGGER_FLAGS```</devsite-code>
<p>The first argument, `YOUR_FUNCTION_NAME`, is a name for
your deployed function. The function name must start with a letter
followed by up to 62 letters, numbers, hyphens, or underscores, and must end
with a letter or a number. The name of the Cloud Run service that
is created for your function will replace underscores with hyphens and
uppercase letters will be converted to lowercase. For example,
`Function_1` will be given the name `function-1` in Cloud Run.</p>
<aside class="note"><strong>Note:</strong><span> Run `gcloud config set functions/gen2 true` to set all future
 first time deployments.</span></aside>
<ul>
<li><p>The <a href="/sdk/gcloud/reference/functions/deploy#--region">`--region`</a> flag
specifies the region in which to deploy your function. See
<a href="/run/docs/locations">Locations</a> for a list of regions supported by
Cloud Run.</p></li>
<li><p>The <a href="/sdk/gcloud/reference/functions/deploy#--runtime">`--runtime`</a> flag
specifies which language runtime your function uses. See
<a href="/functions/docs/runtime-support">Runtime support</a> for a list of supported
runtime IDs.</p></li>
<li><p>The <a href="/sdk/gcloud/reference/functions/deploy#--source">`--source`</a> flag
specifies the location of your function source code.</p></li>
<li><p>The <a href="/sdk/gcloud/reference/functions/deploy#--entry-point">`--entry-point`</a>
flag specifies the entry point to your function in your source code. This is
the code that will be executed when your function runs. The value of this
flag must be a function name or fully-qualified class name that exists in
your source code. For more information, see
<a href="/run/docs/write-functions#function_entry_point">Function entry point</a>.</p></li>
<li><p>To specify the <a href="/sdk/gcloud/reference/functions/deploy#--trigger-bucket">trigger</a> for your
function, additional flags (represented as
`TRIGGER_FLAGS` above) are required, depending on
the trigger you want to use:</p>
<table>
<tr>
<th>Trigger flags</th>
<th>Trigger description</th>
</tr>
<tr>
<td>`--trigger-http`</td>
<td>Trigger the function with an HTTP(S) request.</td>
</tr>
<tr>
<td>`--trigger-topic=YOUR_PUBSUB_TOPIC`</td>
<td>Trigger the function when a message is published to the specified
     Pub/Sub topic.</td>
</tr>
<tr>
<td>`--trigger-bucket=YOUR_STORAGE_BUCKET`</td>
<td>Trigger the function when an object is created or overwritten in the
     specified Cloud Storage bucket.</td>
</tr>
<tr>
<td>`--trigger-event-filters=EVENTARC_EVENT_FILTERS`</td>
<td>Trigger the function with Eventarc when an
     event that matches the specified filters occurs.</td>
</tr>
</table>
<p>For a complete reference on the deployment command and its flags, see the
<a href="/sdk/gcloud/reference/functions/deploy">`gcloud functions deploy`</a>
documentation.</p>
<p>For more details about `gcloud functions deploy` configuration flags,
refer to <a href="/run/docs/configuring">Cloud Run documentation</a>.</p></li>
</ul></li>
</ol>
<p>When deployment finishes successfully, functions appear with a green check
mark in the Cloud Run overview page in the
<a href="https://console.cloud.google.com/run">Google Cloud console</a>.</p>
<p>The initial deployment of a function may take several minutes, while the
underlying infrastructure is provisioned. Redeploying an existing function
is faster, and incoming traffic is automatically migrated to the newest version.</p>
<aside class="note"><strong>Note:</strong><span> Instances provisioned with a previous version of a function may continue
running and processing traffic for several minutes after a new deployment has
finished. This ensures that traffic sent to your function while a deployment is
in progress isn't dropped. Also note that when a deployment fails,
if there is a previous version of the function, it will continue to be
available in most cases.</span></aside>
<h2 data-text="HTTP endpoint URL" id="http-endpoint" tabindex="-1">HTTP endpoint URL</h2>
<p>When you create a function with the `gcloud functions` command or the
Cloud Functions v2 API, by default, the function has a
`cloudfunctions.net` HTTP endpoint URL. If you take this function and deploy it
on Cloud Run, your function also receives a `run.app` HTTP endpoint
URL. However, functions created in Cloud Run won't have an
`cloudfunctions.net` HTTP endpoint URL. A function's `cloudfunctions.net` URL
and `run.app` URL behave in exactly the same way. They are interchangeable,
and are used to trigger your function.</p>
<h2 data-text="Terraform examples" id="terraform_examples" tabindex="-1">Terraform examples</h2>
<p>For examples about how to deploy functions using Terraform, see the
<a href="/functions/docs/tutorials/terraform">Terraform HTTP example</a> and
<a href="/functions/docs/tutorials/terraform-pubsub">Terraform Pub/Sub example</a>.</p>
<h2 data-text="Configure networking" id="configure_networking" tabindex="-1">Configure networking</h2>
<p>Functions created using the <a href="/functions/docs/apis">Cloud Functions v2 API</a>
(for example, by using `gcloud functions`, the REST API, or Terraform) can be
managed with the <a href="/run/docs/reference/cloud-run-admin-api-overview">Cloud Run Admin API</a>
as well as the Cloud Functions v2 API.</p>
<aside class="note"><strong>Note:</strong><span> If you created a Cloud Run function using
`gcloud run` commands or the Cloud Run Admin API, you can't manage that function
with `gcloud functions` commands or the Cloud Functions v2 API.</span></aside>
<p>To learn more about managing networks for functions, including how to route
<a href="/run/docs/configuring/connecting-vpc">VPC network traffic</a>, see
<a href="/run/docs/configuring/networking-best-practices">Best practices for Cloud Run networking</a>.</p>
<h2 data-text="Learn how to deploy Cloud Run functions on Cloud Run" id="learn-about-cloud-run" tabindex="-1">Learn how to deploy Cloud Run functions on Cloud Run</h2>
<p>Deploying functions on Cloud Run is similar to the steps described in
this document, but with some added advantages:</p>
<ul>
<li>You can use the Google Cloud console, as well as the gcloud CLI
(`gcloud run deploy`).</li>
<li>The steps for specifying triggers are slightly different. To learn more, see
<a href="/run/docs/functions/comparison#triggers_and_retries">triggers and retries</a>
and <a href="/run/docs/function-triggers#triggers">examples of function triggers</a>.</li>
<li>Cloud Run offers a broader array of configuration options:
<ul>
<li><a href="/run/docs/configuring/min-instances">Minimum instances</a></li>
<li><a href="/run/docs/configuring/concurrency">Concurrency</a></li>
<li><a href="/run/docs/configuring/services/containers">Container configuration</a></li>
<li><a href="/run/docs/configuring/services/cpu">CPU limits</a></li>
<li><a href="/run/docs/configuring/services/memory-limits">Memory limits</a></li>
<li><a href="/run/docs/configuring/request-timeout">Request timeout</a></li>
<li><a href="/run/docs/configuring/services/secrets">Secrets</a></li>
<li><a href="/run/docs/configuring/services/environment-variables">Environment variables</a></li>
<li><a href="/run/docs/configuring/execution-environments">Execution environment</a></li>
<li><a href="/run/docs/configuring/http2">HTTP/2</a></li>
<li><a href="/run/docs/configuring/services/service-identity">Service accounts</a></li>
<li><a href="/run/docs/configuring/connect-cloudsql">Cloud SQL connections</a></li>
<li><a href="/run/docs/configuring/session-affinity#affinity-and-traffic-splitting">Session affinity and traffic splitting</a></li>
<li><a href="/run/docs/configuring/tags">Tags</a></li>
<li><a href="/run/docs/configuring/networking-best-practices">Networking</a></li>
</ul></li>
</ul>
<devsite-hats-survey class="nocontent" hats-id="Nd7nTix2o0eU5NUYprb0ThtUc5jf" listnr-id="83405"></devsite-hats-survey>
</div>