Access control with IAM

Stay organized with collections Save and categorize content based on your preferences.

By default, only project owners and editors can create, update, delete, or invoke services, and only project owners and Cloud Run Admins can modify Identity and Access Management (IAM) policies—for example, make a service public. Learn more about Cloud Run IAM roles.

To grant other principals or groups the ability to perform these actions, you can use IAM to grant roles to different principals.

Control access on an individual service

If you want to control access on a per-service basis, you can use per-service IAM.

Make a service public

You must have the run.services.setIamPolicy permission to configure authentication on a Cloud Run service. This permission is included in both the Owner and Cloud Run Admin roles. See Cloud Run IAM roles for the full list of roles and their associated permissions.

Console UI

For an existing Cloud Run service:

  1. Go to the Google Cloud console:

    Go to Google Cloud console

  2. Click the checkbox at the left of the service you want to make public. (Don't click on the service itself.)

  3. In the information pane in the top right corner click the Permissions tab. If the information pane isn't visible, you may need to click Show Info Panel, then click Permissions.

  4. Click Add principal.

In the New principals textbox, enter the value allUsers

  1. From the Role dropdown menu, select the Cloud Run Invoker role.

  2. Click Save.

  3. You will be prompted to verify that you would like to make this resource public. Click Allow public access to apply the change to the service IAM settings.

For a new service you are creating, create the servicebut make sure you select Allow unauthenticated invocations in the Authentication tab to make the service publicly available. Selecting Require authentication will make the service private.

gcloud

You can make a service publicly accessible by adding the special allUsers member type to a service and granting it the roles/run.invoker role:

  gcloud run services add-iam-policy-binding [SERVICE_NAME] \
    --member="allUsers" \
    --role="roles/run.invoker"

Additionally, when you deploy your service with the gcloud run deploy command, you can specify whether or not to make your service publicly accessible:

gcloud run deploy [SERVICE_NAME] ... --allow-unauthenticated

Terraform

To create a Cloud Run service, add the following to your to your existing main.tf file:

resource "google_cloud_run_service" "default" {
  name     = "cloud-run-srv"
  location = "us-central1"

  template {
    spec {
      containers {
        image = "gcr.io/cloudrun/hello"
      }
    }
  }

  traffic {
    percent         = 100
    latest_revision = true
  }
}

To update the service IAM binding for roles/run.invoker, add the following resource referencing your Cloud Run service:

resource "google_cloud_run_service_iam_binding" "default" {
  location = google_cloud_run_service.default.location
  service  = google_cloud_run_service.default.name
  role     = "roles/run.invoker"
  members = [
    "allUsers"
  ]
}

This binding is only authoritative for the given role. Other IAM bindings within the service IAM policy are preserved.

Domain restricted sharing

If the project is subject to the domain restricted sharing constraint in an organization policy, you will be unable to create public services by default. You can use tags and conditional policy to exempt specific services from this constraint. For details, refer to the blog post on creating public Cloud Run services when domain restricted sharing is enforced.

Add principals

Console UI

  1. Go to the Google Cloud console:

    Go to Google Cloud console

  2. Click the checkbox at the left of the service you want to add principals to. (Don't click on the service itself.)

  3. In the information pane in the top right corner click the Permissions tab. If the information pane isn't visible, you may need to click Show Info Panel, then click Permissions.

  4. Click Add principal.

  5. In the New principals textbox, enter one or more identities that need access to your service.

  6. From the Role dropdown menu, select a role or roles The roles you select appear in the pane with a short description of the permissions they grant.

  7. Click Save.

gcloud

Use the gcloud run services add-iam-policy-binding command:

gcloud run services add-iam-policy-binding SERVICE_NAME \
  --member=MEMBER_TYPE \
  --role=ROLE

where SERVICE_NAME is the service name, MEMBER_TYPE is the principal type (e.g. user:email@domain.com), and ROLE is the role.

For a list of acceptable values for MEMBER_TYPE, see the IAM concepts page. For a list of acceptable values for ROLE, see Cloud Run IAM roles.

Remove principals from a role

Console UI

  1. Go to the Google Cloud console:

    Go to Google Cloud console

  2. Click the checkbox at the left of the service you want to remove principals from. (Don't click on the service itself.)

  3. In the information pane in the top right corner click the Permissions tab. If the information pane isn't visible, you may need to click Show Info Panel, then click Permissions.

  4. Enter the principal you want to remove in the roles list filter, which displays all roles granted to that principal.

  5. Click the delete trash can next to the principal within the desired role to remove that role from the principal.

  6. In the confirmation dialog, you have the option to remove that principal from all roles in the service. Click Remove to remove the principal from the role.

gcloud

Use the gcloud run services remove-iam-policy-binding command:

  gcloud run services remove-iam-policy-binding SERVICE_NAME \
    --member=MEMBER_TYPE \
    --role=ROLE

where SERVICE_NAME is the service name, MEMBER_TYPE is the principal type (e.g. user:email@domain.com), and ROLE is the role.

For a list of acceptable values for MEMBER_TYPE, see the IAM concepts page. For a list of acceptable values for ROLE, see Cloud Run IAM roles.

Bulk addition or removal of principals

Console UI

  1. Go to the Google Cloud console:

    Go to Google Cloud console

  2. Click the checkbox at the left of the service you want to add principals to or remove principals from. (Don't click on the service itself.)

  3. Select the service you want to add principals to or remove principals from.

  4. In the information pane in the top right corner click the Permissions tab. If the information pane isn't visible, you may need to click Show Info Panel, then click Permissions.

If you want to add principals:

  1. Click Add principal.

  2. In the New principals textbox, enter one or more identities that need access to your service.

  3. From the Role dropdown menu, select a role (or roles) The roles you select appear in the pane with a short description of the permissions they grant.

  4. Click Save.

If you want to remove principals:

  1. Enter the principal you want to remove in the roles list filter, which displays all roles granted to that principal.

  2. Click the delete trash can next to the principal within the desired role to remove that role from the principal.

  3. In the confirmation dialog, you have the option to remove that principal from all roles in the service. Click Remove to remove the principal from the role.

gcloud

Create an IAM policy:

cat <<EOF > policy.json
{
  "bindings": [
    {
      "role": ROLE,
      "members": [
        MEMBER_TYPE
      ]
    }
  ]
}
EOF

Use the gcloud run services set-iam-policy command:

gcloud run services set-iam-policy SERVICE_NAME policy.json

For a list of acceptable values for MEMBER_TYPE, see the IAM concepts page. For a list of acceptable values for ROLE, see Cloud Run IAM roles.

View principals

Console UI

  1. Go to the Google Cloud console:

    Go to Google Cloud console

  2. Click the checkbox at the left of the service you want to view for principals and roles. (Don't click on the service itself.)

  3. Select the service you want to view for principals and roles.

  4. In the information pane in the top right corner click the Permissions tab. If the information pane isn't visible, you may need to click Show Info Panel, then click Permissions.

  5. The list of roles and principals will be shown, grouped by role granted.

gcloud

Use the gcloud run services get-iam-policy command:

gcloud run services get-iam-policy SERVICE_NAME

Control access on all services in a project

If you want to grant roles to principals on all services in a project, you can use project-level IAM.

Console UI

  1. Go to the Google Cloud console:

    Go to Google Cloud console

  2. Find the principal you want to grant a project-wide role.

  3. Click the edit Pencil on the right side of the principal's row.

  4. Click Add another role and pick the desired role.

  5. Click Save.

gcloud

Use the gcloud projects add-iam-policy-binding command:

gcloud projects add-iam-policy-binding PROJECT_ID \
  --member=MEMBER_TYPE \
  --role=ROLE

where MEMBER_TYPE is the principal type (e.g. user:email@domain.com), and ROLE is the role (e.g. roles/run.admin).

The allUsers and allAuthenticatedUsers principal types can't be applied at the project level, and instead must be added to each service individually.

Next steps

Learn how to securely authenticate developers, services, and users to the services you just secured.