To effectively manage and modify log sinks in Google Cloud BigQuery, having the appropriate permissions is crucial. Despite adding necessary roles to your service account, including roles/logging.admin and roles/bigquery.dataOwner, you encounter issues where logs do not appear in the BigQuery dataset. This issue persists even though the log sink configuration and roles setup are seemingly correct.
A common observation in such cases is the difference in the writer_identity service account shown in the sink details compared to the service account used in the log sink configuration. The writer_identity is automatically managed by Google Cloud and should not be explicitly specified in the Terraform configuration. Instead, the focus should be on ensuring this writer_identity has the correct permissions to write to the BigQuery dataset.
First, verify the sink details to ensure that the destination and filter are correctly configured. This can be done in the Google Cloud Console under Logging > Logs Router. Next, ensure that the writer_identity has the necessary roles/bigquery.dataEditor role on the dataset. This can be accomplished with the following gcloud command:
gcloud projects add-iam-policy-binding your-project-id \
--member="serviceAccount:service-<sink-writer-identity>@logging-<project-id>.iam.gserviceaccount.com" \
--role="roles/bigquery.dataEditor"
Double-check the BigQuery dataset permissions to confirm that the writer_identity has the correct access. You can use the bq show command to verify this:
bq show --format=prettyjson your-project-id:your-dataset-id
Ensure the Terraform configuration does not explicitly specify writer_identity:
resource "google_logging_project_sink" "my_sink" {
name = "my-sink"
destination = "bigquery.googleapis.com/projects/your-project-id/datasets/your-dataset"
filter = "logName:\"projects/your-project-id/logs/cloudaudit.googleapis.com%2Factivity\""
bigquery_options {
use_partitioned_tables = true
}
}
Test the logs filter to ensure it accurately captures logs from Cloud Scheduler jobs. Use the Logs Explorer to verify the filter matches the expected logs:
resource.type="cloud_scheduler_job" logName="projects/your-project-id/logs/cloudaudit.googleapis.com%2Factivity"
Generate a test log entry from a Cloud Scheduler job to ensure it appears in the Logs Explorer and check the status of your sink in Logging > Logs Router. Additionally, view metrics related to your log sink in Cloud Monitoring to identify any issues with log ingestion or routing.
If issues persist, consider recreating the sink by deleting the existing one and recreating it using Terraform:
terraform destroy -target=google_logging_project_sink.my_sink
terraform apply
To verify if logs are being ingested in BigQuery, run a simple SQL query:
SELECT *
FROM `your-project-id.your-dataset.your-table`
ORDER BY timestamp DESC
LIMIT 100;
This query should return recent log entries if the sink is functioning correctly.