-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
build(terraform): Add CloudWatch Modules
- Loading branch information
Showing
22 changed files
with
766 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
variable "kms" { | ||
type = object({ | ||
arn = string | ||
id = string | ||
}) | ||
description = "KMS key used to encrypt the resources." | ||
} | ||
|
||
variable "config" { | ||
type = object({ | ||
name = string | ||
destination_arn = string | ||
account_ids = optional(list(string), []) | ||
}) | ||
|
||
description = "Configuration for the CloudWatch destination." | ||
} | ||
|
||
variable "tags" { | ||
type = map(any) | ||
default = {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
terraform { | ||
required_version = "~> 1.5" | ||
|
||
required_providers { | ||
aws = { | ||
source = "hashicorp/aws" | ||
version = "~> 5.0" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
data "aws_region" "current" {} | ||
|
||
data "aws_caller_identity" "current" {} | ||
|
||
locals { | ||
# By default, the current account is included in the list of accounts. | ||
account_ids = concat(var.config.account_ids, [data.aws_caller_identity.current.account_id]) | ||
} | ||
|
||
data "aws_iam_policy_document" "destination_assume_role" { | ||
statement { | ||
effect = "Allow" | ||
actions = ["sts:AssumeRole"] | ||
principals { | ||
type = "Service" | ||
identifiers = [ | ||
"logs.amazonaws.com" | ||
] | ||
} | ||
|
||
condition { | ||
test = "StringLike" | ||
variable = "aws:SourceArn" | ||
|
||
# Creates a list of wildcarded ARNs for each account. | ||
values = formatlist("arn:aws:logs:*:%s:*", local.account_ids) | ||
} | ||
} | ||
} | ||
|
||
data "aws_iam_policy_document" "destination" { | ||
statement { | ||
effect = "Allow" | ||
actions = [ | ||
"kms:Decrypt", | ||
"kms:GenerateDataKey" | ||
] | ||
|
||
// Access the KMS key. | ||
resources = [ | ||
var.kms.arn, | ||
] | ||
} | ||
|
||
// If the destination is Kinesis Firehose, the role must have write access. | ||
dynamic "statement" { | ||
for_each = strcontains(var.config.destination_arn, "arn:aws:firehose:") ? [1] : [] | ||
|
||
content { | ||
effect = "Allow" | ||
actions = [ | ||
"firehose:DescribeDeliveryStream", | ||
"firehose:PutRecord", | ||
"firehose:PutRecordBatch", | ||
] | ||
|
||
resources = [ | ||
var.config.destination_arn, | ||
] | ||
} | ||
} | ||
|
||
// If the destination is Kinesis Data Stream, the role must have write access. | ||
dynamic "statement" { | ||
for_each = strcontains(var.config.destination_arn, "arn:aws:kinesis:") ? [1] : [] | ||
|
||
content { | ||
effect = "Allow" | ||
actions = [ | ||
"kinesis:DescribeStream", | ||
"kinesis:DescribeStreamSummary", | ||
"kinesis:DescribeStreamConsumer", | ||
"kinesis:SubscribeToShard", | ||
"kinesis:RegisterStreamConsumer", | ||
"kinesis:PutRecord", | ||
"kinesis:PutRecords", | ||
] | ||
|
||
resources = [ | ||
var.config.destination_arn, | ||
] | ||
} | ||
} | ||
} | ||
|
||
resource "aws_iam_role" "destination" { | ||
name = "sub-cloudwatch-destination-${var.config.name}-${data.aws_region.current.name}" | ||
assume_role_policy = data.aws_iam_policy_document.destination_assume_role.json | ||
tags = var.tags | ||
} | ||
|
||
resource "aws_iam_role_policy_attachment" "destination" { | ||
role = aws_iam_role.destination.name | ||
policy_arn = aws_iam_policy.destination.arn | ||
} | ||
|
||
resource "aws_iam_policy" "destination" { | ||
name = "sub-cloudwatch-destination-${var.config.name}-${data.aws_region.current.name}" | ||
description = "Policy for the ${var.config.name} CloudWatch destination." | ||
policy = data.aws_iam_policy_document.destination.json | ||
} | ||
|
||
resource "aws_cloudwatch_log_destination" "destination" { | ||
name = var.config.name | ||
role_arn = aws_iam_role.destination.arn | ||
target_arn = var.config.destination_arn | ||
} | ||
|
||
data "aws_iam_policy_document" "destination_access" { | ||
statement { | ||
effect = "Allow" | ||
principals { | ||
type = "AWS" | ||
identifiers = local.account_ids | ||
} | ||
|
||
actions = [ | ||
"logs:PutSubscriptionFilter", | ||
] | ||
resources = [ | ||
aws_cloudwatch_log_destination.destination.arn, | ||
] | ||
} | ||
} | ||
|
||
resource "aws_cloudwatch_log_destination_policy" "destination" { | ||
destination_name = aws_cloudwatch_log_destination.destination.name | ||
access_policy = data.aws_iam_policy_document.destination_access.json | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
output "role" { | ||
value = aws_iam_role.destination | ||
} | ||
|
||
output "arn" { | ||
value = aws_cloudwatch_log_destination.destination.arn | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
variable "config" { | ||
type = object({ | ||
name = string | ||
destination_arn = string | ||
log_groups = list(string) | ||
filter_pattern = optional(string, "") | ||
|
||
}) | ||
|
||
description = "Configuration for the CloudWatch subscription filter." | ||
} | ||
|
||
variable "tags" { | ||
type = map(any) | ||
default = {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
terraform { | ||
required_version = "~> 1.2" | ||
|
||
required_providers { | ||
aws = { | ||
source = "hashicorp/aws" | ||
version = "~> 5.0" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
resource "aws_cloudwatch_log_subscription_filter" "subscription_filter" { | ||
for_each = toset(var.config.log_groups) | ||
log_group_name = each.key | ||
|
||
name = var.config.name | ||
destination_arn = var.config.destination_arn | ||
# By default there is no filter pattern, so all logs are sent to the destination. | ||
filter_pattern = var.config.filter_pattern | ||
|
||
# If the destination is a Kinesis stream, then randomly distribute the logs to avoid hot shards. | ||
distribution = "Random" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.