-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathiam.tf
146 lines (122 loc) · 3.76 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
data "aws_partition" "this" {}
data "aws_caller_identity" "this" {}
data "aws_region" "this" {}
locals {
partition = data.aws_partition.this.partition
account_id = data.aws_caller_identity.this.account_id
region = data.aws_region.this.name
}
###################################################
# IAM Role for Event Bus Rule
###################################################
module "role" {
count = var.default_execution_role.enabled ? 1 : 0
source = "tedilabs/account/aws//modules/iam-role"
version = "~> 0.28.0"
name = coalesce(
var.default_execution_role.name,
"aws-eventbridge-${var.event_bus}-rule-${var.name}"
)
path = var.default_execution_role.path
description = var.default_execution_role.description
trusted_service_policies = [
{
services = ["events.amazonaws.com"]
conditions = [{
key = "aws:SourceAccount"
condition = "StringEquals"
values = [local.account_id]
}]
}
]
policies = var.default_execution_role.policies
inline_policies = merge(
(one(data.aws_iam_policy_document.event_bus) != null
? {
"event-bus-targets" = one(data.aws_iam_policy_document.event_bus).json
}
: {}
),
(one(data.aws_iam_policy_document.ssm_run_commands) != null
? {
"ssm-run-command-targets" = one(data.aws_iam_policy_document.ssm_run_commands).json
}
: {}
),
var.default_execution_role.inline_policies
)
force_detach_policies = true
resource_group_enabled = false
module_tags_enabled = false
tags = merge(
local.module_tags,
var.tags,
)
}
###################################################
# Resource Policy for Event Bus Rule
###################################################
data "aws_iam_policy_document" "event_bus" {
count = (var.default_execution_role.enabled && length(var.event_bus_targets) > 0) ? 1 : 0
statement {
sid = "AllowEventBusTargets"
effect = "Allow"
actions = ["events:PutEvents"]
resources = var.event_bus_targets[*].event_bus
}
}
data "aws_iam_policy_document" "ssm_run_commands" {
count = length(keys(data.aws_iam_policy_document.ssm_run_command)) > 0 ? 1 : 0
source_policy_documents = values(data.aws_iam_policy_document.ssm_run_command)[*].json
}
data "aws_iam_policy_document" "ssm_run_command" {
for_each = {
for target in var.aws_service_targets :
target.id => target
if var.default_execution_role.enabled && target.type == "SSM_RUN_COMMAND"
}
statement {
effect = "Allow"
actions = ["ssm:SendCommand"]
resources = ["arn:${local.partition}:ssm:${local.region}:*:document/${regex("/([0-9A-Za-z_-]+)$", each.value.ssm_run_command.document)[0]}"]
}
dynamic "statement" {
for_each = {
for k, v in each.value.ssm_run_command.target_selector :
k => v
if k == "InstanceIds"
}
content {
effect = "Allow"
actions = ["ssm:SendCommand"]
resources = [
for instance_id in statement.value :
"arn:${local.partition}:ec2:${local.region}:${local.account_id}:instance/${instance_id}"
]
}
}
dynamic "statement" {
for_each = length([
for k in keys(each.value.ssm_run_command.target_selector) :
k
if startswith(k, "tag:")
]) > 0 ? ["go"] : []
content {
effect = "Allow"
actions = ["ssm:SendCommand"]
resources = ["arn:${local.partition}:ec2:${local.region}:${local.account_id}:instance/*"]
dynamic "condition" {
for_each = {
for k, v in each.value.ssm_run_command.target_selector :
k => v
if startswith(k, "tag:")
}
content {
variable = condition.key
test = "StringEquals"
values = condition.value
}
}
}
}
}