This page explains how to create service accounts using the
Identity and Access Management (IAM) API, the Trusted Cloud console, and the gcloud
command-
line tool.
By default, each project can have up to 100 service accounts that control access to your resources. You can request a quota increase if necessary. Learn more about quotas and limits.
Before you begin
Enable the IAM API.
Set up authentication.
Select the tab for how you plan to use the samples on this page:
Console
When you use the Trusted Cloud console to access Trusted Cloud by S3NS services and APIs, you don't need to set up authentication.
gcloud
After installing the Google Cloud CLI, sign in to the gcloud CLI with your federated identity and then initialize it by running the following command:
gcloud init
C#
To use the .NET samples on this page in a local development environment, install and initialize the gcloud CLI, and then set up Application Default Credentials with your user credentials.
-
Install the Google Cloud CLI.
-
Configure the gcloud CLI to use your federated identity.
For more information, see Sign in to the gcloud CLI with your federated identity.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
Create local authentication credentials for your user account:
gcloud auth application-default login
If an authentication error is returned, and you are using an external identity provider (IdP), confirm that you have signed in to the gcloud CLI with your federated identity.
For more information, see Set up ADC for a local development environment in the Trusted Cloud authentication documentation.
C++
To use the C++ samples on this page in a local development environment, install and initialize the gcloud CLI, and then set up Application Default Credentials with your user credentials.
-
Install the Google Cloud CLI.
-
Configure the gcloud CLI to use your federated identity.
For more information, see Sign in to the gcloud CLI with your federated identity.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
Create local authentication credentials for your user account:
gcloud auth application-default login
If an authentication error is returned, and you are using an external identity provider (IdP), confirm that you have signed in to the gcloud CLI with your federated identity.
For more information, see Set up ADC for a local development environment in the Trusted Cloud authentication documentation.
Go
To use the Go samples on this page in a local development environment, install and initialize the gcloud CLI, and then set up Application Default Credentials with your user credentials.
-
Install the Google Cloud CLI.
-
Configure the gcloud CLI to use your federated identity.
For more information, see Sign in to the gcloud CLI with your federated identity.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
Create local authentication credentials for your user account:
gcloud auth application-default login
If an authentication error is returned, and you are using an external identity provider (IdP), confirm that you have signed in to the gcloud CLI with your federated identity.
For more information, see Set up ADC for a local development environment in the Trusted Cloud authentication documentation.
Java
To use the Java samples on this page in a local development environment, install and initialize the gcloud CLI, and then set up Application Default Credentials with your user credentials.
-
Install the Google Cloud CLI.
-
Configure the gcloud CLI to use your federated identity.
For more information, see Sign in to the gcloud CLI with your federated identity.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
Create local authentication credentials for your user account:
gcloud auth application-default login
If an authentication error is returned, and you are using an external identity provider (IdP), confirm that you have signed in to the gcloud CLI with your federated identity.
For more information, see Set up ADC for a local development environment in the Trusted Cloud authentication documentation.
Python
To use the Python samples on this page in a local development environment, install and initialize the gcloud CLI, and then set up Application Default Credentials with your user credentials.
-
Install the Google Cloud CLI.
-
Configure the gcloud CLI to use your federated identity.
For more information, see Sign in to the gcloud CLI with your federated identity.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
Create local authentication credentials for your user account:
gcloud auth application-default login
If an authentication error is returned, and you are using an external identity provider (IdP), confirm that you have signed in to the gcloud CLI with your federated identity.
For more information, see Set up ADC for a local development environment in the Trusted Cloud authentication documentation.
REST
To use the REST API samples on this page in a local development environment, you use the credentials you provide to the gcloud CLI.
After installing the Google Cloud CLI, sign in to the gcloud CLI with your federated identity and then initialize it by running the following command:
gcloud init
For more information, see Authenticate for using REST in the Trusted Cloud authentication documentation.
-
Understand IAM service accounts
Required roles
To get the permissions that
you need to create service accounts,
ask your administrator to grant you the
Create Service Accounts (roles/iam.serviceAccountCreator
)
IAM role on the project.
For more information about granting roles, see Manage access to projects, folders, and organizations.
You might also be able to get the required permissions through custom roles or other predefined roles.
If you want to grant newly created service accounts access to your project, you
also need the Project IAM admin (roles/resourcemanager.projectIamAdmin
) role.
Create a service account
When you create a service account, you must provide an alphanumeric ID
(SERVICE_ACCOUNT_NAME
in the samples below), such as
my-service-account
. The ID must be between 6 and 30 characters, and can
contain lowercase alphanumeric characters and dashes. After you create a service
account, you cannot change its name.
The service account's name appears in the email address that is provisioned
during creation, in the format
SERVICE_ACCOUNT_NAME@PROJECT_ID.s3ns-system.iam.gserviceaccount.com
.
Each service account also has a permanent, unique numeric ID, which is generated automatically.
You also provide the following information when you create a service account:
DESCRIPTION
is an optional description for the service account.DISPLAY_NAME
is a friendly name for the service account.PROJECT_ID
is the ID of your Trusted Cloud by S3NS project.
After you create a service account, you might need to wait for 60 seconds or more before you use the service account. This behavior occurs because read operations are eventually consistent; it can take time for the new service account to become visible. If you try to read or use a service account immediately after you create it, and you receive an error, you can retry the request with exponential backoff.
Console
- In the Trusted Cloud console, go to the Create service account page.
- Select a Trusted Cloud project.
- Enter a service account name to display in the Trusted Cloud console.
The Trusted Cloud console generates a service account ID based on this name. Edit the ID if necessary. You cannot change the ID later.
- Optional: Enter a description of the service account.
- If you don't want to set access controls now, click Done to finish creating the service account. To set access controls now, click Create and continue and continue to the next step.
- Optional: Choose one or more IAM roles to grant to the service account on the project.
- When you are done adding roles, click Continue.
- Optional: In the Service account users role field, add members that need to attach the service account to other resources.
- Optional: In the Service account admins role field, add members that need to manage the service account.
- Click Done to finish creating the service account.
gcloud
-
In the Trusted Cloud console, activate Cloud Shell.
At the bottom of the Trusted Cloud console, a Cloud Shell session starts and displays a command-line prompt. Cloud Shell is a shell environment with the Google Cloud CLI already installed and with values already set for your current project. It can take a few seconds for the session to initialize.
-
To create the service account, run the
gcloud iam service-accounts create
command:gcloud iam service-accounts create SERVICE_ACCOUNT_NAME \ --description="DESCRIPTION" \ --display-name="DISPLAY_NAME"
Replace the following values:
-
SERVICE_ACCOUNT_NAME
: the name of the service account -
DESCRIPTION
: an optional description of the service account -
DISPLAY_NAME
: a service account name to display in the Trusted Cloud console
-
-
Optional: To grant your service account an IAM role on your project, run the
gcloud projects add-iam-policy-binding
command:gcloud projects add-iam-policy-binding PROJECT_ID \ --member="serviceAccount:SERVICE_ACCOUNT_NAME@PROJECT_ID.s3ns-system.iam.gserviceaccount.com" \ --role="ROLE_NAME"
Replace the following values:
-
PROJECT_ID
: the project ID -
SERVICE_ACCOUNT_NAME
: the name of the service account -
ROLE_NAME
: a role name, such asroles/compute.osLogin
-
-
Optional: To allow users to attach the service account to other resources, run the
gcloud iam service-accounts add-iam-policy-binding
command to grant a user the Service Account User role (roles/iam.serviceAccountUser
) on the service account:gcloud iam service-accounts add-iam-policy-binding \ SERVICE_ACCOUNT_NAME@PROJECT_ID.s3ns-system.iam.gserviceaccount.com \ --member="user:USER_EMAIL" \ --role="roles/iam.serviceAccountUser"
Replace the following values:
PROJECT_ID
: the project IDSERVICE_ACCOUNT_NAME
: the name of the service accountUSER_EMAIL
: the email address for the user
C++
To learn how to install and use the client library for IAM, see IAM client libraries. For more information, see the IAM C++ API reference documentation.
To authenticate to IAM, set up Application Default Credentials. For more information, see Before you begin.
Before running code samples, set the GOOGLE_CLOUD_UNIVERSE_DOMAIN
environment
variable to s3nsapis.fr
.
C#
To learn how to install and use the client library for IAM, see IAM client libraries. For more information, see the IAM C# API reference documentation.
To authenticate to IAM, set up Application Default Credentials. For more information, see Before you begin.
Before running code samples, set the GOOGLE_CLOUD_UNIVERSE_DOMAIN
environment
variable to s3nsapis.fr
.
Go
To learn how to install and use the client library for IAM, see IAM client libraries. For more information, see the IAM Go API reference documentation.
To authenticate to IAM, set up Application Default Credentials. For more information, see Before you begin.
Before running code samples, set the GOOGLE_CLOUD_UNIVERSE_DOMAIN
environment
variable to s3nsapis.fr
.
Java
To learn how to install and use the client library for IAM, see IAM client libraries. For more information, see the IAM Java API reference documentation.
To authenticate to IAM, set up Application Default Credentials. For more information, see Before you begin.
Before running code samples, set the GOOGLE_CLOUD_UNIVERSE_DOMAIN
environment
variable to s3nsapis.fr
.
Python
To learn how to install and use the client library for IAM, see IAM client libraries. For more information, see the IAM Python API reference documentation.
To authenticate to IAM, set up Application Default Credentials. For more information, see Before you begin.
Before running code samples, set the GOOGLE_CLOUD_UNIVERSE_DOMAIN
environment
variable to s3nsapis.fr
.
REST
The
serviceAccounts.create
method creates a service account.
Before using any of the request data, make the following replacements:
PROJECT_ID
: Your Trusted Cloud project ID. Project IDs are alphanumeric strings, likemy-project
.SA_NAME
: The alphanumeric ID of your service account. This name must be between 6 and 30 characters, and can contain lowercase alphanumeric characters and dashes.SA_DESCRIPTION
: Optional. A description for the service account.SA_DISPLAY_NAME
: A human-readable name for the service account.
HTTP method and URL:
POST https://iam.googleapis.com/v1/projects/PROJECT_ID/serviceAccounts
Request JSON body:
{ "accountId": "SA_NAME", "serviceAccount": { "description": "SA_DESCRIPTION", "displayName": "SA_DISPLAY_NAME" } }
To send your request, expand one of these options:
You should receive a JSON response similar to the following:
{ "name": "projects/my-project/serviceAccounts/my-service-account@my-project.s3ns-system.iam.gserviceaccount.com", "projectId": "my-project", "uniqueId": "123456789012345678901", "email": "my-service-account@my-project.s3ns-system.iam.gserviceaccount.com", "displayName": "My service account", "etag": "BwUp3rVlzes=", "description": "A service account for running jobs in my project", "oauth2ClientId": "987654321098765432109" }
After you create a service account, grant one or more roles to the service account so that it can act on your behalf.
Also, if the service account needs to access resources in other projects, you usually must enable the APIs for those resources in the project where you created the service account.
What's next
- Learn how to list and edit service accounts.
- Review the process for granting IAM roles to all types of principals, including service accounts.
- Understand how to attach service accounts to resources.