BLOG

Centralize Logs From Multiple Projects On Google Cloud Platform

1 njor dfm imr opqzwluiq

Table of contents

1 njor dfm imr opqzwluiq

At DoiT International, we work with a variety of software companies around the globe. We frequently receive requests to solve similar problems from multiple customers. Recently, I witnessed several cases where organizations wanted to ship their logs from multiple Google Cloud Platform (GCP) projects into a single project for centralized access and observation.

A lot of companies ship their logs to third-party providers like Datadog, Splunk, and others; but in this post, Iโ€™ll illustrate how to accomplish log-file unification with access control using GCPโ€™s Cloud Logging service (formerly Stackdriver) alone. The result is a simple and elegant solution to an emerging common need.

Architecture

1 oingcshhcvwcefmkilvh7a
Demo architecture overview

TL;DR

For this example, I will create two test projects and configure their logs to ship to a central project. As an added bonus, Iโ€™ll show how to centralize monitoring and metrics as well (another common use case).

  1. Create three test projects on GCP, mike-test-log-view, mike-test-log-a, and mike-test-log-b
  2. Create a logs bucket in project mike-test-log-view and copy the path to the bucket
  3. Create a log sink of type Cloud Logging bucket, pointing to the path copied in step 2 for both projects mike-test-log-a and mike-test-log-b
  4. View details for each log sink and copy the writer entity Service Account email address (dynamically created for each)
  5. Edit the IAM roles in project mike-test-log-view and add each Service Account copied from the log sinks, granting the Logs Bucket Writer role to ach
  6. Edit the IAM roles in project mike-test-log-view and add the role Logs View Accessor with condition pointing to the path of your logs bucket (to restrict access by user)
  7. View the Logs Explorer page and click โ€œRefine Scopeโ€ at the top, selecting โ€œScope by storageโ€ and selecting your logs bucket

Step Byย Step

The following steps illustrate how you can ship logs from multiple Google Cloud Platform projects to a single centralized project.

Step 1: Create testย projects

This step is self-explanatory. I created three projects as described above for demo purposes.

Step 2: Create logs bucket in viewย project

1 k45b9akfbqbdswicr 1eq
Visit Logs Storage and โ€œCreate Logs Bucketโ€ then copy the path to theย bucket

Step 3: Create log sinks in testย projects

Create the log sinks in the test projects a and b respectively.

1 zfbvygtmw4qq3fhkofzfua
Set sink destination to โ€œCloud Loggingย Bucketโ€
1 ltehbfet3hxpdllzpgfgpw
Select the โ€œUse a logs bucket in another projectโ€ย option
1 qlcuo8ue6pfrllsqngst6w
Log sink for test project โ€œaโ€, appending destination with a path to logs bucket (afterย domain)
1 ieke uhq jdf u73af9kqa
Log sink for test project โ€œbโ€, appending destination with a path to logs bucket (afterย domain)

Step 4: View details for log sinks and note IAM writerย identity

For each test project, in the list view of the โ€œLog Router Sinksโ€ page click the โ€œโ€ฆโ€ (3 dots) at the far right of the row corresponding to your new log sink and select โ€œView Detailsโ€.

1 zdpskpe vy8n9twqxaclmw

Copy the โ€œWriter identityโ€ which is the service account dynamically created with the log sink. You will add this to the view project to allow it to write log entries to your central logs bucket.

Step 5: Edit IAM roles in view project for log sinkย writers

For each log sink, in your central view project, visit the IAM administration page and add a member with the Logs Bucket Writer role using the copied โ€œWriter identityโ€ service account from step 4 as shown.

1 r91jqx8ysaj85hp9iwmnsq
Adding IAM roles to allow log sinks to write log entries in viewย project

Step 6: Edit IAM roles in view project for log viewersย (users)

In order for users to view the logs in the Logs Explorer, you need to grant them the ability to edit the view (or scope) by granting them the Logs View Accessor role.

1 nvyjmnfmslrq6lfdwgvq w
Granting users the ability to edit the view (scope) for Logsย Explorer

You can (and should) add further granularity to the user IAM role by adding a condition restricting access to only desired resources. In this case, itโ€™s the path to the logs bucket you created. This allows you to restrict usersโ€™ view to only the logs and buckets you desire, useful for compliance controls.

1 3gt042wgfvgwfkyzjfbvow

Step 7: View theย logs

With the permissions in place, within minutes or less, you should begin to see log entries from your view project. You must first โ€œRefine Scopeโ€ and select the desired logs source as shown.

1 nitafe0p3vdqoh4ropvzq
Refining scope of Logs Explorer view to explore logs shipped to the Logs Bucket fromย sinks

Congratulations! From your view project, you now can view logs from other projects as shown.

1 wx0kxjg01glwvjx6c3cpwg
Logs from test project โ€œaโ€ are visible in the โ€œviewโ€ย project

Bonus #1: Reduce costs with exclusion filters

You can disable your โ€œ_Defaultโ€ log sink on your projects to avoid paying for logging in multiple locations.

You may also add exclusion filters (or inclusion filters) in your log sinks to control which services they ship and which they filter out.

Bonus #2: Centralize your Cloud Monitoring

Google Cloud Operations (formerly Stackdriver) is a full-featured observability platform which, in addition to logging, includes another tool called Cloud Monitoring.

In a few clicks you can create a โ€œWorkspaceโ€ in your view project, and then select your other projects to centralize your monitoring and dashboards if you desire.

1 rxgvvjajxqmws5miditw3g
Cloud Monitoring allows you to centralize monitoring from GCP and AWS in one or more โ€œWorkspacesโ€

Summary

Hopefully, this article helps you better organize and manage your logging and observability across your organization. Follow me or view the DoiT Blog for more articles on tips & techniques, new features, best practices, and more for the public cloud.

Schedule a call with our team

You will receive a calendar invite to the email address provided below for a 15-minute call with one of our team members to discuss your needs.

You will be presented with date and time options on the next step