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

[!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:

RolePurpose
roles/pubsub.adminCreate and manage the Pub/Sub topic and subscription that deliver events to Guardrails.
roles/logging.adminCreate 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.

PolicyDescription
Turbot > Workspace > Webhook Secrets > RotationInstructs Guardrails to regularly rotate the secrets used to sign JWTs.
Turbot > Workspace > Webhook Secrets > Expiration PeriodSpecifies the interval for secret rotation. Turbot Support recommends at least once a year.
Turbot > Workspace > Webhook SecretsUse 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:

  1. In the Turbot Guardrails Master account, go to the AWS API Gateway console and find the Guardrails API Gateway.
  2. Navigate to APIs > turbot-api (or {resource_prefix}-api) > Stages > turbot.
  3. Copy the Invoke URL (e.g., https://{uniqueId}.execute-api.{region}.amazonaws.com/turbot).
  4. Strip https:// and set the remaining value as the Gateway Domain Name policy.
  5. After enabling event handling, let the source policies recalculate and the Event Handler controls run again.
  6. 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:

[!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:

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 Configured before removing the project.

When to Decommission

Decommission Event Handlers before:

Next Steps

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.

IssueSolution
Permissions issuesIf 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 confirmCheck 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 errorThe Logging stack depends on Pub/Sub resources. Wait for the Pub/Sub Event Handler control to reach OK state first.
Events not being receivedVerify 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 errorCheck 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 existThe 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:

[!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 Enabled controls remain in alarm while set to Enforce: Enabled, check this constraint.