diff --git a/README.md b/README.md index b0fe885..944155d 100644 --- a/README.md +++ b/README.md @@ -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 | diff --git a/_variables.tf b/_variables.tf index c2a3320..09b1488 100644 --- a/_variables.tf +++ b/_variables.tf @@ -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." diff --git a/alb-target-group.tf b/alb-target-group.tf index c71fc9f..57cebce 100644 --- a/alb-target-group.tf +++ b/alb-target-group.tf @@ -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" { @@ -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" { @@ -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 + }, + ) } @@ -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" { @@ -207,4 +236,11 @@ resource "aws_lb_target_group" "blue" { type = stickiness.value.type } } + + tags = merge( + var.tags, + { + "Terraform" = true + }, + ) } \ No newline at end of file diff --git a/cloudwatch-alarms.tf b/cloudwatch-alarms.tf index eec07da..7b6fa74 100644 --- a/cloudwatch-alarms.tf +++ b/cloudwatch-alarms.tf @@ -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]))" @@ -33,6 +40,8 @@ resource "aws_cloudwatch_metric_alarm" "min_healthy_tasks" { TargetGroup = aws_lb_target_group.blue.arn_suffix } } + + } metric_query { @@ -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 @@ -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 diff --git a/cloudwatch-ecs-event-logs.tf b/cloudwatch-ecs-event-logs.tf index 292dff4..ef3c9bd 100644 --- a/cloudwatch-ecs-event-logs.tf +++ b/cloudwatch-ecs-event-logs.tf @@ -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" { @@ -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}" + } \ No newline at end of file diff --git a/cloudwatch-logs.tf b/cloudwatch-logs.tf index 011941f..f337335 100644 --- a/cloudwatch-logs.tf +++ b/cloudwatch-logs.tf @@ -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 + } + ) } + + diff --git a/codedeploy.tf b/codedeploy.tf index 638ba74..82fddd7 100644 --- a/codedeploy.tf +++ b/codedeploy.tf @@ -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" { @@ -58,4 +65,12 @@ resource "aws_codedeploy_deployment_group" "ecs" { } } } + + tags = merge( + var.tags, + { + "terraform" = "true" + }, + ) + } \ No newline at end of file diff --git a/ecs-service.tf b/ecs-service.tf index a4fc035..4cc5839 100644 --- a/ecs-service.tf +++ b/ecs-service.tf @@ -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" + }, + ) + } diff --git a/ecs-task-definition.tf b/ecs-task-definition.tf index 96d3255..29e3efa 100644 --- a/ecs-task-definition.tf +++ b/ecs-task-definition.tf @@ -64,4 +64,12 @@ resource "aws_ecs_task_definition" "default" { container_definitions ] } + + tags = merge( + var.tags, + { + "terraform" = "true" + }, + ) + } diff --git a/efs-access-point.tf b/efs-access-point.tf index 5e8b3d9..ed6cbd1 100644 --- a/efs-access-point.tf +++ b/efs-access-point.tf @@ -9,4 +9,12 @@ resource "aws_efs_access_point" "default" { } path = "/${var.name}" } + + tags = merge( + var.tags, + { + "terraform" = "true" + }, + ) + } diff --git a/iam-codedeploy.tf b/iam-codedeploy.tf index c3d8027..95a3502 100644 --- a/iam-codedeploy.tf +++ b/iam-codedeploy.tf @@ -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" { diff --git a/iam-ecs-service.tf b/iam-ecs-service.tf index e439cd9..101e0a7 100644 --- a/iam-ecs-service.tf +++ b/iam-ecs-service.tf @@ -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" { diff --git a/iam-ecs-task-attach.tf b/iam-ecs-task-attach.tf index b958b8d..d583421 100644 --- a/iam-ecs-task-attach.tf +++ b/iam-ecs-task-attach.tf @@ -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 + } \ No newline at end of file diff --git a/iam-ecs-task.tf b/iam-ecs-task.tf index d55625b..bf3b6b1 100644 --- a/iam-ecs-task.tf +++ b/iam-ecs-task.tf @@ -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" {