This document explains how to configure your Cloud Build service account
to [impersonate service accounts](https://docs.cloud.google.com/iam/docs/service-account-impersonation) for
Runtime, Compute Engine, or App Engine so that you can deploy images to
managed services.

## Overview

When an image is deployed to specific managed services, the image runs using the
identity of the service account for that managed service. Because
Cloud Build can deploy new instances or versions automatically,
your [specified Cloud Build service account](https://docs.cloud.google.com/build/docs/securing-builds/configure-user-specified-service-accounts)
or the [default Cloud Build service account](https://docs.cloud.google.com/build/docs/cloud-build-service-account)
must be able to impersonate the service account of your managed service. This
requirement applies when deploying to the following managed services:

| Managed Service | Service Account |
|---|---|
| App Engine | [App Engine service account](https://docs.cloud.google.com/appengine/docs/flexible/configure-service-accounts) |
| Cloud Run functions | [Runtime service account](https://docs.cloud.google.com/functions/docs/securing/function-identity) |
| Cloud Run | [Service identity](https://docs.cloud.google.com/run/docs/configuring/services/service-identity) |
| Compute Engine | [Compute Engine service account](https://docs.cloud.google.com/compute/docs/access/service-accounts) |

## Set up service account impersonation

To configure your Cloud Build service account to impersonate
another service account, do the following:

### console


1. Go to the Cloud Build **Permissions** page.

2. Select your Cloud Build service account.

3. Enable the required IAM role for your managed service:

   | Managed Service | Required Role |
   |---|---|
   | App Engine | App Engine Admin ( `roles/appengine.appAdmin` ) |
   | Cloud Run functions | Cloud Functions Developer ( `roles/cloudfunctions.developer` ) |
   | Cloud Run | Cloud Run Admin ( `roles/run.admin` ) |
   | Compute Engine | Compute Instance Admin v1 ( `roles/compute.instanceAdmin.v1` ) |

   Cloud Build shows the **Assign Service Account User Role**
   panel.
4. Choose a service account to impersonate. For example, if you plan to
   deploy to Compute Engine and enabled the Compute Instance Admin v1
   role, you would choose the Compute Engine service account that you
   want your Cloud Build service account to impersonate.

5. Click **Grant permission**.

<br />

### gcloud CLI


In the Google Cloud CLI, enter the following:

      gcloud iam service-accounts add-iam-policy-binding \
            SERVICE_ACCOUNT_TO_IMPERSONATE \
            --member="CLOUD_BUILD_SERVICE_ACCOUNT" \
            --role="roles/iam.serviceAccountUser"

Where:

- <var translate="no">SERVICE_ACCOUNT_TO_IMPERSONATE</var> is the service account that you want to impersonate.
- <var translate="no">CLOUD_BUILD_SERVICE_ACCOUNT</var> is your Cloud Build service account.

<br />

Cloud Build assigns your Cloud Build service account
the Service Account User (`roles/iam.serviceAccountUser`) role for
the service account that you want to impersonate.

The Service Account User role lets a principal attach a service account to
a resource, allowing your Cloud Build service account to
impersonate your chosen managed service account. For more information,
see [Service Account User role](https://docs.cloud.google.com/iam/docs/service-account-permissions#user-role)
and [Attach service accounts to resources](https://docs.cloud.google.com/iam/docs/attach-service-accounts)
in the IAM documentation.