Configurar o ADC para um recurso com uma conta de serviço anexada

Alguns serviços do Google Cloud , como o Compute Engine, o App Engine e o Cloud Run functions, permitem anexar uma conta de serviço gerenciada pelo usuário a alguns tipos de recursos. Em geral, a anexação de uma conta de serviço acontece quando os recursos do serviço podem executar ou incluir códigos de aplicativo. Quando você anexa uma conta de serviço a um recurso, o código em execução no recurso pode usar essa conta de serviço como a própria identidade.

Anexar uma conta de serviço gerenciada pelo usuário é a maneira preferencial de fornecer credenciais ao ADC para códigos de produção em execução no Google Cloud.

Para receber ajuda ao determinar os papéis que você precisa fornecer à conta de serviço, consulte Escolher papéis predefinidos.

Para informações sobre quais recursos podem ser anexados a uma conta de serviço e ajuda para anexar a conta de serviço ao recurso, consulte a documentação do IAM sobre como anexar uma conta de serviço.

Set up authentication:

  1. Create the service account:

    gcloud iam service-accounts create SERVICE_ACCOUNT_NAME

    Replace SERVICE_ACCOUNT_NAME with a name for the service account.

  2. 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
  3. To grant another role to the service account, run the command as you did in the previous step.
  4. 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

A seguir