Monitor usage patterns for service accounts and keys Stay organized with collections Save and categorize content based on your preferences.
This page explains how to use Cloud Monitoring to view usage metrics for yourservice accounts and service account keys. These metrics let you view and trackusage patterns, which can help you identify anomalies, either automatically ormanually.
Note: To quickly identify the dates of only the most recent serviceaccount and key usages, seeView recent usage for service accounts andkeys.Service accounts and service account keys appear in these metrics if they areused to call any Google API, including APIs that are not part of Google Cloud.The metrics include both successful and failed API calls. For example, if an APIcall fails because the caller is not authorized to call that API, or because therequest referred to a resource that does not exist, the service account or keythat was used for that API call appears in the metrics.
Service account keys also appear in these metrics if a system lists the keyswhile attempting to authenticate a request, even if the system doesn't usethe key to authenticate the request. This behavior ismost common when usingsigned URLs for Cloud Storageor when authenticating to third-party applications. As aresult it is possible to see usage metrics for keys that have not been used forauthentication.
The following don't appear in either service account or service account keymetrics:
- Cloud Storage HMAC authentication keys
- Requests authenticated byAPI keys bound to service accounts
Monitoring retains service account metrics for6 weeks. Ifyou need to access data for a longer time period, you can periodically exportthe results to BigQuery. For more information, seeMonitoring metric export in the Solutionsdocumentation.
After you use a service account or service account key, usage metrics areusually available within a few minutes.
Before you begin
Enable the IAM and Cloud Monitoring APIs.
Roles required to enable APIs
To enable APIs, you need the Service Usage Admin IAM role (
roles/serviceusage.serviceUsageAdmin), which contains theserviceusage.services.enablepermission.Learn how to grant roles.
Required roles
To get the permissions that you need to view recent usage for service accounts and keys, ask your administrator to grant you theMonitoring Viewer (roles/monitoring.viewer) 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.
View usage metrics for all service accounts or keys
To view the usage metrics for your service accounts or service account keys,follow these steps:
Console
To view the metrics for a monitored resource by using theMetrics Explorer, do the following:
In the Google Cloud console, go to theleaderboard Metrics explorer page:
If you use the search bar to find this page, then select the result whose subheading isMonitoring.
- In the toolbar of the Google Cloud console, select your Google Cloud project. ForApp Hub configurations, select the App Hub host project or the app-enabled folder's management project.
- In theMetric element, expand theSelect a metric menu, enter
IAM Service Accountin the filter bar, and then use the submenus to select a specific resource type and metric:- In theActive resources menu, selectIAM Service Account.
- In theActive metric categories menu, selectService_account.
- In theActive metrics menu, select a service account metric. The following metrics are available within your selected time interval:
- For service account usage metrics, selectService account authentication events.
- For service account key usage metrics, selectService account key authentication events.
- ClickApply.
To add filters, which remove time series from the query results, use theFilter element.
To combine time series, use the menus on theAggregation element. For example, to display the CPU utilization for your VMs, based on their zone, set the first menu toMean and the second menu tozone.
All time series are displayed when the first menu of theAggregation element is set toUnaggregated. The default settings for theAggregation element are determined by the metric type you selected.
- For quota and other metrics that report one sample per day, do the following:
- In theDisplay pane, set theWidget type toStacked bar chart.
- Set the time period to at least one week.
REST
The Cloud Monitoring API API'stimeSeries.list method allows you to access usage metrics programmatically.
Before using any of the request data, make the following replacements:
PROJECT_ID: Your Google Cloud projectID. Project IDs are alphanumeric strings, likemy-project.METRIC_TYPE: The type of metric you want to check. Use one of the following values:- For service account usage metrics, use
iam.googleapis.com%2Fservice_account%2Fauthn_events_count. - For service account key usage metrics, use
iam.googleapis.com%2Fservice_account%2Fkey%2Fauthn_events_count.
- For service account usage metrics, use
END_TIME: The end of the time intervalthat you want to check, in percent-encodedRFC 3339 format. For example,2020-06-12T00%3A00%3A00.00Z.START_TIME: The start of the time intervalthat you want to check, in percent-encodedRFC 3339 format. For example,2020-04-12T00%3A00%3A00.00Z.
Note: If you are sending the request using the API Explorer, do not use percent-encoded values.
HTTP method and URL:
GET https://monitoring.googleapis.com/v3/projects/PROJECT_ID/timeSeries?filter=metric.type%3D%22METRIC_TYPE%22&interval.endTime=END_TIME&interval.startTime=START_TIME
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.Execute the following command:
curl -X GET \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
"https://monitoring.googleapis.com/v3/projects/PROJECT_ID/timeSeries?filter=metric.type%3D%22METRIC_TYPE%22&interval.endTime=END_TIME&interval.startTime=START_TIME"
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.Execute the following command:
$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }
Invoke-WebRequest `
-Method GET `
-Headers $headers `
-Uri "https://monitoring.googleapis.com/v3/projects/PROJECT_ID/timeSeries?filter=metric.type%3D%22METRIC_TYPE%22&interval.endTime=END_TIME&interval.startTime=START_TIME" | Select-Object -Expand Content
APIs Explorer (browser)
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. Complete any required fields and clickExecute.
For more information about programmatically reading usage metrics, seeReading metric data in the Monitoring documentation.
Note: Service account metrics include disabled service accounts. Service accountkey metricsdo not include disabled service account keys, but they mightinclude expired or deleted service account keys.View usage metrics for a single service account
To view usage metrics for a single service account, follow these steps:
Console
In the Google Cloud console, go to theService Accounts page.
Select the project that contains your service account.
Click the email address of your service account.
Click theMetrics tab. TheAuthentication traffic chart showsthe usage metrics for the service account.
Optional: To view the chart on theMetrics explorer page, which offersadditional filtering and viewing options, click >View in Metrics Explorer.
REST
The Cloud Monitoring API'stimeSeries.list method , when used with specific filters, allows you to get usage metrics fora single service account. You can then use those metrics to determine when the account was lastused.
Before using any of the request data, make the following replacements:
PROJECT_ID: Your Google Cloud projectID. Project IDs are alphanumeric strings, likemy-project.SERVICE_ACCOUNT_ID: The unique numeric ID of your service account. To find your service account's unique numeric ID, follow these steps:In the Google Cloud console, go to theService Accounts page.
Go to the Service Accounts page- Click the email address of your service account. Your service account's unique numeric ID is the value in theUnique ID field.
END_TIME: The end of the time intervalthat you want to check, in percent-encodedRFC 3339 format. For example,2020-06-12T00%3A00%3A00.00Z.START_TIME: The start of the time intervalthat you want to check, in percent-encodedRFC 3339 format. For example,2020-04-12T00%3A00%3A00.00Z.
Note: If you are sending the request using the API Explorer, do not use percent-encoded values.
HTTP method and URL:
GET https://monitoring.googleapis.com/v3/projects/PROJECT_ID/timeSeries?filter=metric.type%3D%22iam.googleapis.com%2Fservice_account%2Fauthn_events_count%22%20AND%20resource.labels.unique_id%3D%22SERVICE_ACCOUNT_ID%22&interval.endTime=END_TIME&interval.startTime=START_TIME
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.Execute the following command:
curl -X GET \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
"https://monitoring.googleapis.com/v3/projects/PROJECT_ID/timeSeries?filter=metric.type%3D%22iam.googleapis.com%2Fservice_account%2Fauthn_events_count%22%20AND%20resource.labels.unique_id%3D%22SERVICE_ACCOUNT_ID%22&interval.endTime=END_TIME&interval.startTime=START_TIME"
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.Execute the following command:
$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }
Invoke-WebRequest `
-Method GET `
-Headers $headers `
-Uri "https://monitoring.googleapis.com/v3/projects/PROJECT_ID/timeSeries?filter=metric.type%3D%22iam.googleapis.com%2Fservice_account%2Fauthn_events_count%22%20AND%20resource.labels.unique_id%3D%22SERVICE_ACCOUNT_ID%22&interval.endTime=END_TIME&interval.startTime=START_TIME" | Select-Object -Expand Content
APIs Explorer (browser)
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. Complete any required fields and clickExecute.
The response contains atimeSeries object with all of the recent authentication events for the specified service account.
View usage metrics for a single service account key
To view usage metrics for a single service account key, follow these steps:
Console
In the Google Cloud console, go to theService Accounts page.
Select the project that contains the service account associated withyour key.
Click the email address of the service account associated with your key.
Click theMetrics tab. TheAuthentication traffic per key chartshows usage metrics for all keys associated with the service account.
In the chart legend, click the ID of the service account key that you want toview usage metrics for. The chart updates to show metrics for only thatservice account key.
Optional: To view the chart on theMetrics explorer page, which offersadditional filtering and viewing options, click >View in Metrics Explorer.
REST
First, get the service account key's ID.
List the service account keys:
The
projects.serviceAccounts.keys.listmethod lists all of the service account keys for 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 name of the service account whose keys you want to list.KEY_TYPES: Optional. A comma-separated list of key types that you want to include in the response. The key type indicates whether a key is user-managed (USER_MANAGED) or system-managed (SYSTEM_MANAGED). If left blank, all keys are returned.
HTTP method and URL:
GET https://iam.googleapis.com/v1/projects/PROJECT_ID/serviceAccounts/SA_NAME@PROJECT_ID.iam.gserviceaccount.com/keys?keyTypes=KEY_TYPES
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 thegcloudCLI with your user account by runninggcloud initorgcloud auth login, or by usingCloud Shell, which automatically logs you into thegcloudCLI . You can check the currently active account by runninggcloud auth list.Execute the following command:
curl -X GET \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
"https://iam.googleapis.com/v1/projects/PROJECT_ID/serviceAccounts/SA_NAME@PROJECT_ID.iam.gserviceaccount.com/keys?keyTypes=KEY_TYPES"PowerShell (Windows)
Note: The following command assumes that you have logged in to thegcloudCLI with your user account by runninggcloud initorgcloud auth login. You can check the currently active account by runninggcloud auth list.Execute the following command:
$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }
Invoke-WebRequest `
-Method GET `
-Headers $headers `
-Uri "https://iam.googleapis.com/v1/projects/PROJECT_ID/serviceAccounts/SA_NAME@PROJECT_ID.iam.gserviceaccount.com/keys?keyTypes=KEY_TYPES" | Select-Object -Expand ContentAPIs Explorer (browser)
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. Complete any required fields and clickExecute.
You should receive a JSON response similar to the following:
{ "keys": [ { "name": "projects/my-project/serviceAccounts/my-service-account@my-project.iam.gserviceaccount.com/keys/90c48f61c65cd56224a12ab18e6ee9ca9c3aee7c", "validAfterTime": "2020-03-04T17:39:47Z", "validBeforeTime": "9999-12-31T23:59:59Z", "keyAlgorithm": "KEY_ALG_RSA_2048", "keyOrigin": "GOOGLE_PROVIDED", "keyType": "USER_MANAGED" }, { "name": "projects/my-project/serviceAccounts/my-service-account@my-project.iam.gserviceaccount.com/keys/e5e3800831ac1adc8a5849da7d827b4724b1fce8", "validAfterTime": "2020-03-31T23:50:09Z", "validBeforeTime": "9999-12-31T23:59:59Z", "keyAlgorithm": "KEY_ALG_RSA_2048", "keyOrigin": "GOOGLE_PROVIDED", "keyType": "USER_MANAGED" }, { "name": "projects/my-project/serviceAccounts/my-service-account@my-project.iam.gserviceaccount.com/keys/b97699f042b8eee6a846f4f96259fbcd13e2682e", "validAfterTime": "2020-05-17T18:58:13Z", "validBeforeTime": "9999-12-31T23:59:59Z", "keyAlgorithm": "KEY_ALG_RSA_2048", "keyOrigin": "GOOGLE_PROVIDED", "keyType": "USER_MANAGED", "disabled": true "disable_reason": "SERVICE_ACCOUNT_KEY_DISABLE_REASON_EXPOSED" "extended_status": "SERVICE_ACCOUNT_KEY_EXTENDED_STATUS_KEY_EXPOSED" "extended_status_message": "exposed at: https://www.github.com/SomePublicRepo" } ]}Use the metadata in the response to identify the key you want to track.Then, copy the key's unique ID from the end of the
namefield.The
namefield has the following format:"name":"projects/PROJECT_ID/serviceAccounts/SERVICE_ACCOUNT_EMAIL/keys/KEY_ID"
The key's unique ID is everything after
keys/.For example, the unique ID in the following key name is
0f561cc41650ff521899de2fd653bd3de08e2da4:"name":"projects/my-project/serviceAccounts/my-account@my-project.iam.gserviceaccount.com/keys/0f561cc41650ff521899de2fd653bd3de08e2da4"
Then, use the ID to view usage metrics for the service account key.
The Cloud Monitoring API'stimeSeries.list method , when used with specific filters, allows you to get usage metrics fora single service account key. You can then use those metrics to determine when the key was lastused.
Before using any of the request data, make the following replacements:
PROJECT_ID: Your Google Cloud projectID. Project IDs are alphanumeric strings, likemy-project.KEY_ID: The unique ID of your service account key.END_TIME: The end of the time intervalthat you want to check, in percent-encodedRFC 3339 format. For example,2020-06-12T00%3A00%3A00.00Z.START_TIME: The start of the time intervalthat you want to check, in percent-encodedRFC 3339 format. For example,2020-04-12T00%3A00%3A00.00Z.
Note: If you are sending the request using the API Explorer, do not use percent-encoded values.
HTTP method and URL:
GET https://monitoring.googleapis.com/v3/projects/PROJECT_ID/timeSeries?filter=metric.type%3D%22iam.googleapis.com%2Fservice_account%2Fkey%2Fauthn_events_count%22%20AND%20metric.labels.key_id%3D%22KEY_ID%22&interval.endTime=END_TIME&interval.startTime=START_TIME
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.Execute the following command:
curl -X GET \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
"https://monitoring.googleapis.com/v3/projects/PROJECT_ID/timeSeries?filter=metric.type%3D%22iam.googleapis.com%2Fservice_account%2Fkey%2Fauthn_events_count%22%20AND%20metric.labels.key_id%3D%22KEY_ID%22&interval.endTime=END_TIME&interval.startTime=START_TIME"
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.Execute the following command:
$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }
Invoke-WebRequest `
-Method GET `
-Headers $headers `
-Uri "https://monitoring.googleapis.com/v3/projects/PROJECT_ID/timeSeries?filter=metric.type%3D%22iam.googleapis.com%2Fservice_account%2Fkey%2Fauthn_events_count%22%20AND%20metric.labels.key_id%3D%22KEY_ID%22&interval.endTime=END_TIME&interval.startTime=START_TIME" | Select-Object -Expand Content
APIs Explorer (browser)
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. Complete any required fields and clickExecute.
The response contains atimeSeries object with all of the recent authentication events for the specified service account key.
Export metrics
You can use Monitoring to export your metricsto BigQuery. Exporting metrics is useful for performinglong-term analysis because Monitoring only retains metrics for alimited time.
For instructions, seeMonitoring metric exportin the Solutions documentation.
What's next
- Discover how toexport metric data to BigQuery.
- Use Activity Analyzer toview only the most recent authenticationevents for your service accounts and keys.
- Useservice account insights to identify service accounts thathave not been used in the past 90 days.
- Learn how todisable service accounts ordelete service accounts.
- Learn how todelete service account keys.
- Explore the features offered byMonitoring.
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-15 UTC.