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

Alguns Google Cloud serviços, como o Compute Engine, o App Engine e as funções do Cloud Run, suportam a associação de umaconta de serviço gerida pelo utilizador a alguns tipos de recursos. Geralmente, a associação de uma conta de serviço é suportada quando os recursos desse serviço podem executar ou incluir código de aplicação. Quando anexa uma conta de serviço a um recurso, o código executado no recurso pode usar essa conta de serviço como a sua identidade.

Anexar uma conta de serviço gerida pelo utilizador é a forma preferencial de fornecer credenciais ao ADC para código de produção executado no Google Cloud.

Para obter ajuda na determinação das funções que tem de fornecer à sua conta de serviço, consulte o artigo Escolha funções predefinidas.

Para obter informações sobre os recursos aos quais pode anexar 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. 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

O que se segue?