Overview and What You Will Learn
Cloud Audit Logs record who did what, where, and when in Google Cloud, and they are the only reliable source for answering questions about administrative actions. In this lab you will generate Admin Activity logs, enable Data Access logging for Cloud Storage, and run the queries an investigator uses to trace a VM deletion, an object read, and an IAM change.
By the end you will be able to:
- Name the four audit log types and say which are on by default.
- Enable Data Access logs through the IAM policy and verify they work.
- Write
gcloud logging readfilters that identify the person or service account behind an action. - Design retention so evidence still exists months later.
Why This Matters in Production
When a storage bucket turns up public or a database disappears, the first question is "who did this and when". Application logs cannot answer it, because they never see actions taken in the Console, gcloud, or the API. Imagine a payments platform that finds a bucket was made public on a Friday evening: with audit logs the team has the principal, the exact method, and the timestamp in minutes. Without Data Access logs enabled beforehand, they can never learn what was read from it.
Core Principles
Cloud Audit Logs have four types, and knowing which one holds the evidence matters as much as knowing audit logs exist.
| Log type | Records | Default |
|---|---|---|
| Admin Activity | Configuration and IAM changes | Always on, cannot be disabled |
| Data Access | Reads and writes of user data | Off for most services, on for BigQuery |
| System Event | Changes Google makes itself, such as live migration | Always on |
| Policy Denied | Requests blocked by a security policy | On by default |
Admin Activity and System Event logs go to the _Required bucket and are kept 400 days. Data Access and Policy Denied logs go to _Default, which keeps 30 days unless you change it or export them.
400 days)] L -->|Data access| D[(_Default bucket
30 days)] L -->|Sink| X[(Locked archive
separate project)] classDef actor fill:#DBEAFE,color:#1E293B classDef svc fill:#326CE5,color:#FFFFFF classDef store fill:#10B981,color:#FFFFFF class A actor class G,L svc class R,D,X store
Audit entries live under log names such as cloudaudit.googleapis.com%2Factivity and cloudaudit.googleapis.com%2Fdata_access. The %2F is a URL-encoded slash, and forgetting it is the most common reason a query returns nothing.
Detailed Step-by-Step Practical Lab
You need gcloud, a billing account ID, and permission to create projects.
Step 1: Create a project and enable the services
export BILLING_ACCOUNT=0X0X0X-0X0X0X-0X0X0X gcloud projects create gcp-audit-lab-2026 --name="Audit Logs Lab"gcloud billing projects link gcp-audit-lab-2026 --billing-account=$BILLING_ACCOUNTgcloud config set project gcp-audit-lab-2026gcloud services enable compute.googleapis.com storage.googleapis.comStep 2: Perform an administrative action Creating a VM writes an Admin Activity entry automatically.
gcloud compute instances create vm-audit-test \ --zone=asia-south1-a \ --machine-type=e2-small \ --image-family=debian-12 \ --image-project=debian-cloudStep 3: Find who created the VM Entries can take a minute to appear.
gcloud logging read \ 'logName="projects/gcp-audit-lab-2026/logs/cloudaudit.googleapis.com%2Factivity" AND protoPayload.methodName="v1.compute.instances.insert"' \ --limit=5 \ --format="table(timestamp,protoPayload.authenticationInfo.principalEmail,protoPayload.methodName,protoPayload.resourceName)"The principalEmail column answers "who", and timestamp answers "when".
Step 4: Enable Data Access logs for Cloud Storage
Data Access logging lives in the project's IAM policy under auditConfigs. Download the policy, add the config, and apply it.
gcloud projects get-iam-policy gcp-audit-lab-2026 --format=yaml > policy.yaml grep -c auditConfigs policy.yaml || true cat >> policy.yaml << 'AUDITEOF'auditConfigs:- service: storage.googleapis.com auditLogConfigs: - logType: ADMIN_READ - logType: DATA_READ - logType: DATA_WRITEAUDITEOF gcloud projects set-iam-policy gcp-audit-lab-2026 policy.yamlCommon MistakeAppending a second
auditConfigsblock to a policy that already has one. YAML keys must be unique, so merge your service into the existing block instead. Also remember thatset-iam-policyreplaces the whole policy, so always start from a fresh download to keep theetag.
Step 5: Generate data access events
gcloud storage buckets create gs://gcp-audit-lab-2026-bucket --location=asia-south1echo "order-1001 paid 4599.00" > test-file.txtgcloud storage cp test-file.txt gs://gcp-audit-lab-2026-bucket/gcloud storage cat gs://gcp-audit-lab-2026-bucket/test-file.txtStep 6: Find who read and wrote the object Reading Data Access logs needs the Private Logs Viewer role, which project owners already have.
gcloud logging read \ 'logName="projects/gcp-audit-lab-2026/logs/cloudaudit.googleapis.com%2Fdata_access" AND (protoPayload.methodName="storage.objects.get" OR protoPayload.methodName="storage.objects.create")' \ --limit=10 \ --format="table(timestamp,protoPayload.authenticationInfo.principalEmail,protoPayload.methodName,protoPayload.resourceName)"You should see one storage.objects.create for the upload and one storage.objects.get for the read.
Step 7: Trace an IAM change Make a harmless IAM change, then find it the way you would during an incident.
ME=$(gcloud config get-value account)gcloud projects add-iam-policy-binding gcp-audit-lab-2026 \ --member="user:$ME" --role="roles/logging.viewer"gcloud projects remove-iam-policy-binding gcp-audit-lab-2026 \ --member="user:$ME" --role="roles/logging.viewer" gcloud logging read \ 'protoPayload.methodName="SetIamPolicy" AND protoPayload.serviceName="cloudresourcemanager.googleapis.com"' \ --freshness=1d \ --format="table(timestamp,protoPayload.authenticationInfo.principalEmail,protoPayload.resourceName)"Step 8: Trace a deletion
gcloud compute instances delete vm-audit-test --zone=asia-south1-a --quiet gcloud logging read \ 'protoPayload.methodName="v1.compute.instances.delete"' \ --freshness=1d \ --format="table(timestamp,protoPayload.authenticationInfo.principalEmail,protoPayload.resourceName)"Step 9: Look at System Event logs These record Google-initiated actions and may be empty in a short lab.
gcloud logging read \ 'logName="projects/gcp-audit-lab-2026/logs/cloudaudit.googleapis.com%2Fsystem_event"' \ --limit=5Step 10: Clean up
gcloud storage rm --recursive gs://gcp-audit-lab-2026-bucketrm -f policy.yaml test-file.txtgcloud projects delete gcp-audit-lab-2026 --quietProduction Best Practices & Common Pitfalls
- Enable Data Access logs before you need them. Logs exist only from the moment logging is enabled, and no one can recover earlier history.
- Export audit logs to a separate, locked-down project. Someone with admin rights in the audited project could otherwise delete the trail of their own actions. An organization-level aggregated sink is the usual pattern:
gcloud logging sinks create audit-archive \ storage.googleapis.com/devopsnet-audit-archive \ --organization=123456789012 --include-children \ --log-filter='logName:"cloudaudit.googleapis.com"'- Match retention to compliance. The default 30 days for Data Access logs is rarely enough for a regulated workload, so raise bucket retention or export to long-term storage.
- Control cost. Data Access logs can be very large for chatty services, so enable them for sensitive datasets and use exemptions for noisy service accounts.
- Alert on high-risk methods. Create log-based alerts for
SetIamPolicy, firewall changes, and audit config changes.
Common MistakeAssuming Data Access logs exist by default the way Admin Activity logs do. During an incident the team discovers that nobody recorded who read the data, and that evidence cannot be recreated.
SecurityTreat Data Access logs as sensitive. They can reveal resource names and who touched them, so restrict
roles/logging.privateLogViewerto the security team.
Quick Reference & Troubleshooting Commands
| Command | Description |
|---|---|
gcloud logging read 'logName=".../cloudaudit.googleapis.com%2Factivity"' |
Query Admin Activity logs |
gcloud logging read 'logName=".../cloudaudit.googleapis.com%2Fdata_access"' |
Query Data Access logs |
gcloud logging read 'protoPayload.methodName="SetIamPolicy"' |
Find IAM policy changes |
gcloud projects get-iam-policy PROJECT --format=yaml |
View the policy, including auditConfigs |
gcloud projects set-iam-policy PROJECT policy.yaml |
Apply an edited policy |
| Symptom | Likely fix |
|---|---|
| Query returns nothing | Wait a minute, and check the %2F in the log name and the project ID |
| No Data Access entries | Confirm auditConfigs is in the policy and the events happened after it was applied |
| Permission denied on Data Access logs | Grant roles/logging.privateLogViewer |
set-iam-policy fails on etag |
Download a fresh policy, reapply your edit, and set again |