Skip to content

Commit a6ab7e8

Browse files
authored
feat: Automatically run the exporter when the queue is empty. (#41)
1 parent 669a888 commit a6ab7e8

File tree

9 files changed

+97
-7
lines changed

9 files changed

+97
-7
lines changed

tofu/modules/ephemeral_service/outputs.tf

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,17 @@ docker push ${module.ecr.repository_url}:latest
1313
EOT
1414
}
1515

16+
output "execution_role_arn" {
17+
description = "ARN of the ECS task execution role."
18+
value = aws_iam_role.execution.arn
19+
}
20+
1621
output "task_definition_arn" {
1722
description = "ARN of the ECS task definition."
1823
value = module.ecs_task.arn
1924
}
25+
26+
output "task_role_arn" {
27+
description = "ARN of the ECS task role."
28+
value = aws_iam_role.task.arn
29+
}

tofu/modules/system/alarms.tf

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,43 @@ resource "aws_cloudwatch_metric_alarm" "queue_empty" {
4545

4646
tags = var.tags
4747
}
48+
49+
resource "aws_cloudwatch_event_rule" "export" {
50+
name = "${local.prefix}-queue-empty-export"
51+
description = "Run the exporter task when the ingestion queue is empty."
52+
force_destroy = !var.deletion_protection
53+
54+
event_pattern = jsonencode({
55+
source = ["aws.cloudwatch"],
56+
detail-type = ["CloudWatch Alarm State Change"],
57+
resources = [aws_cloudwatch_metric_alarm.queue_empty.arn],
58+
detail = {
59+
state = { value = ["ALARM"] },
60+
previousState = { value = ["OK", "INSUFFICIENT_DATA"] }
61+
}
62+
})
63+
64+
tags = var.tags
65+
}
66+
67+
resource "aws_cloudwatch_event_target" "exporter" {
68+
rule = aws_cloudwatch_event_rule.export.name
69+
arn = module.ecs.arn
70+
role_arn = aws_iam_role.eventbridge.arn
71+
force_destroy = !var.deletion_protection
72+
target_id = "export"
73+
74+
ecs_target {
75+
task_definition_arn = module.exporter.task_definition_arn
76+
launch_type = "FARGATE"
77+
task_count = 1
78+
propagate_tags = "TASK_DEFINITION"
79+
enable_ecs_managed_tags = true
80+
81+
network_configuration {
82+
subnets = var.container_subnets
83+
security_groups = [module.task_security_group.security_group_id]
84+
assign_public_ip = false
85+
}
86+
}
87+
}

tofu/modules/system/iam.tf

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,28 @@ resource "aws_iam_policy" "secrets" {
5151
create_before_destroy = true
5252
}
5353
}
54+
55+
resource "aws_iam_role" "eventbridge" {
56+
name = "${local.prefix}-eventbridge-run-task"
57+
assume_role_policy = jsonencode({
58+
Version = "2012-10-17",
59+
Statement = [{
60+
Effect = "Allow",
61+
Principal = { Service = "events.amazonaws.com" },
62+
Action = "sts:AssumeRole"
63+
}]
64+
})
65+
66+
tags = var.tags
67+
}
68+
69+
resource "aws_iam_role_policy" "eventbridge" {
70+
name = "${local.prefix}-eventbridge-run-task"
71+
role = aws_iam_role.eventbridge.id
72+
73+
policy = jsonencode(yamldecode(templatefile("${path.module}/templates/eventbridge-policy.yaml.tftpl", {
74+
export_task_arn = module.exporter.task_definition_arn
75+
execution_role_arn = module.exporter.execution_role_arn
76+
task_role_arn = module.exporter.task_role_arn
77+
})))
78+
}

tofu/modules/system/templates/aws-otel-config.yaml.tftpl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -180,11 +180,11 @@ exporters:
180180
awsxray:
181181
awsemf/application:
182182
namespace: "${app_namespace}"
183-
log_group_name: '/aws/ecs/application/metrics'
183+
log_group_name: /aws/ecs/application/metrics
184184
awsemf/performance:
185185
namespace: ECS/ContainerInsights
186-
log_group_name: '/aws/ecs/containerinsights/{ClusterName}/performance'
187-
log_stream_name: '{TaskId}'
186+
log_group_name: "/aws/ecs/containerinsights/{ClusterName}/performance"
187+
log_stream_name: "{TaskId}"
188188
resource_to_telemetry_conversion:
189189
enabled: true
190190
dimension_rollup_option: NoDimensionRollup

tofu/modules/system/templates/container-key-policy.yaml.tftpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Version: '2012-10-17'
1+
Version: "2012-10-17"
22
Id: Encryption key for Senzing containers.
33
Statement:
44
- Sid: Enable IAM User Permissions
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
Version: "2012-10-17"
2+
Statement:
3+
- Action:
4+
- ecs:RunTask
5+
Effect: Allow
6+
Resource:
7+
- ${export_task_arn}
8+
Sid: RunTask
9+
- Action:
10+
- iam:PassRole
11+
Effect: Allow
12+
Resource:
13+
- ${execution_role_arn}
14+
- ${task_role_arn}
15+
Sid: PassRoles

tofu/modules/system/templates/exports-access-policy.yaml.tftpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Version: '2012-10-17'
1+
Version: "2012-10-17"
22
Statement:
33
- Sid: KeyAccess
44
Effect: Allow

tofu/modules/system/templates/queue-access-policy.yaml.tftpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Version: '2012-10-17'
1+
Version: "2012-10-17"
22
Statement:
33
- Sid: KeyAccess
44
Effect: Allow

tofu/modules/system/templates/secrets-access-policy.yaml.tftpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Version: '2012-10-17'
1+
Version: "2012-10-17"
22
Statement:
33
- Sid: KeyAccess
44
Effect: Allow

0 commit comments

Comments
 (0)