Event Handlers

Event handlers deliver near real-time events from AWS to Guardrails via EventBridge and SNS. When a resource changes in your AWS account, CloudTrail records the API call, EventBridge routes it through filtering rules, and an SNS subscription delivers it to Guardrails for processing.

There are two event handler mechanisms:

To migrate from regional to global, see Migrate to Global Event Handlers.

Required Mods

The following mods must be installed and up to date for event handlers to function:

FunctionalityRequired Mods
AWS Account Importaws, aws-iam, aws-kms
CloudTrail Configurationaws-cloudtrail, aws-s3
Event Handler Configurationaws-events, aws-sns

Verify all mods are installed and in ok state under Settings > Mods before enabling event handlers.

CloudTrail Configuration

CloudTrail must be active in every region where you want event handling. CloudTrail feeds API activity into EventBridge, which event handlers use to route events to Guardrails.

You can use any existing trail: regional, global, or Organization trail. An additional CloudTrail dedicated to Guardrails is unnecessary cost.

[!WARNING] If CloudTrail is not configured in a region, event handlers cannot capture events from that region regardless of other configuration.

Guardrails-Managed CloudTrail (Optional)

If you don't have an existing trail, Guardrails can manage one for you.

Create logging buckets: CloudTrail requires an S3 bucket for log storage. Set AWS > Turbot > Logging > Bucket to Enforce: Configured:

resource "turbot_policy_setting" "loggingBucket" {
  resource = "id-of-parent-folder-or-policy-pack"
  type     = "tmod:@turbot/aws#/policy/types/loggingBucket"
  value    = "Enforce: Configured"
}

This creates a logging bucket in each region specified in the AWS > Account > Approved Regions [Default] policy. Customize with AWS > Turbot > Logging > Bucket > * sub-policies. Use AWS > Turbot > Logging > Bucket > Regions to limit which regions get logging buckets.

Verify that AWS > Turbot > Logging > Bucket controls reach ok state for each region.

Set up the audit trail, once logging buckets are ready:

resource "turbot_policy_setting" "auditTrail" {
  resource = "id-of-parent-folder-or-policy-pack"
  type     = "tmod:@turbot/aws#/policy/types/auditTrail"
  value    = "Enforce: Configured"
}

The default creates a global trail in us-east-1. Customize with AWS > Turbot > Audit Trail > * sub-policies.

Verify that AWS > Turbot > Audit Trail controls reach ok state.

Workspace Policies

Review these workspace-level policies before enabling event handlers:

PolicyDescription
Turbot > Workspace > Webhook Secrets > RotationRegular rotation of JWT signing secrets used to authenticate events. Recommended: at least yearly.
Turbot > Workspace > Webhook Secrets > Expiration PeriodInterval for secret rotation.
Turbot > Workspace > Webhook SecretsUse only when specific secrets are required. Otherwise, Guardrails auto-generates secrets as needed.
AWS > Account > RegionsRegions Guardrails monitors. Must not exceed regions permitted by your SCPs. If SCPs restrict certain regions, update this policy to match. Otherwise event handler deployment will fail with Access Denied errors.

Enterprise API Gateway Configuration

For Enterprise installations with a private Guardrails console, an API Gateway is required for event delivery. The hostname of the API Gateway must be set in the Turbot > Workspace > Gateway Domain Name policy.

Typically, the domain name is gateway.{installation_domain_name}, where the installation domain name comes from the TEF stack configuration.

If gateway.{installation_domain_name} cannot be used (due to DNS resolution complexity or enterprise policy), use the AWS API Gateway domain name instead. It is always publicly resolvable:

  1. In the Turbot Master account, go to the AWS API Gateway console.
  2. Go to APIs. Click into turbot-api or {resource_prefix}-api. Click into Stages, then click the turbot stage.
  3. From the top of the turbot stage page, copy the Invoke URL. It will be in the form https://{uniqueId}.execute-api.{region}.amazonaws.com/turbot. The /turbot path is required. Do not add a trailing /.
  4. Strip off https:// and set the Turbot > Workspace > Gateway Domain Name policy to the result: {uniqueId}.execute-api.{region}.amazonaws.com/turbot
  5. If event handlers are already enabled, let the event handler source policies recalculate and the Event Handler controls run again.
  6. Check the SNS Subscription in the Event Handler control logs to confirm it subscribed successfully.

How Event Handlers Work

Guardrails uses Terraform to deploy event handling infrastructure. The Terraform source is compiled from multiple inputs:

All inputs combine in the read-only AWS > Turbot > Event Handlers > Source (or Event Handlers [Global] > Source for GEH) policy value, which contains the compiled Terraform. The "Depends On" tab of this policy shows all dependencies. Errors in any dependency may prevent event handlers from deploying or updating properly.

Event Flow Path

Events pass through several services on their way from AWS to Guardrails:

  1. CloudTrail records API activity in the region.
  2. EventBridge rules filter raw CloudTrail events to those Guardrails needs based on installed mods.
  3. SNS topic receives the filtered events.
  4. SNS subscription sends events via HTTPS to the Guardrails endpoint.
  5. (Enterprise only) API Gateway and Events Proxy Lambda forward from the public API endpoint to the private ALB.
  6. Guardrails Events Container processes events, triggering CMDB updates and control runs.

For Global Event Handlers, secondary regions forward events to the primary region's event bus at step 2, and steps 3-6 happen only in the primary region. For Regional Event Handlers, steps 1-6 happen independently in every region.

Event Handler Errors and Bulk Changes

Event handler configuration changes can be triggered by:

Each update requires at least one Terraform run per region (or per account for GEH). In large environments, an event handler control may time out waiting for a Terraform container, resulting in an error state. Guardrails retries these errors automatically over time.

[!WARNING] While event handlers are in error state, the event poller will NOT run, because event handling is technically "enabled." Events may be missed until the errors are resolved. Resolve event handler errors as quickly as possible.

Guides

GuideDescription
Global Event HandlersSet up the recommended real-time event delivery mechanism.
Regional Event HandlersConfigure legacy per-region event handlers.
Migrate to Global Event HandlersMove from regional to global event handlers.