Skip to content

AWS Log Ingestion through Kinesis Firehose

This guide is for customers who want to send AWS CloudTrail, VPC Flow Logs, and Route 53 Resolver query logs to SenseOn. All three travel over one Kinesis Firehose delivery stream in your own AWS account, pushed straight to SenseOn's ingestion endpoint. There is no Lambda to run or maintain, unlike the legacy CloudWatch forwarder method.

How it works

VPC Flow Logs and Route 53 Resolver query logs both support Kinesis Firehose as a direct delivery destination:

VPC Flow Logs / Route 53 Resolver query logs (your account)
        -> Kinesis Firehose delivery stream (your account)
        -> HTTPS -> SenseOn ingestion endpoint

CloudTrail has no direct Firehose destination, so it travels one hop further, through its existing CloudWatch Logs delivery:

CloudTrail -> CloudWatch Log Group (your account)
        -> Subscription filter
        -> Kinesis Firehose delivery stream (your account, same stream as above)
        -> HTTPS -> SenseOn ingestion endpoint

You deploy one Firehose delivery stream in your account. Point it at the ingestion endpoint and access key SenseOn gives you, then attach each log source to it. VPC Flow Logs and Resolver query logs attach directly. CloudTrail attaches by enabling its CloudWatch Logs delivery and adding a subscription filter on that log group. Everything runs in your own account, and SenseOn never needs a role or credentials into it.

Prerequisites

  • Permissions in your AWS account to create a Kinesis Firehose delivery stream, an S3 bucket and IAM roles.
  • Permissions to create a CloudTrail trail, VPC Flow Logs and Route 53 Resolver query logging.
  • The Firehose destination URL and access key for your tenant, from your SenseOn contact (see Step 1).
  • An existing CloudTrail trail, if you want CloudTrail included, or create a new one as part of this setup.

Step 1: Get your Firehose destination details

Ask your SenseOn contact to enable this for your tenant. They will give you two values:

Item Example Used as
Firehose destination URL https://your-tenant-firehose-logs.senseon.io firehose_destination_url
Access key a long random string firehose_access_key

⚠ The access key is a credential. SenseOn checks every delivery against it. Keep it out of version control and share it only over an approved secure channel. Terraform also writes it into terraform.tfstate and any plan file in plain text, regardless of the sensitive flag on the variable. The zip's .gitignore excludes these files. Encrypt them too, if you use a shared or remote state backend.

Step 2: Deploy the Firehose stream and attach your log sources

Download the Terraform module (zip) to get every file below already in place, rather than creating each one by hand. Unzip it, review the contents against the code shown below, then skip ahead to terraform init.

Prerequisites beyond the ones above: Terraform >= 1.5 and the aws CLI.

The files, for reference, are identical to what is in the zip. Each one is collapsed by default. Click to expand.

versions.tf
terraform {
  required_version = ">= 1.5"

  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 5.0"
    }
  }
}

provider "aws" {
  region  = var.aws_region
  profile = var.aws_profile
}
variables.tf
variable "aws_region" {
  description = "Region to build these resources in."
  type        = string
  default     = "eu-west-1"
}

variable "aws_profile" {
  description = "Named AWS CLI profile to authenticate with (leave null to use the default credential chain)."
  type        = string
  default     = null
}

variable "name_prefix" {
  description = "Prefix applied to every resource name, so this stack is identifiable in your account and can be torn down cleanly on its own."
  type        = string
  default     = "senseon-ingest"
}

variable "firehose_destination_url" {
  description = <<-EOT
    SenseOn's Kinesis Firehose HTTP Endpoint destination for your tenant.
    Provided by your SenseOn contact, for example
    "https://your-tenant-firehose-logs.senseon.io" (no port, AWS only
    allows 443).
  EOT
  type        = string
}

variable "firehose_access_key" {
  description = "The shared secret SenseOn gave you alongside firehose_destination_url. Firehose sends it as the x-amz-firehose-access-key header on every request, and SenseOn checks it against the value they hold for your tenant."
  type        = string
  sensitive   = true
}

variable "vpc_id" {
  description = "VPC to enable Flow Logs and Resolver query logging on. Leave null to use the account/region's default VPC; an account with no default VPC must set this explicitly, the lookup has no fallback."
  type        = string
  default     = null
}

variable "log_retention_days" {
  description = "Retention for the CloudWatch log groups this stack creates for CloudTrail and Firehose delivery errors; VPC Flow Logs and Resolver query logs go directly to Firehose."
  type        = number
  default     = 14
}
firehose.tf
data "aws_caller_identity" "current" {}

# Firehose requires an S3 backup destination even when the primary target is
# an HTTP endpoint, for records the endpoint rejects or times out on.
resource "aws_s3_bucket" "firehose_backup" {
  bucket        = "${var.name_prefix}-firehose-backup-${data.aws_caller_identity.current.account_id}"
  force_destroy = true
}

resource "aws_s3_bucket_public_access_block" "firehose_backup" {
  bucket                  = aws_s3_bucket.firehose_backup.id
  block_public_acls       = true
  block_public_policy     = true
  ignore_public_acls      = true
  restrict_public_buckets = true
}

# Versioned so a stray delete (or the force_destroy teardown below) doesn't
# silently lose a failed-delivery record with no recovery path.
resource "aws_s3_bucket_versioning" "firehose_backup" {
  bucket = aws_s3_bucket.firehose_backup.id
  versioning_configuration {
    status = "Enabled"
  }
}

resource "aws_cloudwatch_log_group" "firehose_delivery" {
  name              = "/aws/kinesisfirehose/${var.name_prefix}"
  retention_in_days = var.log_retention_days
}

resource "aws_cloudwatch_log_stream" "firehose_delivery" {
  name           = "HttpEndpointDelivery"
  log_group_name = aws_cloudwatch_log_group.firehose_delivery.name
}

data "aws_iam_policy_document" "firehose_trust" {
  statement {
    effect  = "Allow"
    actions = ["sts:AssumeRole"]
    principals {
      type        = "Service"
      identifiers = ["firehose.amazonaws.com"]
    }
  }
}

resource "aws_iam_role" "firehose" {
  name               = "${var.name_prefix}-firehose"
  assume_role_policy = data.aws_iam_policy_document.firehose_trust.json
}

data "aws_iam_policy_document" "firehose_permissions" {
  statement {
    effect = "Allow"
    actions = [
      "s3:AbortMultipartUpload",
      "s3:GetBucketLocation",
      "s3:GetObject",
      "s3:ListBucket",
      "s3:ListBucketMultipartUploads",
      "s3:PutObject",
    ]
    resources = [
      aws_s3_bucket.firehose_backup.arn,
      "${aws_s3_bucket.firehose_backup.arn}/*",
    ]
  }

  statement {
    effect    = "Allow"
    actions   = ["logs:PutLogEvents"]
    resources = ["${aws_cloudwatch_log_group.firehose_delivery.arn}:log-stream:*"]
  }
}

resource "aws_iam_role_policy" "firehose" {
  name   = "${var.name_prefix}-firehose"
  role   = aws_iam_role.firehose.id
  policy = data.aws_iam_policy_document.firehose_permissions.json
}

resource "aws_kinesis_firehose_delivery_stream" "this" {
  name        = "${var.name_prefix}-to-senseon"
  destination = "http_endpoint"

  http_endpoint_configuration {
    url                = var.firehose_destination_url
    name               = "SenseOn"
    access_key         = var.firehose_access_key
    role_arn           = aws_iam_role.firehose.arn
    buffering_size     = 1
    buffering_interval = 60
    retry_duration     = 60
    s3_backup_mode     = "FailedDataOnly"

    cloudwatch_logging_options {
      enabled         = true
      log_group_name  = aws_cloudwatch_log_group.firehose_delivery.name
      log_stream_name = aws_cloudwatch_log_stream.firehose_delivery.name
    }

    s3_configuration {
      role_arn   = aws_iam_role.firehose.arn
      bucket_arn = aws_s3_bucket.firehose_backup.arn
    }
  }

  depends_on = [aws_iam_role_policy.firehose]
}
vpc_flow_logs.tf
data "aws_vpc" "default" {
  count   = var.vpc_id == null ? 1 : 0
  default = true
}

locals {
  target_vpc_id = coalesce(var.vpc_id, try(data.aws_vpc.default[0].id, null))
}

resource "aws_flow_log" "this" {
  vpc_id                   = local.target_vpc_id
  traffic_type             = "ALL"
  log_destination_type     = "kinesis-data-firehose"
  log_destination          = aws_kinesis_firehose_delivery_stream.this.arn
  max_aggregation_interval = 60
}
dns_query_logs.tf
# No IAM role argument exists on this resource (confirmed against the
# provider schema); Resolver's own AWS-to-AWS Firehose delivery needs none.
resource "aws_route53_resolver_query_log_config" "this" {
  name            = "${var.name_prefix}-dns-query-logs"
  destination_arn = aws_kinesis_firehose_delivery_stream.this.arn
}

resource "aws_route53_resolver_query_log_config_association" "this" {
  resolver_query_log_config_id = aws_route53_resolver_query_log_config.this.id
  resource_id                  = local.target_vpc_id
}
cloudtrail.tf
# CloudTrail requires an S3 destination on every trail; this bucket exists
# only to satisfy that requirement and carries no ingestion traffic.
resource "aws_s3_bucket" "cloudtrail" {
  bucket        = "${var.name_prefix}-cloudtrail-${data.aws_caller_identity.current.account_id}"
  force_destroy = true
}

resource "aws_s3_bucket_public_access_block" "cloudtrail" {
  bucket                  = aws_s3_bucket.cloudtrail.id
  block_public_acls       = true
  block_public_policy     = true
  ignore_public_acls      = true
  restrict_public_buckets = true
}

# Versioned so a stray delete (or the force_destroy teardown below) doesn't
# silently lose the CloudTrail archive with no recovery path.
resource "aws_s3_bucket_versioning" "cloudtrail" {
  bucket = aws_s3_bucket.cloudtrail.id
  versioning_configuration {
    status = "Enabled"
  }
}

data "aws_iam_policy_document" "cloudtrail_bucket" {
  statement {
    sid     = "AWSCloudTrailAclCheck"
    effect  = "Allow"
    actions = ["s3:GetBucketAcl"]
    principals {
      type        = "Service"
      identifiers = ["cloudtrail.amazonaws.com"]
    }
    resources = [aws_s3_bucket.cloudtrail.arn]
  }

  statement {
    sid     = "AWSCloudTrailWrite"
    effect  = "Allow"
    actions = ["s3:PutObject"]
    principals {
      type        = "Service"
      identifiers = ["cloudtrail.amazonaws.com"]
    }
    resources = ["${aws_s3_bucket.cloudtrail.arn}/AWSLogs/${data.aws_caller_identity.current.account_id}/*"]
    condition {
      test     = "StringEquals"
      variable = "s3:x-amz-acl"
      values   = ["bucket-owner-full-control"]
    }
  }
}

resource "aws_s3_bucket_policy" "cloudtrail" {
  bucket = aws_s3_bucket.cloudtrail.id
  policy = data.aws_iam_policy_document.cloudtrail_bucket.json
}

resource "aws_cloudwatch_log_group" "cloudtrail" {
  name              = "/aws/cloudtrail/${var.name_prefix}"
  retention_in_days = var.log_retention_days
}

data "aws_iam_policy_document" "cloudtrail_to_cwl_trust" {
  statement {
    effect  = "Allow"
    actions = ["sts:AssumeRole"]
    principals {
      type        = "Service"
      identifiers = ["cloudtrail.amazonaws.com"]
    }
  }
}

resource "aws_iam_role" "cloudtrail_to_cwl" {
  name               = "${var.name_prefix}-cloudtrail-to-cwl"
  assume_role_policy = data.aws_iam_policy_document.cloudtrail_to_cwl_trust.json
}

data "aws_iam_policy_document" "cloudtrail_to_cwl_permissions" {
  statement {
    effect = "Allow"
    actions = [
      "logs:CreateLogStream",
      "logs:PutLogEvents",
    ]
    resources = ["${aws_cloudwatch_log_group.cloudtrail.arn}:*"]
  }
}

resource "aws_iam_role_policy" "cloudtrail_to_cwl" {
  name   = "${var.name_prefix}-cloudtrail-to-cwl"
  role   = aws_iam_role.cloudtrail_to_cwl.id
  policy = data.aws_iam_policy_document.cloudtrail_to_cwl_permissions.json
}

resource "aws_cloudtrail" "this" {
  name                          = var.name_prefix
  s3_bucket_name                = aws_s3_bucket.cloudtrail.id
  include_global_service_events = true
  is_multi_region_trail         = true
  enable_log_file_validation    = true

  cloud_watch_logs_group_arn = "${aws_cloudwatch_log_group.cloudtrail.arn}:*"
  cloud_watch_logs_role_arn  = aws_iam_role.cloudtrail_to_cwl.arn

  depends_on = [
    aws_s3_bucket_policy.cloudtrail,
    aws_iam_role_policy.cloudtrail_to_cwl,
  ]
}

# CloudTrail has no direct Firehose destination (unlike VPC Flow Logs and
# Resolver query logs), so it still goes through CloudWatch Logs first.
# aws:SourceArn confines the role to this stack's own log group.
data "aws_iam_policy_document" "cwl_to_firehose_trust" {
  statement {
    effect  = "Allow"
    actions = ["sts:AssumeRole"]
    principals {
      type        = "Service"
      identifiers = ["logs.${var.aws_region}.amazonaws.com"]
    }
    condition {
      test     = "ArnLike"
      variable = "aws:SourceArn"
      values   = ["${aws_cloudwatch_log_group.cloudtrail.arn}:*"]
    }
  }
}

resource "aws_iam_role" "cwl_to_firehose" {
  name               = "${var.name_prefix}-cwl-to-firehose"
  assume_role_policy = data.aws_iam_policy_document.cwl_to_firehose_trust.json
}

data "aws_iam_policy_document" "cwl_to_firehose_permissions" {
  statement {
    effect    = "Allow"
    actions   = ["firehose:PutRecord", "firehose:PutRecordBatch"]
    resources = [aws_kinesis_firehose_delivery_stream.this.arn]
  }
}

resource "aws_iam_role_policy" "cwl_to_firehose" {
  name   = "${var.name_prefix}-cwl-to-firehose"
  role   = aws_iam_role.cwl_to_firehose.id
  policy = data.aws_iam_policy_document.cwl_to_firehose_permissions.json
}

# CloudWatch Logs batches several CloudTrail events into one Firehose record.
# SenseOn unpacks this on its own side -- one Hunt Lab row per CloudTrail
# event, same as the other two sources.
resource "aws_cloudwatch_log_subscription_filter" "cloudtrail_to_firehose" {
  name            = "${var.name_prefix}-cloudtrail-to-firehose"
  log_group_name  = aws_cloudwatch_log_group.cloudtrail.name
  filter_pattern  = ""
  destination_arn = aws_kinesis_firehose_delivery_stream.this.arn
  role_arn        = aws_iam_role.cwl_to_firehose.arn

  depends_on = [aws_iam_role_policy.cwl_to_firehose]
}
outputs.tf
output "firehose_delivery_stream_arn" {
  value = aws_kinesis_firehose_delivery_stream.this.arn
}

output "firehose_backup_bucket" {
  value = aws_s3_bucket.firehose_backup.id
}

output "firehose_delivery_log_group" {
  value = aws_cloudwatch_log_group.firehose_delivery.name
}

output "cloudtrail_arn" {
  value = aws_cloudtrail.this.arn
}

output "vpc_id" {
  value = local.target_vpc_id
}

output "resolver_query_log_config_id" {
  value = aws_route53_resolver_query_log_config.this.id
}

example.tfvars is included in the zip with the same placeholders shown below. Copy it and fill in your own values -- keep the real terraform.tfvars out of version control, since it holds your access key:

example.tfvars
aws_region  = "eu-west-1"
aws_profile = "your-aws-cli-profile" # named AWS CLI profile for your account
name_prefix = "senseon-ingest"

firehose_destination_url = "https://your-tenant-firehose-logs.senseon.io"
firehose_access_key      = "" # paste the access key your SenseOn contact gave you

vpc_id = null # defaults to the account/region's default VPC
cp example.tfvars terraform.tfvars
# edit terraform.tfvars with your own values

Then apply:

terraform init
terraform plan  -var-file=terraform.tfvars
terraform apply -var-file=terraform.tfvars

This builds, in one pass, the Firehose delivery stream and its backup bucket. It also builds a CloudTrail trail with CloudWatch Logs delivery and a subscription filter onto the Firehose stream. It adds VPC Flow Logs on the target VPC, pointed directly at Firehose. It adds a Route 53 Resolver query log configuration, pointed at the same stream.

If a VPC Flow Logs source already has flow logs enabled

A VPC can have more than one Flow Logs configuration active at once, each with its own destination. If you already send flow logs elsewhere (for example to S3 for compliance retention), this stack's aws_flow_log resource adds a second, independent one. It does not replace or disturb the existing configuration.

If you already have a CloudTrail trail

This module creates its own trail. To attach the Firehose path to an existing trail, enable that trail's CloudWatch Logs delivery yourself, pointing at a log group of your choice. Then adapt cloudtrail.tf to reference your existing log group instead of creating aws_cloudtrail.this, and keep the subscription filter resource as it is.

Step 3: Verify it is working

Generate traffic: any AWS API call in the account (including terraform apply itself) generates a CloudTrail event. A DNS lookup and some network activity from an instance in the target VPC exercises the other two. Wait at least 60 seconds for Firehose's buffer to flush, then check delivery:

aws s3 ls "s3://$(terraform output -raw firehose_backup_bucket)" --profile <aws_profile> --region <aws_region>   # should stay empty; objects indicate rejected/failed deliveries
aws logs tail "$(terraform output -raw firehose_delivery_log_group)" --since 10m --profile <aws_profile> --region <aws_region>

An empty backup bucket and a clean delivery log group rule out failed deliveries, not that anything arrived. The same result shows if nothing was sent at all. Confirm with your SenseOn contact once you expect data to be flowing.

Decommissioning

terraform destroy -var-file=terraform.tfvars

⚠ This permanently deletes both S3 buckets and every version they hold, including the CloudTrail archive and any failed-delivery backups. force_destroy empties a bucket, past versions included, before it removes the bucket. Export anything you need to keep first.

Good practice

  • No cross-account access required: this is a push model from your account. SenseOn never needs a role or credentials into your AWS account for this path.
  • One stream, three sources: all three log types share the same Firehose delivery stream and access key. You do not need a separate stream per source.
  • Keep the access key secret: rotate it by generating a new one, and sending it to your SenseOn contact over a secure channel. Update terraform.tfvars once they confirm it is in place on their side.
  • CloudTrail arrives a little differently under the hood: CloudWatch Logs batches several CloudTrail events into one Firehose record before SenseOn ever sees it. SenseOn unpacks this on its own side. You will see one row per CloudTrail event in Hunt Lab, the same as the other two sources. There is nothing extra for you to configure.