Build process overview (original) (raw)

This guide shows the build process overview for functions that were deployed using the gcloud functions command. To learn the build process for functions deployed using the gcloud run command, see:

When you deploy your function's source code using the gcloud functions deploycommand, that source is stored in a Cloud Storage bucket. Cloud Build then automatically builds your code into a container image and pushes that image to an image registry.

The process of building the image is entirely automatic and requires no direct input from you. All the resources used in the build process execute in your own user project.

Executing the build process within your project means that:

Characteristics of the build process

The build process has the following characteristics:

View your build image logs

A key benefit of having the build image process in your user project is access to build logs. You can use gcloud CLI or the Google Cloud console to reach the logs, which are available through Cloud Logging.

gcloud

  1. Deploy your function using the gcloud functions deploy command.
  2. The URL of the logs is shown as part of the response in your terminal window. For example:
    Deploying function (may take a while - up to 2 minutes)...⠹
    For Cloud Build Stackdriver Logs, visit:
    https://https://console.cloud.google.com/logs/viewer?project=&advancedFilter=resource.type%
    3Dbuild%0Aresource.labels.build_id%3D38d5b662-2315-45dd-8aa2-
    380d50d4f5e8%0AlogName%3Dprojects%2F%
    2Flogs%2Fcloudbuild
    Deploying function (may take a while - up to 2 minutes)...done.

Google Cloud console

To view function logs in the Cloud Run page:

  1. Go to Cloud Run
  2. Click the chosen function in the displayed list.
  3. Click the LOGS tab to get the request and container logs for all revisions of this function. You can filter by log severity level.

Image registry

Artifact Registry is used to store the images built from your function source code. Images are stored in a repository named REGION-docker.pkg.dev/PROJECT_ID/gcf-artifactslocated in the same project where your function is created.

To specify a self-managed Artifact Registry repository, run the following command:

gcloud functions deploy FUNCTION_NAME
--docker-repository=REPOSITORY
[FLAGS...]

Replace the following:

When specifying a Artifact Registry repository located in a different project or region you may need to consider additional configurations:

IAM configurations:

Secure your build with private pools

To allow your functions to use dependencies (for example, npm packages), Cloud Build has by default unlimited internet access during the build process. If you have set up a VPC Service Controls (VPC SC) perimeter and want to limit the build's access only to dependencies stored inside the perimeter, you can use the Cloud Build private worker poolsfeature.

In general, follow these steps to set up your private pool:

  1. Create your private worker pool. See Create and manage private pools.
  2. Configure your VPC Service Controls perimeter. SeeUsing VPC Service Controls.
  3. If your private worker pool is in a different project than your function, you need to grant the Cloud Functions Service AgentService Account (service-FUNCTION_PROJECT_NUMBER@gcf-admin-robot.iam.gserviceaccount.com) the cloudbuild.workerPoolUser role so that the Cloud Build service can access the worker pool.
    gcloud projects add-iam-policy-binding PRIVATE_POOL_PROJECT_ID \
    --member serviceAccount:service-FUNCTION_PROJECT_NUMBER@gcf-admin-robot.iam.gserviceaccount.com
    --role roles/cloudbuild.workerPoolUser
    Replace FUNCTION_PROJECT_NUMBER with the number of the project where the function runs and PRIVATE_POOL_PROJECT_ID with the ID of the project in which the worker pool is located. See Running builds in a private poolfor more information.
  4. Deploy your function to build using a private pool:
    gcloud functions deploy FUNCTION_NAME \
    --runtime RUNTIME \
    --build-worker-pool PRIVATE_POOL_NAME
    [FLAGS...]
    Replace FUNCTION_NAME with the name of the function, RUNTIMEwith the runtime you are using, and PRIVATE_POOL_NAME with the name of your pool.

To stop using a given private pool and instead use the default Cloud Build pool, use the --clear-build-worker-pool flag when re-deploying.

gcloud functions deploy FUNCTION_NAME
--runtime RUNTIME
--clear-build-worker-pool [FLAGS...]

Replace FUNCTION_NAME with the name of the function andRUNTIME with the runtime you are using.

Secure your build with a custom service account

Your function's source code is sent to Cloud Build to be containerized. The containerized function is stored in Artifact Registry and deployed to Cloud Run as a service. Cloud Run functions leverages Cloud Build when building and deploying your Cloud Run function. By default, Cloud Run functions uses the default Cloud Build service account as the principal when performing your build. Starting in July 2024, Cloud Buildchanged the default behavior for how Cloud Build uses service accountsin new projects. As a result of this change, new projects deploying functions for the first time may be using a default Cloud Build service account withinsufficient permissions for building a function.

For Google Cloud projects created before July 2024, Cloud Build uses the legacy Cloud Build service account. This service account was designed to help users execute a broad range of use cases that may be too permissive for your project's needs. If you want to move your existing projects away from this service account, you can take the following steps to further secure your functions build environment:

Prevent the legacy Cloud Build service account from being used for build

You can verify that your project is using the legacy Cloud Build service account by inspecting the details of your function's build. The default build service account has the following format:

PROJECT_NUMBER@cloudbuild.gserviceaccount.com.

You can forcibly disable use of this service account by setting the org policy constraint cloudbuild.useBuildServiceAccount to Not Enforced. Alternatively, removing all of its role grants will limit its ability to access Google Cloud resources.

Prevent the default compute service account from being used for build

The default compute service account has the formatPROJECT_NUMBER-compute@developer.gserviceaccount.com. You can disable it from being the default used for the build by setting the org policy cloudbuild.useComputeServiceAccount to Not Enforced. Alternatively, disabling this service account prevents it from being used to access Google Cloud resources.

Provide a service account for building functions

As part of a function's configuration, you can specify a build service account when deploying the function. When the legacy Cloud Build service account and the default compute service account are prevented from being used for build, you must specify a build service account in order to deploy a function, as described in this section.

If you are impacted by the changes described inCloud Build Service Account Change, you can do one of the following:

Here are some scenarios where you may want to provide a different service account to be used when Cloud Build builds your function:

The following sections show how to create a custom Cloud Build service account for function deployments.

Create a service account

Create a new service account as described in Create a service account.

Grant Permissions

The service account you use will need the following roles:

Grant the following roles using theGoogle Cloud CLI, or useGoogle Cloud console.

gcloud projects add-iam-policy-binding PROJECT_ID \
--member=serviceAccount:SA_EMAIL \
    --role=roles/logging.logWriter

gcloud projects add-iam-policy-binding PROJECT_ID \
--member=serviceAccount:SA_EMAIL \
--role=roles/artifactregistry.writer

gcloud projects add-iam-policy-binding PROJECT_ID \
--member=serviceAccount:SA_EMAIL \
--role=roles/storage.objectViewer

Replace the following:

VPC Service Controls considerations

If you have a VPC Service Controls perimeter protecting both your project and the Cloud Run functions API, and if you're using the Compute Engine default service account as the Cloud Build Service Account role for Cloud Run functions, you must create the following ingress rules:

Deploy a function with a custom service account

To pass in a user-created service account, to be used by Cloud Build, when deploying your function, run the following gcloud command:


gcloud functions deploy FUNCTION_NAME \
   --gen2 \
   --region=REGION \
   --project=PROJECT_ID \
   --runtime=RUNTIME \
   --entry-point=CODE_ENTRYPOINT \
   --build-service-account=projects/PROJECT_ID/serviceAccounts/SA_EMAIL \
   --memory=256Mi \
   --trigger-http \
   --source=.

Replace the following:

Grant the Cloud Build service account access to the VPC Service Controls perimeter

Cloud Run functions uses Cloud Build to build your source code into a runnable container. To use Cloud Run functions with VPC Service Controls, you must configure your Cloud Build service account (whether default or custom) to have access to your service perimeter.

Find the service account name

If you are using the default Cloud Build service account, you can find its name as follows:

  1. Use the IAM page in the Google Cloud console to find the Cloud Build service account.
    Open IAM
  2. Make sure the correct project is displayed in the project drop-down.
  3. Search for cloudbuild.gserviceaccount.com. The email address in the formPROJECT_NUMBER@cloudbuild.gserviceaccount.com is the service account name.

If you have acustom Cloud Build service account, use that name instead.

Grant the service account access to the service perimeter

Once you have the service account name, follow the guide atLimit access by user or service accountto create an access level for the service account. Then, followAdding an access level to an existing perimeterto add the access level to your service perimeter.