Event Handlers
GCP Event Handlers use a push-based mechanism to deliver change events from GCP Logging to Guardrails in near real-time. This is the recommended event delivery mechanism for production environments.
You can also use the gcp_setup baseline to automate this process.
Prerequisites
Turbot/OwnerorTurbot/Adminpermissions.- A GCP project connected to Guardrails or connected via organization.
- The following mods installed and up to date:
[!NOTE] GCP no longer requires domain verification for Pub/Sub subscriptions. Existing domain verification DNS records have no effect.
Service Account Permissions
Event Handlers manage Logging sinks and Pub/Sub topics in your GCP projects, so the Guardrails service account needs different roles than the read-only set used for Event Pollers.
If you set up the service account using one of the connection guides (Connect a GCP Organization or Connect a GCP Project), it was granted roles/logging.privateLogViewer for Event Pollers. To switch to Event Handlers, swap that role for the two below:
| Role | Purpose |
|---|---|
roles/pubsub.admin | Create and manage the Pub/Sub topic and subscription that deliver events to Guardrails. |
roles/logging.admin | Create and manage the Logging sink that routes Cloud Audit Log entries into the Pub/Sub topic. |
Apply the change at the same scope you used for the original grant: organization-wide for org imports, or per-project for standalone project imports.
SA_EMAIL="<your-sa-email>"
ORG_ID="<your-org-id>"
# Remove the Event Pollers role
gcloud organizations remove-iam-policy-binding "$ORG_ID" \
--member="serviceAccount:$SA_EMAIL" --role="roles/logging.privateLogViewer"
# Add the Event Handlers roles
for ROLE in roles/pubsub.admin roles/logging.admin; do
gcloud organizations add-iam-policy-binding "$ORG_ID" \
--member="serviceAccount:$SA_EMAIL" --role="$ROLE"
done
[!TIP] For least privilege, you can replace the two predefined roles above with a custom role scoped to event-handler resources only. See Custom Roles for Event Handling for the exact permission list and Terraform templates.
Step 1: Enable the Required APIs
Before configuring event handlers, enable the Pub/Sub and Logging APIs. You can either set the policies below to Enforce: Enabled so Guardrails enables them automatically, or manually enable the APIs in the GCP Console.
# GCP > Pub/Sub > API Enabled
resource "turbot_policy_setting" "pubsubApiEnabled" {
resource = "id of parent folder or policy pack"
type = "tmod:@turbot/gcp-pubsub#/policy/types/pubsubApiEnabled"
value = "Enforce: Enabled"
}
# GCP > Logging > API Enabled
resource "turbot_policy_setting" "loggingApiEnabled" {
resource = "id of parent folder or policy pack"
type = "tmod:@turbot/gcp-logging#/policy/types/loggingApiEnabled"
value = "Enforce: Enabled"
}
Step 2: Configure Workspace Policies (Optional)
These Turbot > Workspace policies control event handling behavior across the workspace. Configure them before enabling event handling if needed. Changes to these policies force a refresh of all event handling infrastructure.
| Policy | Description |
|---|---|
| Turbot > Workspace > Webhook Secrets > Rotation | Instructs Guardrails to regularly rotate the secrets used to sign JWTs. |
| Turbot > Workspace > Webhook Secrets > Expiration Period | Specifies the interval for secret rotation. Turbot Support recommends at least once a year. |
| Turbot > Workspace > Webhook Secrets | Use only when specific secrets are required. The default auto-generates secrets. |
Enterprise-Specific: API Gateway Configuration
For enterprise customers with a private Guardrails console, an API gateway must be used. Set the Turbot > Workspace > Gateway Domain Name policy to the API Gateway hostname (typically gateway.{installation_domain_name} from the TEF stack). SaaS customers should not set this policy.
If DNS resolution prevents using gateway.{installation_domain_name}, you can use the AWS API Gateway domain as an alternative:
- In the Turbot Guardrails Master account, go to the AWS API Gateway console and find the Guardrails API Gateway.
- Navigate to APIs >
turbot-api(or{resource_prefix}-api) > Stages >turbot. - Copy the Invoke URL (e.g.,
https://{uniqueId}.execute-api.{region}.amazonaws.com/turbot). - Strip
https://and set the remaining value as the Gateway Domain Name policy. - After enabling event handling, let the source policies recalculate and the Event Handler controls run again.
- Verify the Pub/Sub Subscription confirmed properly in the GCP Event Handler control logs.
Step 3: Enable Event Handlers
Deploy the event handling infrastructure by setting these policies:
- GCP > Turbot > Event Handlers > PubSub →
Enforce: Configured - GCP > Turbot > Event Handlers > Logging →
Enforce: Configured
[!NOTE] The Logging stack depends on the Pub/Sub Topic and Subscription. It will go into error until the Pub/Sub resources are present.
# GCP > Turbot > Event Handlers > Pub/Sub
resource "turbot_policy_setting" "eventHandlersPubSub" {
resource = "id of parent folder or policy pack"
type = "tmod:@turbot/gcp#/policy/types/eventHandlersPubSub"
value = "Enforce: Configured"
}
# GCP > Turbot > Event Handlers > Logging
resource "turbot_policy_setting" "eventHandlersLogging" {
resource = "id of parent folder or policy pack"
type = "tmod:@turbot/gcp#/policy/types/eventHandlersLogging"
value = "Enforce: Configured"
}
After setting the policies, the Event Handler stacks begin running in all projects in scope. After a few minutes, all controls should be in the OK state.
You can verify via GraphQL:
{
controls(
filter: "resource:your-project-name-or-id controlType:tmod:@turbot/gcp#/control/types/eventHandlers"
) {
items {
state
type {
turbot {
title
}
}
resource {
turbot {
title
}
}
}
}
}
Or via the Turbot Guardrails CLI:
turbot graphql controls \
--filter "controlType:tmod:@turbot/gcp#/control/types/eventHandlers" \
--output "{ items { state type { turbot { title } } resource { turbot { title } } } }"
Step 4: Verify Event Handlers
Create a new resource or modify an existing resource in the GCP project. Guardrails should receive the event and trigger relevant controls. If resource changes don't get picked up, check the troubleshooting section below.
Decommissioning Event Handlers
Set these policies to decommission:
- GCP > Turbot > Event Handlers > PubSub →
Enforce: Not Configured - GCP > Turbot > Event Handlers > Logging →
Enforce: Not Configured
Guardrails will perform the required Terraform runs to destroy the event handler infrastructure.
[!CAUTION] Deleting a GCP project from a Guardrails workspace does not automatically decommission event handlers. You must explicitly set the policies to
Enforce: Not Configuredbefore removing the project.
When to Decommission
Decommission Event Handlers before:
- Destroying the GCP project itself
- Removing the project from Guardrails supervision
- Switching to Event Pollers: processing duplicate events is highly undesirable
- Event handling is no longer desired for the project
Next Steps
- Connect a Member Project: Upgrade additional projects to All Resources mode now that Event Handlers are wired up.
- Enable GCP Services: Control which GCP services Guardrails governs in the project.
- Managing GCP Permissions: Configure IAM management through Guardrails.
Troubleshooting
The best source of troubleshooting information is in the GCP > Turbot > Event Handlers > Pub/Sub and GCP > Turbot > Event Handlers > Logging control logs for the project.
| Issue | Solution |
|---|---|
| Permissions issues | If the service account doesn't have permission to configure Logging and Pub/Sub resources, the control logs will indicate access denied. Grant the appropriate roles. |
| Pub/Sub Subscription won't confirm | Check the Turbot > Workspace policies to ensure they are properly configured. For enterprise customers, verify that resources in the Guardrails Master VPC can resolve the ALB hostname. |
| Logging stack in error | The Logging stack depends on Pub/Sub resources. Wait for the Pub/Sub Event Handler control to reach OK state first. |
| Events not being received | Verify that Cloud Audit Logs are enabled in the project. Create or modify a resource and check if events appear in the project's Activity tab in Guardrails. |
| Controls stuck in error | Check the control logs for the specific error. Common causes: missing API enablement, insufficient permissions, or workspace policy misconfiguration. |
| Pub/Sub stack in error, but the topic and subscription exist | The topic's IAM binding may be rejected by domain-restricted sharing. See Domain-restricted sharing below. |
Domain-restricted sharing
If your organization enforces domain-restricted sharing (the constraints/iam.allowedPolicyMemberDomains organization policy constraint), the Pub/Sub event handler stack will fail to configure and real-time events will not be delivered.
To publish events, Guardrails must grant roles/pubsub.publisher on the event handler topic to the logging sink's writer identity, which by default is the shared Google-managed account [email protected]. This account belongs to Google, not to your organization or Cloud Identity domain, so the constraint rejects the binding.
[!CAUTION] Per Google's documentation, allowlisting your Google Workspace customer ID covers service agents associated with resources in your organization, but not shared Google-managed accounts such as
[email protected]. Adding your own customer ID to the allowed list does not permit the default writer identity — it needs an explicit exception.
This failure is easy to miss. The topic and the subscription are created successfully, and the GCP > Turbot > Event Handlers > Logging control reports OK — only the topic's IAM binding is rejected. The sink then publishes to a topic it lacks permission to publish to, so events stop silently rather than producing an obvious error.
To resolve this, do one of the following:
- Add an exception to the constraint for the projects or folders that Guardrails manages.
- If you use the
iam.managed.allowedPolicyMembersmanaged constraint, add the writer identity as an allowed principal. This keeps domain-restricted sharing enforced elsewhere.
[!NOTE] Domain-restricted sharing can also cause API enablement to fail. Enabling a Google Cloud API may require automatically granting a role to that service's agent, and the constraint blocks that grant, which can fail the operation. If services will not enable, or
GCP > {Service} > API Enabledcontrols remain in alarm while set toEnforce: Enabled, check this constraint.