Configurar ADC para un recurso con una cuenta de servicio adjunta

Algunos Google Cloud servicios, como Compute Engine, App Engine y las funciones de Cloud Run, admiten que se adjunte una cuenta de servicio gestionada por el usuario a algunos tipos de recursos. Por lo general, se puede adjuntar una cuenta de servicio cuando los recursos de ese servicio pueden ejecutar o incluir código de aplicación. Cuando asocias una cuenta de servicio a un recurso, el código que se ejecuta en el recurso puede usar esa cuenta de servicio como su identidad.

Asociar una cuenta de servicio gestionada por el usuario es la forma preferida de proporcionar credenciales a ADC para el código de producción que se ejecuta en Google Cloud.

Para obtener ayuda a la hora de determinar los roles que debes asignar a tu cuenta de servicio, consulta Elegir roles predefinidos.

Para obtener información sobre los recursos a los que puedes adjuntar una cuenta de servicio y ayuda para adjuntarla, consulta la documentación de gestión de identidades y accesos sobre cómo adjuntar una cuenta de servicio.

Set up authentication:

  1. Ensure that you have the Create Service Accounts IAM role (roles/iam.serviceAccountCreator). Learn how to grant roles.
  2. Create the service account:

    gcloud iam service-accounts create SERVICE_ACCOUNT_NAME

    Replace SERVICE_ACCOUNT_NAME with a name for the service account.

  3. To provide access to your project and your resources, grant a role to the service account:

    gcloud projects add-iam-policy-binding PROJECT_ID --member="serviceAccount:SERVICE_ACCOUNT_NAME@PROJECT_ID.iam.gserviceaccount.com" --role=ROLE

    Replace the following:

    • SERVICE_ACCOUNT_NAME: the name of the service account
    • PROJECT_ID: the project ID where you created the service account
    • ROLE: the role to grant
  4. To grant another role to the service account, run the command as you did in the previous step.
  5. Grant the required role to the principal that will attach the service account to other resources.

    gcloud iam service-accounts add-iam-policy-binding SERVICE_ACCOUNT_NAME@PROJECT_ID.iam.gserviceaccount.com --member="user:USER_EMAIL" --role=roles/iam.serviceAccountUser

    Replace the following:

    • SERVICE_ACCOUNT_NAME: the name of the service account
    • PROJECT_ID: the project ID where you created the service account
    • USER_EMAIL: the email address for a Google Account

Siguientes pasos