Skip to content

Commit

Permalink
Merge pull request #50 from DNXLabs/adding_tags
Browse files Browse the repository at this point in the history
Adding tags
  • Loading branch information
mvsnogueira-dnx committed Jun 12, 2024
2 parents 07f59d2 + d615773 commit a9c5206
Show file tree
Hide file tree
Showing 14 changed files with 150 additions and 4 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ In addition you have the option to create or not :
| ssm\_variables | Map of variables and SSM locations to add to the task definition | `map(string)` | `{}` | no |
| static\_variables | Map of variables and static values to add to the task definition | `map(string)` | `{}` | no |
| subnets | The subnets associated with the task or service. (REQUIRED IF 'LAUCH\_TYPE' IS FARGATE) | `any` | `null` | no |
| tags | Map of tags that will be added to created resources. By default resources will be tagged with terraform=true. | `map(string)` | `{}` | no |
| task\_definition\_arn | Task definition to use for this service (optional) | `string` | `""` | no |
| task\_role\_arn | Existing task role ARN created by ECS cluster module | `any` | `null` | no |
| task\_role\_policies | Custom policies to be added on the task role. | `list` | `[]` | no |
Expand Down
7 changes: 7 additions & 0 deletions _variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,13 @@ variable "compat_keep_target_group_naming" {
description = "Keeps old naming convention for target groups to avoid recreation of resource in production environments"
}


variable "tags" {
description = "Map of tags that will be added to created resources. By default resources will be tagged with terraform=true."
type = map(string)
default = {}
}

variable "launch_type" {
default = "EC2"
description = "The launch type on which to run your service. The valid values are EC2 and FARGATE. Defaults to EC2."
Expand Down
36 changes: 36 additions & 0 deletions alb-target-group.tf
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,13 @@ resource "aws_lb_listener_rule" "green" {
aws_lb_listener_rule.green_auth_oidc[0].priority + 1, var.alb_priority != 0 ? var.alb_priority : null
)
)

tags = merge(
var.tags,
{
"Terraform" = true
},
)
}

resource "aws_lb_listener_rule" "blue" {
Expand Down Expand Up @@ -90,6 +97,14 @@ resource "aws_lb_listener_rule" "blue" {
}

priority = var.alb_priority != 0 ? var.alb_priority + 1 : null

tags = merge(
var.tags,
{
"Terraform" = true
},
)

}

resource "aws_lb_listener_rule" "redirects" {
Expand Down Expand Up @@ -138,6 +153,13 @@ resource "aws_lb_listener_rule" "path_redirects" {
priority = try(aws_lb_listener_rule.green_auth_oidc[0].priority + 1,
var.alb_priority != 0 ? var.alb_priority : null
)

tags = merge(
var.tags,
{
"Terraform" = true
},
)
}


Expand Down Expand Up @@ -177,6 +199,13 @@ resource "aws_lb_target_group" "green" {
type = stickiness.value.type
}
}

tags = merge(
var.tags,
{
"Terraform" = true
},
)
}

resource "aws_lb_target_group" "blue" {
Expand Down Expand Up @@ -207,4 +236,11 @@ resource "aws_lb_target_group" "blue" {
type = stickiness.value.type
}
}

tags = merge(
var.tags,
{
"Terraform" = true
},
)
}
22 changes: 21 additions & 1 deletion cloudwatch-alarms.tf
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ resource "aws_cloudwatch_metric_alarm" "min_healthy_tasks" {
insufficient_data_actions = []
treat_missing_data = "ignore"

tags = merge(
var.tags,
{
"Terraform" = true
},
)

metric_query {
id = "e1"
expression = "MAX(REMOVE_EMPTY([m1, m2]))"
Expand All @@ -33,6 +40,8 @@ resource "aws_cloudwatch_metric_alarm" "min_healthy_tasks" {
TargetGroup = aws_lb_target_group.blue.arn_suffix
}
}


}

metric_query {
Expand Down Expand Up @@ -72,6 +81,12 @@ resource "aws_cloudwatch_metric_alarm" "high_cpu_usage" {
statistic = "Average"
unit = "Percent"

tags = merge(
var.tags,
{
"Terraform" = true
},
)
dimensions = {
ClusterName = var.cluster_name
ServiceName = aws_ecs_service.default.name
Expand All @@ -95,7 +110,12 @@ resource "aws_cloudwatch_metric_alarm" "ecs_running_tasks" {
ok_actions = var.alarm_sns_topics
insufficient_data_actions = []
treat_missing_data = "ignore"

tags = merge(
var.tags,
{
"Terraform" = true
},
)
dimensions = {
ClusterName = var.cluster_name
ServiceName = aws_ecs_service.default.name
Expand Down
7 changes: 7 additions & 0 deletions cloudwatch-ecs-event-logs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ resource "aws_cloudwatch_event_rule" "ecs_events" {
}
}
EOF
tags = merge(
var.tags,
{
"Terraform" = true
},
)
}

resource "aws_cloudwatch_event_target" "ecs_events" {
Expand Down Expand Up @@ -52,4 +58,5 @@ resource "aws_cloudwatch_log_resource_policy" "ecs_events" {
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}"

}
11 changes: 8 additions & 3 deletions cloudwatch-logs.tf
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
resource "aws_cloudwatch_log_group" "default" {
name = "/ecs/${var.cluster_name}/${var.name}"
retention_in_days = var.cloudwatch_logs_retention
tags = {
ExportToS3 = var.cloudwatch_logs_export
}
tags = merge(
var.tags,
{
ExportToS3 = var.cloudwatch_logs_export
}
)
}


15 changes: 15 additions & 0 deletions codedeploy.tf
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@ resource "aws_codedeploy_app" "ecs" {
count = var.deployment_controller == "CODE_DEPLOY" ? 1 : 0
compute_platform = "ECS"
name = "${var.cluster_name}-${var.name}"
tags = merge(
var.tags,
{
"terraform" = "true"
},
)

}

resource "aws_codedeploy_deployment_group" "ecs" {
Expand Down Expand Up @@ -58,4 +65,12 @@ resource "aws_codedeploy_deployment_group" "ecs" {
}
}
}

tags = merge(
var.tags,
{
"terraform" = "true"
},
)

}
8 changes: 8 additions & 0 deletions ecs-service.tf
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,12 @@ resource "aws_ecs_service" "default" {
aws_lb_listener_rule.green,
aws_lb_listener_rule.blue
]

tags = merge(
var.tags,
{
"terraform" = "true"
},
)

}
8 changes: 8 additions & 0 deletions ecs-task-definition.tf
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,12 @@ resource "aws_ecs_task_definition" "default" {
container_definitions
]
}

tags = merge(
var.tags,
{
"terraform" = "true"
},
)

}
8 changes: 8 additions & 0 deletions efs-access-point.tf
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,12 @@ resource "aws_efs_access_point" "default" {
}
path = "/${var.name}"
}

tags = merge(
var.tags,
{
"terraform" = "true"
},
)

}
8 changes: 8 additions & 0 deletions iam-codedeploy.tf
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ resource "aws_iam_role" "codedeploy_service" {
]
}
EOF

tags = merge(
var.tags,
{
"terraform" = "true"
},
)

}

resource "aws_iam_role_policy_attachment" "codedeploy_service" {
Expand Down
8 changes: 8 additions & 0 deletions iam-ecs-service.tf
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ resource "aws_iam_role" "ecs_service" {
]
}
EOF

tags = merge(
var.tags,
{
"terraform" = "true"
},
)

}

data "aws_iam_policy_document" "ecs_service_policy" {
Expand Down
8 changes: 8 additions & 0 deletions iam-ecs-task-attach.tf
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,18 @@ resource "aws_iam_policy" "task_role_policy_custom" {
name = "ecs-${each.value.name}-${var.cluster_name}-${var.name}-${data.aws_region.current.name}"
description = try(each.value.description, "")
policy = data.aws_iam_policy_document.task_role_policy_custom[each.value.name].json

tags = merge(
var.tags,
{
"terraform" = "true"
},
)
}

resource "aws_iam_role_policy_attachment" "task_role_attach_policy_custom" {
for_each = { for policy in try(var.task_role_policies, []) : policy.name => policy }
role = aws_iam_role.ecs_task[0].name
policy_arn = aws_iam_policy.task_role_policy_custom[each.value.name].arn

}
7 changes: 7 additions & 0 deletions iam-ecs-task.tf
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ resource "aws_iam_role" "ecs_task" {
]
}
EOF

tags = merge(
var.tags,
{
"terraform" = "true"
},
)
}

resource "aws_iam_role_policy_attachment" "ecs_task" {
Expand Down

0 comments on commit a9c5206

Please sign in to comment.