diff --git a/lambda_function/lambda_function.tf b/lambda_function/lambda_function.tf index 3331c9e..87ba273 100644 --- a/lambda_function/lambda_function.tf +++ b/lambda_function/lambda_function.tf @@ -1,3 +1,22 @@ +locals { + log_group_name = var.log_group_name == null ? "/aws/lambda/${var.function_name}" : var.log_group_name +} + +resource "aws_cloudwatch_log_group" "lambda_log_group" { + name = local.log_group_name + retention_in_days = var.log_retention_in_days + tags = var.log_tags == {} ? var.tags : var.log_tags + skip_destroy = var.log_skip_destroy +} + +resource "aws_cloudwatch_log_subscription_filter" "example_subscription_filter" { + count = var.ship_logs_to_sumo ? 1 : 0 + name = "${var.function_name}_subscription_filter" + log_group_name = aws_cloudwatch_log_group.lambda_log_group.name + destination_arn = "arn:aws:lambda:${data.aws_region.current.name}:${data.aws_caller_identity.current.account_id}:function:SumoCWLogsLambda" + filter_pattern = "" +} + resource "aws_lambda_function" "lambda" { function_name = var.function_name description = var.description diff --git a/lambda_function/variables.tf b/lambda_function/variables.tf index ec03616..c3ebaf5 100644 --- a/lambda_function/variables.tf +++ b/lambda_function/variables.tf @@ -159,4 +159,33 @@ variable "create_codebuild_to_run_unit_test" { type = bool default = false description = "If true, will create codebuild and all the resources for running unit tests" +} + +variable "log_group_name" { + type = string + default = null + description = "If set, will create a log group with that name for the lambda, otherwise the default name (/aws/lambda/) will be used" +} + +variable "log_retention_in_days" { + type = number + default = 0 # Never expire +} + +variable "log_skip_destroy" { + type = bool + default = true + description = "If true, the log group will not be destroyed when the lambda is destroyed" +} + +variable "ship_logs_to_sumo" { + type = bool + default = false + description = "If true, will create a subscription filter to send logs to Sumo Logic" +} + +variable "log_tags" { + type = map(string) + default = {} + description = "Tags to apply to the log group. Defaults to the same tags as the lambda function if nothing is passed in." } \ No newline at end of file