Newer
Older
gcp_docs_scrape / page_content / deploy_a_function.txt
Deploy a function
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
Deploy a Cloud Run function using the gcloud CLI
.
The deployment process takes your source code and configuration settings and
builds a runnable image
 that Cloud Run functions
manages automatically in order to handle requests to your function.
Deployment basics
For an introduction on the type of functions you can deploy, see
Write Cloud Run functions
.
Users deploying functions must have the
Cloud Functions Developer
IAM role or a role that includes the same permissions. See also
Additional configuration for deployment
.
In the Google Cloud console, activate Cloud Shell.
Activate Cloud Shell
      At the bottom of the Google Cloud console, a
      
Cloud Shell
      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.
    
Use the 
`gcloud functions deploy`
command to deploy a function:
```gcloud functions deploy YOUR_FUNCTION_NAME \
   --region=YOUR_REGION \
   --runtime=YOUR_RUNTIME \
   --source=YOUR_SOURCE_LOCATION \
   --entry-point=YOUR_CODE_ENTRYPOINT \
   TRIGGER_FLAGS```
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.
Note:
 Run 
`gcloud config set functions/gen2 true`
 to set all future
 first time deployments.
The 
`--region`
 flag
specifies the region in which to deploy your function. See
Locations
 for a list of regions supported by
Cloud Run.
The 
`--runtime`
 flag
specifies which language runtime your function uses. See
Runtime support
 for a list of supported
runtime IDs.
The 
`--source`
 flag
specifies the location of your function source code.
The 
`--entry-point`
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
Function entry point
.
To specify the 
trigger
 for your
function, additional flags (represented as
`TRIGGER_FLAGS`
 above) are required, depending on
the trigger you want to use:
Trigger flags
Trigger description
`--trigger-http`
Trigger the function with an HTTP(S) request.
`--trigger-topic=YOUR_PUBSUB_TOPIC`
Trigger the function when a message is published to the specified
     Pub/Sub topic.
`--trigger-bucket=YOUR_STORAGE_BUCKET`
Trigger the function when an object is created or overwritten in the
     specified Cloud Storage bucket.
`--trigger-event-filters=EVENTARC_EVENT_FILTERS`
Trigger the function with Eventarc when an
     event that matches the specified filters occurs.
For a complete reference on the deployment command and its flags, see the
`gcloud functions deploy`
documentation.
For more details about 
`gcloud functions deploy`
 configuration flags,
refer to 
Cloud Run documentation
.
When deployment finishes successfully, functions appear with a green check
mark in the Cloud Run overview page in the
Google Cloud console
.
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.
Note:
 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.
HTTP endpoint URL
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.
Terraform examples
For examples about how to deploy functions using Terraform, see the
Terraform HTTP example
 and
Terraform Pub/Sub example
.
Configure networking
Functions created using the 
Cloud Functions v2 API
(for example, by using 
`gcloud functions`
, the REST API, or Terraform) can be
managed with the 
Cloud Run Admin API
as well as the Cloud Functions v2 API.
Note:
 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.
To learn more about managing networks for functions, including how to route
VPC network traffic
, see
Best practices for Cloud Run networking
.
Learn how to deploy Cloud Run functions on Cloud Run
Deploying functions on Cloud Run is similar to the steps described in
this document, but with some added advantages:
You can use the Google Cloud console, as well as the gcloud CLI
(
`gcloud run deploy`
).
The steps for specifying triggers are slightly different. To learn more, see
triggers and retries
and 
examples of function triggers
.
Cloud Run offers a broader array of configuration options:
Minimum instances
Concurrency
Container configuration
CPU limits
Memory limits
Request timeout
Secrets
Environment variables
Execution environment
HTTP/2
Service accounts
Cloud SQL connections
Session affinity and traffic splitting
Tags
Networking