-
Notifications
You must be signed in to change notification settings - Fork 1
/
schedule.iam.tf
39 lines (34 loc) · 1.25 KB
/
schedule.iam.tf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
resource "aws_iam_role" "event_dispatcher" {
name = "ecs-events-${var.environment_name}-${var.application_name}"
assume_role_policy = data.aws_iam_policy_document.events_assume.json
}
data "aws_iam_policy_document" "events_assume" {
statement {
actions = ["sts:AssumeRole"]
principals {
type = "Service"
identifiers = ["events.amazonaws.com"]
}
}
}
resource "aws_iam_role_policy" "event_dispatch" {
count = ((length(local.scheduled_tasks) + length(local.reactive_tasks)) > 0) ? 1 : 0
name = "event-dispatch"
policy = data.aws_iam_policy_document.event_dispatch.json
role = aws_iam_role.event_dispatcher.id
}
data "aws_iam_policy_document" "event_dispatch" {
# Allow EventBridge to run a task
statement {
actions = ["ecs:RunTask"]
resources = [
"arn:aws:ecs:${data.aws_region.current.name}:${data.aws_caller_identity.current.account_id}:task-definition/${local.common_name}", # Grouped services
"arn:aws:ecs:${data.aws_region.current.name}:${data.aws_caller_identity.current.account_id}:task-definition/${local.common_name}-*", # Separated services
]
}
# Let EventBridge pass assign an IAM role to the task
statement {
actions = ["iam:PassRole"]
resources = [aws_iam_role.execute.arn]
}
}