Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(lambda-promtail): allow prefix matching for CloudWatch log groups #14801

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 23 additions & 5 deletions tools/lambda-promtail/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -245,18 +245,36 @@ resource "aws_lambda_permission" "lambda_promtail_allow_cloudwatch" {
principal = "logs.${data.aws_region.current.name}.amazonaws.com"
}

# This block allows for easily subscribing to multiple log groups via the `log_group_names` var.
# However, if you need to provide an actual filter_pattern for a specific log group you should
# copy this block and modify it accordingly.
# Providing a log group prefix of "" enables matching _all_ log groups
data "aws_cloudwatch_log_groups" "lambdafunction_logs" {
for_each = var.log_group_prefixes

log_group_name_prefix = each.value
}

locals {
# Combine prefix-generated names from the data source call with the explicitly defined
# names to get the full set of log groups to create subscription filters for.
# Be sure to remove "/aws/lambda/${var.name}" so we don't log ourselves into oblivion.
log_group_names = setsubtract(
setunion(
var.log_group_names,
data.aws_cloudwatch_log_groups.lambdafunction_logs.log_group_names
),
toset(["/aws/lambda/${var.name}"])
)
}

resource "aws_cloudwatch_log_subscription_filter" "lambdafunction_logfilter" {
for_each = var.log_group_names

name = "lambdafunction_logfilter_${each.value}"
log_group_name = each.value
destination_arn = aws_lambda_function.this.arn

# required but can be empty string
filter_pattern = ""
# Default to no filter at all (empty string), but allow callers to narrow their
# search as desired.
filter_pattern = lookup(var.log_group_subscription_filter_patterns, each.value, "")
}

#-------------------------------------------------------------------------------
Expand Down
12 changes: 12 additions & 0 deletions tools/lambda-promtail/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,18 @@ variable "log_group_names" {
default = []
}

variable "log_group_prefixes" {
type = set(string)
description = "List of prefixes to match CloudWatch Log Group names to create Subscription Filters for."
default = []
}

variable "log_group_subscription_filter_patterns" {
type = map(string)
description = "Filter subscription patterns. The key is the log group and the value is the filter pattern."
default = {}
}

variable "lambda_promtail_image" {
type = string
description = "The ECR image URI to pull and use for lambda-promtail."
Expand Down
Loading