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

Made cloudwatch resources optional #43

Merged
merged 3 commits into from
Aug 20, 2023
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ In addition you have the option to create or not :
| autoscaling\_scale\_out\_cooldown | Cooldown in seconds to wait between scale out events | `number` | `300` | no |
| autoscaling\_target\_cpu | Target average CPU percentage to track for autoscaling | `number` | `50` | no |
| autoscaling\_target\_memory | Target average Memory percentage to track for autoscaling | `number` | `90` | no |
| cloudwatch\_logs\_create | Whether to create cloudwatch log resources or not | `bool` | `true` | no |
| cloudwatch\_logs\_export | Whether to mark the log group to export to an S3 bucket (needs terraform-aws-log-exporter to be deployed in the account/region) | `bool` | `false` | no |
| cloudwatch\_logs\_retention | Specifies the number of days you want to retain log events in the specified log group. Possible values are: 1, 3, 5, 7, 14, 30, 60, 90, 120, 150, 180, 365, 400, 545, 731, 1827, and 3653. | `number` | `120` | no |
| cluster\_name | n/a | `string` | `"Name of existing ECS Cluster to deploy this app to"` | no |
Expand Down
5 changes: 5 additions & 0 deletions _variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,11 @@ variable "codedeploy_deployment_config_name" {
description = "Specifies the deployment configuration for CodeDeploy"
}

variable "cloudwatch_logs_create" {
default = true
description = "Whether to create cloudwatch log resources or not"
}

variable "cloudwatch_logs_retention" {
default = 120
description = "Specifies the number of days you want to retain log events in the specified log group. Possible values are: 1, 3, 5, 7, 14, 30, 60, 90, 120, 150, 180, 365, 400, 545, 731, 1827, and 3653."
Expand Down
13 changes: 9 additions & 4 deletions cloudwatch-ecs-event-logs.tf
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
resource "aws_cloudwatch_log_group" "ecs_events" {
count = var.cloudwatch_logs_create ? 1 : 0
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to disable the creation of a new CW log group or the aws_cloudwatch_log_resource_policy only?

name = "/ecs/events/${var.cluster_name}/${var.name}"
retention_in_days = var.cloudwatch_logs_retention
tags = {
Expand All @@ -8,6 +9,7 @@ resource "aws_cloudwatch_log_group" "ecs_events" {


resource "aws_cloudwatch_event_rule" "ecs_events" {
count = var.cloudwatch_logs_create ? 1 : 0
name = "capture-ecs-events-${var.cluster_name}-${var.name}"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if I want to use an existing aws_cloudwatch_log_resource_policy?

description = "Capture ecs service events from ${var.cluster_name}-${var.name}"
event_pattern = <<EOF
Expand All @@ -23,19 +25,21 @@ EOF
}

resource "aws_cloudwatch_event_target" "ecs_events" {
rule = aws_cloudwatch_event_rule.ecs_events.name
arn = aws_cloudwatch_log_group.ecs_events.arn
count = var.cloudwatch_logs_create ? 1 : 0
rule = aws_cloudwatch_event_rule.ecs_events[0].name
arn = aws_cloudwatch_log_group.ecs_events[0].arn
}

data "aws_iam_policy_document" "ecs_events" {
count = var.cloudwatch_logs_create ? 1 : 0
statement {
actions = [
"logs:CreateLogStream",
"logs:PutLogEvents",
"logs:PutLogEventsBatch",
]

resources = ["${aws_cloudwatch_log_group.ecs_events.arn}:*"]
resources = ["${aws_cloudwatch_log_group.ecs_events[0].arn}:*"]

principals {
identifiers = ["events.amazonaws.com", "delivery.logs.amazonaws.com"]
Expand All @@ -45,6 +49,7 @@ data "aws_iam_policy_document" "ecs_events" {
}

resource "aws_cloudwatch_log_resource_policy" "ecs_events" {
policy_document = data.aws_iam_policy_document.ecs_events.json
count = var.cloudwatch_logs_create ? 1 : 0
policy_document = data.aws_iam_policy_document.ecs_events[0].json
policy_name = "capture-ecs-events-${var.cluster_name}-${var.name}"
}