Skip to content

Commit 4cd1a76

Browse files
authored
Avoids creation of redundant resources when providing custom Events role. (#29)
1 parent 06bb293 commit 4cd1a76

File tree

2 files changed

+15
-15
lines changed

2 files changed

+15
-15
lines changed

main.tf

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,15 @@ data "aws_iam_policy_document" "scheduled_task_cw_event_role_cloudwatch_policy"
3030
}
3131

3232
resource "aws_iam_role" "scheduled_task_cw_event_role" {
33+
count = var.event_rule_role_arn == null ? 1 : 0
3334
name = "${var.name_prefix}-st-cw-role"
3435
assume_role_policy = data.aws_iam_policy_document.scheduled_task_cw_event_role_assume_role_policy.json
3536
}
3637

3738
resource "aws_iam_role_policy" "scheduled_task_cw_event_role_cloudwatch_policy" {
39+
count = var.event_rule_role_arn == null ? 1 : 0
3840
name = "${var.name_prefix}-st-cw-policy"
39-
role = aws_iam_role.scheduled_task_cw_event_role.id
41+
role = aws_iam_role.scheduled_task_cw_event_role[0].id
4042
policy = data.aws_iam_policy_document.scheduled_task_cw_event_role_cloudwatch_policy.json
4143
}
4244

@@ -49,7 +51,7 @@ resource "aws_cloudwatch_event_rule" "event_rule" {
4951
event_bus_name = var.event_rule_event_bus_name
5052
event_pattern = var.event_rule_event_pattern
5153
description = var.event_rule_description
52-
role_arn = var.event_rule_role_arn
54+
role_arn = var.event_rule_role_arn == null ? aws_iam_role.scheduled_task_cw_event_role[0].arn : var.event_rule_role_arn
5355
is_enabled = var.event_rule_is_enabled
5456
tags = {
5557
Name = "${var.name_prefix}-cw-event-rule"
@@ -66,8 +68,7 @@ resource "aws_cloudwatch_event_target" "ecs_scheduled_task" {
6668
arn = var.ecs_cluster_arn
6769
input = var.event_target_input
6870
input_path = var.event_target_input_path
69-
role_arn = aws_iam_role.scheduled_task_cw_event_role.arn
70-
71+
role_arn = aws_cloudwatch_event_rule.event_rule.role_arn
7172
ecs_target {
7273
group = var.event_target_ecs_target_group
7374
launch_type = "FARGATE"

outputs.tf

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,52 +3,51 @@
33
#------------------------------------------------------------------------------
44
output "scheduled_task_cw_event_role_arn" {
55
description = "The Amazon Resource Name (ARN) specifying the role."
6-
value = aws_iam_role.scheduled_task_cw_event_role.arn
6+
value = var.event_rule_role_arn == null ? aws_iam_role.scheduled_task_cw_event_role[0].arn : null
77
}
88

99
output "scheduled_task_cw_event_role_create_date" {
1010
description = "The creation date of the IAM role."
11-
value = aws_iam_role.scheduled_task_cw_event_role.create_date
11+
value = var.event_rule_role_arn == null ? aws_iam_role.scheduled_task_cw_event_role[0].create_date : null
1212
}
1313

1414
output "scheduled_task_cw_event_role_description" {
1515
description = "The description of the role."
16-
value = aws_iam_role.scheduled_task_cw_event_role.description
16+
value = var.event_rule_role_arn == null ? aws_iam_role.scheduled_task_cw_event_role[0].description : null
1717
}
1818

1919
output "scheduled_task_cw_event_role_id" {
2020
description = "The name of the role."
21-
value = aws_iam_role.scheduled_task_cw_event_role.id
21+
value = var.event_rule_role_arn == null ? aws_iam_role.scheduled_task_cw_event_role[0].id : null
2222
}
2323

2424
output "scheduled_task_cw_event_role_name" {
2525
description = "The name of the role."
26-
value = aws_iam_role.scheduled_task_cw_event_role.name
26+
value = var.event_rule_role_arn == null ? aws_iam_role.scheduled_task_cw_event_role[0].name : null
2727
}
2828

2929
output "scheduled_task_cw_event_role_unique_id" {
3030
description = "The stable and unique string identifying the role."
31-
value = aws_iam_role.scheduled_task_cw_event_role.unique_id
31+
value = var.event_rule_role_arn == null ? aws_iam_role.scheduled_task_cw_event_role[0].unique_id : null
3232
}
33-
3433
output "aws_iam_role_policy_scheduled_task_cw_event_role_cloudwatch_policy_id" {
3534
description = "The role policy ID, in the form of role_name:role_policy_name."
36-
value = aws_iam_role_policy.scheduled_task_cw_event_role_cloudwatch_policy.id
35+
value = var.event_rule_role_arn == null ? aws_iam_role_policy.scheduled_task_cw_event_role_cloudwatch_policy[0].id : null
3736
}
3837

3938
output "aws_iam_role_policy_scheduled_task_cw_event_role_cloudwatch_policy_name" {
4039
description = "The name of the policy."
41-
value = aws_iam_role_policy.scheduled_task_cw_event_role_cloudwatch_policy.name
40+
value = var.event_rule_role_arn == null ? aws_iam_role_policy.scheduled_task_cw_event_role_cloudwatch_policy[0].name : null
4241
}
4342

4443
output "aws_iam_role_policy_scheduled_task_cw_event_role_cloudwatch_policy_policy" {
4544
description = "The policy document attached to the role."
46-
value = aws_iam_role_policy.scheduled_task_cw_event_role_cloudwatch_policy.policy
45+
value = var.event_rule_role_arn == null ? aws_iam_role_policy.scheduled_task_cw_event_role_cloudwatch_policy[0].policy : null
4746
}
4847

4948
output "aws_iam_role_policy_scheduled_task_cw_event_role_cloudwatch_policy_role" {
5049
description = "The name of the role associated with the policy."
51-
value = aws_iam_role_policy.scheduled_task_cw_event_role_cloudwatch_policy.role
50+
value = var.event_rule_role_arn == null ? aws_iam_role_policy.scheduled_task_cw_event_role_cloudwatch_policy[0].role : null
5251
}
5352

5453
#------------------------------------------------------------------------------

0 commit comments

Comments
 (0)