Create service accounts

This page explains how to create service accounts using theIdentity and Access Management (IAM) API, the Google Cloud console, and thegcloud command-line tool.

By default, each project can have up to 100 serviceaccounts that control access to your resources. You can request a quota increaseif necessary.Learn more about quotas and limits.

Before you begin

  • Enable the IAM API.

    Roles required to enable APIs

    To enable APIs, you need the Service Usage Admin IAM role (roles/serviceusage.serviceUsageAdmin), which contains theserviceusage.services.enable permission.Learn how to grant roles.

    Enable the API

  • Set up authentication.

    Select the tab for how you plan to use the samples on this page:

    Console

    When you use the Google Cloud console to access Google Cloud services and APIs, you don't need to set up authentication.

    gcloud

    In the Google Cloud console, activate Cloud Shell.

    Activate Cloud Shell

    At the bottom of the Google Cloud console, aCloud 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.

    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.

      If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.

      If you're using a local shell, then create local authentication credentials for your user account:

      gcloudauthapplication-defaultlogin

      You don't need to do this if you're using Cloud Shell.

      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 Google 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.

      If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.

      If you're using a local shell, then create local authentication credentials for your user account:

      gcloudauthapplication-defaultlogin

      You don't need to do this if you're using Cloud Shell.

      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 Google 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.

      If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.

      If you're using a local shell, then create local authentication credentials for your user account:

      gcloudauthapplication-defaultlogin

      You don't need to do this if you're using Cloud Shell.

      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 Google 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.

      If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.

      If you're using a local shell, then create local authentication credentials for your user account:

      gcloudauthapplication-defaultlogin

      You don't need to do this if you're using Cloud Shell.

      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 Google 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.

      If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.

      If you're using a local shell, then create local authentication credentials for your user account:

      gcloudauthapplication-defaultlogin

      You don't need to do this if you're using Cloud Shell.

      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 Google 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.

      Install the Google Cloud CLI.

      If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.

    For more information, seeAuthenticate for using REST in the Google Cloud authentication documentation.

  • UnderstandIAM service accounts

Required roles

To get the permissions that you need to create service accounts, ask your administrator to grant you theCreate Service Accounts (roles/iam.serviceAccountCreator) IAM role on the project. For more information about granting roles, seeManage access to projects, folders, and organizations.

You might also be able to get the required permissions throughcustom roles or otherpredefined roles.

Note:IAM basic roles might also contain permissions to create service accounts. You shouldn't grant basic roles in a production environment, but you can grant them in a development or test environment.

If you want to grant newly created service accounts access to your project, youalso 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 asmy-service-account. The ID must be between 6 and 30 characters, and cancontain lowercase alphanumeric characters and dashes. After you create a serviceaccount, you cannot change its name.

The service account's name appears in the email address that is provisionedduring creation, in the formatSERVICE_ACCOUNT_NAME@PROJECT_ID.iam.gserviceaccount.com.

Each service account also has a permanent, unique numeric ID, which is generatedautomatically.

You also provide the following information when you create a service account:

  • DESCRIPTION is an optional description for theservice account.
  • DISPLAY_NAME is a friendly name for the serviceaccount.
  • PROJECT_ID is the ID of your Google Cloud project.

After you create a service account, you might need to wait for60 seconds or more before you use the service account. This behavioroccurs because read operations are eventually consistent; it can take time forthe new service account to become visible. If you try to read or use a serviceaccount immediately after you create it, and you receive an error, you canretry the request with exponential backoff.

Console

  1. In the Google Cloud console, go to theCreate service account page.

    Go to Create service account

    The remaining steps appear in the Google Cloud console.

  2. Select a Google Cloud project.
  3. Enter a service account name to display in the Google Cloud console.

    The Google Cloud console generates a service account ID based on this name. Edit the ID if necessary. You cannot change the ID later.

  4. Optional: Enter a description of the service account.
  5. If you don't want to set access controls now, clickDone to finish creating the service account. To set access controls now, clickCreate and continue and continue to the next step.
  6. Optional: Choose one or moreIAM roles to grant to the service account on the project.
  7. When you are done adding roles, clickContinue.
  8. Optional: In theService account users role field, add members that need toattach the service account to other resources.
  9. Optional: In theService account admins role field, add members that need to manage the service account.
  10. ClickDone to finish creating the service account.

gcloud

  1. In the Google Cloud console, activate Cloud Shell.

    Activate Cloud Shell

    At the bottom of the Google Cloud console, aCloud 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.

  2. To create the service account, run thegcloud iam service-accounts create command:

    gcloudiamservice-accountscreateSERVICE_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 Google Cloud console

  3. Optional: To grant your service account anIAM role on your project, run thegcloud projects add-iam-policy-binding command:

    gcloudprojectsadd-iam-policy-bindingPROJECT_ID\--member="serviceAccount:SERVICE_ACCOUNT_NAME@PROJECT_ID.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

  4. Optional: To allow users toattach the service account to other resources, run thegcloud iam service-accounts add-iam-policy-binding command to grant a user the Service Account User role (roles/iam.serviceAccountUser) on the service account:

    gcloudiamservice-accountsadd-iam-policy-binding\SERVICE_ACCOUNT_NAME@PROJECT_ID.iam.gserviceaccount.com\--member="user:USER_EMAIL"\--role="roles/iam.serviceAccountUser"

    Replace the following values:

    • PROJECT_ID: the project ID

    • SERVICE_ACCOUNT_NAME: the name of the service account

    • USER_EMAIL: the email address for the user

C++

To learn how to install and use the client library for IAM, seeIAM client libraries. For more information, see theIAMC++ API reference documentation.

To authenticate to IAM, set up Application Default Credentials. For more information, seeBefore you begin.

namespaceiam=::google::cloud::iam_admin_v1;[](std::stringconst&project_id,std::stringconst&account_id,std::stringconst&display_name,std::stringconst&description){iam::IAMClientclient(iam::MakeIAMConnection());google::iam::admin::v1::ServiceAccountservice_account;service_account.set_display_name(display_name);service_account.set_description(description);autoresponse=client.CreateServiceAccount("projects/"+project_id,account_id,service_account);if(!response)throwstd::move(response).status();std::cout <<"ServiceAccount successfully created: "            <<response->DebugString() <<"\n";}

C#

To learn how to install and use the client library for IAM, seeIAM client libraries. For more information, see theIAMC# API reference documentation.

To authenticate to IAM, set up Application Default Credentials. For more information, seeBefore you begin.

usingSystem;usingGoogle.Apis.Auth.OAuth2;usingGoogle.Apis.Iam.v1;usingGoogle.Apis.Iam.v1.Data;publicpartialclassServiceAccounts{publicstaticServiceAccountCreateServiceAccount(stringprojectId,stringname,stringdisplayName){varcredential=GoogleCredential.GetApplicationDefault().CreateScoped(IamService.Scope.CloudPlatform);varservice=newIamService(newIamService.Initializer{HttpClientInitializer=credential});varrequest=newCreateServiceAccountRequest{AccountId=name,ServiceAccount=newServiceAccount{DisplayName=displayName}};varserviceAccount=service.Projects.ServiceAccounts.Create(request,"projects/"+projectId).Execute();Console.WriteLine("Created service account: "+serviceAccount.Email);returnserviceAccount;}}

Go

To learn how to install and use the client library for IAM, seeIAM client libraries. For more information, see theIAMGo API reference documentation.

To authenticate to IAM, set up Application Default Credentials. For more information, seeBefore you begin.

import("context""fmt""io"iam"google.golang.org/api/iam/v1")// createServiceAccount creates a service account.funccreateServiceAccount(wio.Writer,projectID,name,displayNamestring)(*iam.ServiceAccount,error){ctx:=context.Background()service,err:=iam.NewService(ctx)iferr!=nil{returnnil,fmt.Errorf("iam.NewService: %w",err)}request:=&iam.CreateServiceAccountRequest{AccountId:name,ServiceAccount:&iam.ServiceAccount{DisplayName:displayName,},}account,err:=service.Projects.ServiceAccounts.Create("projects/"+projectID,request).Do()iferr!=nil{returnnil,fmt.Errorf("Projects.ServiceAccounts.Create: %w",err)}fmt.Fprintf(w,"Created service account: %v",account)returnaccount,nil}

Java

To learn how to install and use the client library for IAM, seeIAM client libraries. For more information, see theIAMJava API reference documentation.

To authenticate to IAM, set up Application Default Credentials. For more information, seeBefore you begin.

importcom.google.cloud.iam.admin.v1.IAMClient;importcom.google.iam.admin.v1.CreateServiceAccountRequest;importcom.google.iam.admin.v1.ProjectName;importcom.google.iam.admin.v1.ServiceAccount;importjava.io.IOException;publicclassCreateServiceAccount{publicstaticvoidmain(String[]args)throwsIOException{// TODO(developer): Replace the variables before running the sample.StringprojectId="your-project-id";StringserviceAccountName="my-service-account-name";createServiceAccount(projectId,serviceAccountName);}// Creates a service account.publicstaticServiceAccountcreateServiceAccount(StringprojectId,StringserviceAccountName)throwsIOException{ServiceAccountserviceAccount=ServiceAccount.newBuilder().setDisplayName("your-display-name").build();CreateServiceAccountRequestrequest=CreateServiceAccountRequest.newBuilder().setName(ProjectName.of(projectId).toString()).setAccountId(serviceAccountName).setServiceAccount(serviceAccount).build();// Initialize client that will be used to send requests.// This client only needs to be created once, and can be reused for multiple requests.try(IAMClientiamClient=IAMClient.create()){serviceAccount=iamClient.createServiceAccount(request);System.out.println("Created service account: "+serviceAccount.getEmail());}returnserviceAccount;}}

Python

To learn how to install and use the client library for IAM, seeIAM client libraries. For more information, see theIAMPython API reference documentation.

To authenticate to IAM, set up Application Default Credentials. For more information, seeBefore you begin.

fromtypingimportOptionalfromgoogle.cloudimportiam_admin_v1fromgoogle.cloud.iam_admin_v1importtypesdefcreate_service_account(project_id:str,account_id:str,display_name:Optional[str]=None)->types.ServiceAccount:"""Creates a service account.    project_id: ID or number of the Google Cloud project you want to use.    account_id: ID which will be unique identifier of the service account    display_name (optional): human-readable name, which will be assigned        to the service account    return: ServiceAccount    """iam_admin_client=iam_admin_v1.IAMClient()request=types.CreateServiceAccountRequest()request.account_id=account_idrequest.name=f"projects/{project_id}"service_account=types.ServiceAccount()service_account.display_name=display_namerequest.service_account=service_accountaccount=iam_admin_client.create_service_account(request=request)print(f"Created a service account:{account.email}")returnaccount

REST

TheserviceAccounts.create method creates a service account.

Before using any of the request data, make the following replacements:

  • PROJECT_ID: Your Google Cloud projectID. Project IDs are alphanumeric strings, likemy-project.
  • SA_NAME: The alphanumeric ID of yourservice account. This name must be between 6 and 30 characters, and can contain lowercasealphanumeric characters and dashes.
  • SA_DESCRIPTION: Optional. A description forthe service account.
  • SA_DISPLAY_NAME: A human-readablename 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:

curl (Linux, macOS, or Cloud Shell)

Note: The following command assumes that you have logged in to thegcloud CLI with your user account by runninggcloud init orgcloud auth login , or by usingCloud Shell, which automatically logs you into thegcloud CLI . You can check the currently active account by runninggcloud auth list.

Save the request body in a file namedrequest.json, and execute the following command:

curl -X POST \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json; charset=utf-8" \
-d @request.json \
"https://iam.googleapis.com/v1/projects/PROJECT_ID/serviceAccounts"

PowerShell (Windows)

Note: The following command assumes that you have logged in to thegcloud CLI with your user account by runninggcloud init orgcloud auth login . You can check the currently active account by runninggcloud auth list.

Save the request body in a file namedrequest.json, and execute the following command:

$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }

Invoke-WebRequest `
-Method POST `
-Headers $headers `
-ContentType: "application/json; charset=utf-8" `
-InFile request.json `
-Uri "https://iam.googleapis.com/v1/projects/PROJECT_ID/serviceAccounts" | Select-Object -Expand Content

APIs Explorer (browser)

Copy the request body and open themethod reference page. The APIs Explorer panel opens on the right side of the page. You can interact with this tool to send requests. Paste the request body in this tool, complete any other required fields, and clickExecute.

You should receive a JSON response similar to the following:

{  "name": "projects/my-project/serviceAccounts/my-service-account@my-project.iam.gserviceaccount.com",  "projectId": "my-project",  "uniqueId": "123456789012345678901",  "email": "my-service-account@my-project.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 accountso that it can act on your behalf.

Also, if the service account needs to access resources in other projects, youusually mustenable the APIs for those resources in the projectwhere you created the service account.

What's next

Try it for yourself

If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.

Get started for free

Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2025-12-09 UTC.