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: Add Log Anomaly Detection Support #74

Merged
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
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,23 @@ module "log_account_policy" {
}
```

### Log Anomaly Detector

```hcl
module "log_anomaly_detector" {
source = "terraform-aws-modules/cloudwatch/aws//modules/log-anomaly-detector"
version = "~> 4.0"

detector_name = "anomaly-detector"
log_group_arns = ["arn:aws:logs:eu-west-1:835367859852:log-group:my-log-group"]
anomaly_visibility_time = 7
enabled = true
evaluation_frequency = "FIVE_MIN"
filter_pattern = "%test%"
kms_key_id = "arn:aws:kms:eu-west-1:835367859852:key/9051f3e7-17b8-4543-8070-50e22599966"
}
```

## Examples

- [Complete Cloudwatch log metric filter and alarm](https://github.com/terraform-aws-modules/terraform-aws-cloudwatch/tree/master/examples/complete-log-metric-filter-and-alarm)
Expand All @@ -267,6 +284,7 @@ module "log_account_policy" {
- [Cloudwatch log subscription filter](https://github.com/terraform-aws-modules/terraform-aws-cloudwatch/tree/master/examples/log-subscription-filter)
- [Cloudwatch log data protection policy](https://github.com/terraform-aws-modules/terraform-aws-cloudwatch/tree/master/examples/log-group-with-data-protection-policy)
- [Cloudwatch Log Account Policy](https://github.com/terraform-aws-modules/terraform-aws-cloudwatch/tree/master/examples/log-account-policy)
- [Cloudwatch Log Anomaly Detector](https://github.com/terraform-aws-modules/terraform-aws-cloudwatch/tree/master/examples/log-anomaly-detector)

<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
Expand Down
2 changes: 1 addition & 1 deletion examples/cis-alarms/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Note that this example may create resources which cost money. Run `terraform des
| Name | Version |
|------|---------|
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 1.0 |
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 5.58 |
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 5.81 |

## Providers

Expand Down
2 changes: 1 addition & 1 deletion examples/cis-alarms/versions.tf
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = ">= 5.58"
version = ">= 5.81"
}
}
}
2 changes: 1 addition & 1 deletion examples/complete-log-metric-filter-and-alarm/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Note that this example may create resources which cost money. Run `terraform des
| Name | Version |
|------|---------|
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 1.0 |
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 5.58 |
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 5.81 |

## Providers

Expand Down
2 changes: 1 addition & 1 deletion examples/complete-log-metric-filter-and-alarm/versions.tf
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = ">= 5.58"
version = ">= 5.81"
}
}
}
2 changes: 1 addition & 1 deletion examples/composite-alarm/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Note that this example may create resources which cost money. Run `terraform des
| Name | Version |
|------|---------|
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 1.0 |
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 5.58 |
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 5.81 |

## Providers

Expand Down
2 changes: 1 addition & 1 deletion examples/composite-alarm/versions.tf
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = ">= 5.58"
version = ">= 5.81"
}
}
}
2 changes: 1 addition & 1 deletion examples/fixtures/aws_cloudwatch_log_group/versions.tf
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = ">= 5.58"
version = ">= 5.81"
}

random = {
Expand Down
2 changes: 1 addition & 1 deletion examples/fixtures/aws_kms_key/versions.tf
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = ">= 5.58"
version = ">= 5.81"
}

random = {
Expand Down
2 changes: 1 addition & 1 deletion examples/fixtures/aws_lambda_function/versions.tf
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = ">= 5.58"
version = ">= 5.81"
}

random = {
Expand Down
2 changes: 1 addition & 1 deletion examples/fixtures/aws_sns_topic/versions.tf
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = ">= 5.58"
version = ">= 5.81"
}

random = {
Expand Down
2 changes: 1 addition & 1 deletion examples/lambda-metric-alarm/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Note that this example may create resources which cost money. Run `terraform des
| Name | Version |
|------|---------|
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 1.0 |
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 5.58 |
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 5.81 |

## Providers

Expand Down
2 changes: 1 addition & 1 deletion examples/lambda-metric-alarm/versions.tf
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = ">= 5.58"
version = ">= 5.81"
}
}
}
4 changes: 2 additions & 2 deletions examples/log-account-policy/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
| Name | Version |
|------|---------|
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 1.0 |
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 5.58 |
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 5.81 |
| <a name="requirement_random"></a> [random](#requirement\_random) | >= 3.5 |

## Providers

| Name | Version |
|------|---------|
| <a name="provider_aws"></a> [aws](#provider\_aws) | >= 5.58 |
| <a name="provider_aws"></a> [aws](#provider\_aws) | >= 5.81 |
| <a name="provider_random"></a> [random](#provider\_random) | >= 3.5 |

## Modules
Expand Down
2 changes: 1 addition & 1 deletion examples/log-account-policy/versions.tf
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = ">= 5.58"
version = ">= 5.81"
}
random = {
source = "hashicorp/random"
Expand Down
41 changes: 41 additions & 0 deletions examples/log-anomaly-detector/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Complete Log Anomaly Detector

<!-- BEGIN_TF_DOCS -->
## Requirements

| Name | Version |
|------|---------|
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 1.0 |
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 5.81 |

## Providers

| Name | Version |
|------|---------|
| <a name="provider_aws"></a> [aws](#provider\_aws) | >= 5.81 |

## Modules

| Name | Source | Version |
|------|--------|---------|
| <a name="module_kms"></a> [kms](#module\_kms) | terraform-aws-modules/kms/aws | ~> 1.0 |
| <a name="module_log_anomaly_detector"></a> [log\_anomaly\_detector](#module\_log\_anomaly\_detector) | ../../modules/log-anomaly-detector | n/a |
| <a name="module_log_group"></a> [log\_group](#module\_log\_group) | ../../modules/log-group | n/a |

## Resources

| Name | Type |
|------|------|
| [aws_caller_identity.current](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/caller_identity) | data source |
| [aws_region.current](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/region) | data source |

## Inputs

No inputs.

## Outputs

| Name | Description |
|------|-------------|
| <a name="output_cloudwatch_log_anomaly_arn"></a> [cloudwatch\_log\_anomaly\_arn](#output\_cloudwatch\_log\_anomaly\_arn) | The ARN of the anomaly detector. |
<!-- END_TF_DOCS -->
103 changes: 103 additions & 0 deletions examples/log-anomaly-detector/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
provider "aws" {
region = "eu-west-1"
}

locals {
name = "ex-${basename(path.cwd)}"
}

module "log_group" {
source = "../../modules/log-group"

name = local.name
retention_in_days = 7
kms_key_id = module.kms.key_arn
}

module "log_anomaly_detector" {
source = "../../modules/log-anomaly-detector"

detector_name = local.name
log_group_arns = [module.log_group.cloudwatch_log_group_arn]
anomaly_visibility_time = 7
enabled = true
evaluation_frequency = "FIVE_MIN"
filter_pattern = "%test%"
kms_key_id = module.kms.key_arn
}

################################################################################
# Supporting Resources
################################################################################

data "aws_caller_identity" "current" {}
data "aws_region" "current" {}

module "kms" {
source = "terraform-aws-modules/kms/aws"
version = "~> 1.0"
description = "KMS key for log anomaly detection"

key_owners = [data.aws_caller_identity.current.arn]
key_statements = [
{
sid = "CloudWatchLogs"
actions = [
"kms:Encrypt*",
"kms:Decrypt*",
"kms:ReEncrypt*",
"kms:GenerateDataKey*",
"kms:Describe*"
]
resources = ["*"]

principals = [
{
type = "Service"
identifiers = ["logs.${data.aws_region.current.name}.amazonaws.com"]
}
]

conditions = [
{
test = "ArnLike"
variable = "kms:EncryptionContext:aws:logs:arn"
values = [
"arn:aws:logs:${data.aws_region.current.name}:${data.aws_caller_identity.current.account_id}:log-group:${local.name}",
"arn:aws:logs:${data.aws_region.current.name}:${data.aws_caller_identity.current.account_id}:anomaly-detector:*"
]
}
]
},
{
actions = [
"kms:Encrypt*",
"kms:Decrypt*",
"kms:ReEncrypt*",
"kms:GenerateDataKey*",
"kms:Describe*"
]
resources = ["*"]

principals = [
{
type = "Service"
identifiers = ["logs.${data.aws_region.current.name}.amazonaws.com"]
}
]

conditions = [
{
test = "ArnLike"
variable = "kms:EncryptionContext:aws-crypto-ec:aws:logs:arn"
values = [
"arn:aws:logs:${data.aws_region.current.name}:${data.aws_caller_identity.current.account_id}:anomaly-detector:*"
]
}
]
}
]
tags = {
terraform = true
}
}
4 changes: 4 additions & 0 deletions examples/log-anomaly-detector/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
output "cloudwatch_log_anomaly_arn" {
description = "The ARN of the anomaly detector."
value = module.log_anomaly_detector.arn
}
Empty file.
10 changes: 10 additions & 0 deletions examples/log-anomaly-detector/versions.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
terraform {
required_version = ">= 1.0"

required_providers {
aws = {
source = "hashicorp/aws"
version = ">= 5.81"
}
}
}
2 changes: 1 addition & 1 deletion examples/log-group-with-log-stream/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Note that this example may create resources which cost money. Run `terraform des
| Name | Version |
|------|---------|
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 1.0 |
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 5.58 |
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 5.81 |

## Providers

Expand Down
2 changes: 1 addition & 1 deletion examples/log-group-with-log-stream/versions.tf
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = ">= 5.58"
version = ">= 5.81"
}
}
}
2 changes: 1 addition & 1 deletion examples/log-subscription-filter/versions.tf
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = ">= 5.58"
version = ">= 5.81"
}
random = {
source = "hashicorp/random"
Expand Down
4 changes: 2 additions & 2 deletions examples/metric-stream/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ Note that this example may create resources which cost money. Run `terraform des
| Name | Version |
|------|---------|
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 1.0 |
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 5.58 |
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 5.81 |
| <a name="requirement_random"></a> [random](#requirement\_random) | >= 3.5 |

## Providers

| Name | Version |
|------|---------|
| <a name="provider_aws"></a> [aws](#provider\_aws) | >= 5.58 |
| <a name="provider_aws"></a> [aws](#provider\_aws) | >= 5.81 |
| <a name="provider_random"></a> [random](#provider\_random) | >= 3.5 |

## Modules
Expand Down
2 changes: 1 addition & 1 deletion examples/metric-stream/versions.tf
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = ">= 5.58"
version = ">= 5.81"
}
random = {
source = "hashicorp/random"
Expand Down
2 changes: 1 addition & 1 deletion examples/multiple-lambda-metric-alarm/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Note that this example may create resources which cost money. Run `terraform des
| Name | Version |
|------|---------|
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 1.0 |
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 5.58 |
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 5.81 |

## Providers

Expand Down
2 changes: 1 addition & 1 deletion examples/multiple-lambda-metric-alarm/versions.tf
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = ">= 5.58"
version = ">= 5.81"
}
}
}
Loading
Loading