-
Notifications
You must be signed in to change notification settings - Fork 0
/
iam.tf
69 lines (57 loc) · 1.75 KB
/
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
locals {
iam_service_types = ["bus", "event_api", "sfn"]
target_types = [for k, v in var.targets : k if length(v) > 0]
needs_iam = length(setintersection(local.iam_service_types, local.target_types)) > 0
}
data "aws_iam_policy_document" "assume_role_policy" {
statement {
actions = ["sts:AssumeRole"]
principals {
type = "Service"
identifiers = ["events.amazonaws.com"]
}
}
}
resource "aws_iam_role" "event_role" {
count = local.needs_iam ? 1 : 0
name = local.name
assume_role_policy = data.aws_iam_policy_document.assume_role_policy.json
}
data "aws_iam_policy_document" "sfn_policy" {
statement {
actions = ["states:StartExecution"]
resources = values(var.targets.sfn)
}
}
data "aws_iam_policy_document" "bus_policy" {
statement {
actions = ["events:PutEvents"]
resources = values(var.targets.bus)
}
}
data "aws_iam_policy_document" "api_event_invoke" {
statement {
actions = [
"events:InvokeApiDestination"
]
resources = [for k, v in aws_cloudwatch_event_api_destination.destination : v.arn]
}
}
resource "aws_iam_role_policy" "api_events" {
count = length(var.targets.event_api) > 0 ? 1 : 0
name = "invoke-api"
role = aws_iam_role.event_role[0].id
policy = data.aws_iam_policy_document.api_event_invoke.json
}
resource "aws_iam_role_policy" "bus_events" {
count = length(var.targets.bus) > 0 ? 1 : 0
name = "invoke-bus"
role = aws_iam_role.event_role[0].id
policy = data.aws_iam_policy_document.bus_policy.json
}
resource "aws_iam_role_policy" "sfn_events" {
count = length(var.targets.sfn) > 0 ? 1 : 0
name = "invoke-sfn"
role = aws_iam_role.event_role[0].id
policy = data.aws_iam_policy_document.sfn_policy.json
}