1
1
variable "resources" {
2
2
type = object ({
3
- secrets = optional (map (object ({ arn = string })), {})
4
- ssm_parameters = optional (map (object ({ arn = string })), {})
5
- s3 = optional (map (object ({ arn = string , id = string , env = map (string ) })), {})
6
- dynamodb = optional (map (object ({ arn = string , id = string , env = map (string ) })), {})
7
- eventbridge = optional (map (object ({ arn = string , id = string , env = map (string ) })), {})
3
+ secret = optional (map (object ({ arn = string })), {})
4
+ ssm_parameter = optional (map (object ({ arn = string })), {})
5
+ s3 = optional (map (object ({ arn = string , id = string , env = map (string ) })), {})
6
+ dynamodb = optional (map (object ({ arn = string , id = string , env = map (string ) })), {})
7
+ eventbridge = optional (map (object ({ arn = string , id = string , env = map (string ) })), {})
8
+ sqs_queue = optional (map (object ({ name = optional (string ), name_prefix = optional (string ), arn = string , url = string , kms_master_key_id = string , visibility_timeout_seconds = number , env = map (string ) })), {})
9
+ sqs_dlq = optional (map (object ({ name = optional (string ), name_prefix = optional (string ), arn = string , url = string , kms_master_key_id = string , visibility_timeout_seconds = number , env = map (string ) })), {})
8
10
9
11
appconfig = optional (object ({
10
12
configuration_profiles = map (object ({ configuration_profile_id = string })),
@@ -36,14 +38,14 @@ variable "appconfig_application_arn" {
36
38
locals {
37
39
function_resources = {
38
40
for function_id , definition in local . handlers : function_id => {
39
- secrets = [
41
+ secret = [
40
42
for resource in try (definition. resources , []) : {
41
43
path = try (resource. secret . path , resource. secret )
42
44
actions = try (resource. secret . actions , [" read" ])
43
45
actions_string = join (" ," , sort (toset (try (resource. secret . actions , [" read" ]))))
44
46
} if try (resource. secret , null ) != null
45
47
]
46
- ssm_parameters = [
48
+ ssm_parameter = [
47
49
for resource in try (definition. resources , []) : {
48
50
path = try (resource. parameter . path , resource. parameter )
49
51
actions = try (resource. parameter . actions , [" read" ])
@@ -61,9 +63,17 @@ locals {
61
63
for resource in try (definition. resources , []) : {
62
64
tableId = resource . dynamodb . tableId
63
65
actions = resource . dynamodb . actions
64
- actions_string = join (" ," , sort (toset (resource. dynamodb . actions )))
66
+ iamActions = try (flatten ([resource . dynamodb . iamActions ]), [])
67
+ actions_string = join (" ," , sort (toset (flatten ([resource . dynamodb . actions , try (resource. dynamodb . iamActions , [])]))))
65
68
} if try (resource. dynamodb , null ) != null
66
69
]
70
+ custom = [
71
+ for resource in try (definition. resources , []) : {
72
+ arn = resource . custom . arn
73
+ iam_actions = flatten ([resource . custom . iamActions ])
74
+ iam_actions_string = join (" ," , sort (toset (flatten ([resource . custom . iamActions ]))))
75
+ } if try (resource. custom , null ) != null
76
+ ]
67
77
appconfig = concat (try (var. resources . appconfig . configuration_profiles . default , null ) != null ? [{ configuration_profile_id = " default" }] : [], [
68
78
# TODO: Add support for multiple configuration profiles
69
79
])
@@ -72,7 +82,7 @@ locals {
72
82
}
73
83
74
84
data "aws_iam_policy_document" "secrets_access" {
75
- for_each = { for function_id , definition in local . function_resources : function_id => definition . secrets if length (try (definition. secrets , [])) > 0 }
85
+ for_each = { for function_id , definition in local . function_resources : function_id => definition . secret if length (try (definition. secret , [])) > 0 }
76
86
77
87
dynamic "statement" {
78
88
for_each = merge ([
@@ -100,14 +110,14 @@ data "aws_iam_policy_document" "secrets_access" {
100
110
))
101
111
102
112
resources = [
103
- for secret in each . value : var . resources . secrets [secret . path ]. arn if secret . actions_string == statement . key
113
+ for secret in each . value : var . resources . secret [secret . path ]. arn if secret . actions_string == statement . key
104
114
]
105
115
}
106
116
}
107
117
}
108
118
109
119
data "aws_iam_policy_document" "ssm_parameters_access" {
110
- for_each = { for function_id , definition in local . function_resources : function_id => definition . ssm_parameters if length (try (definition. ssm_parameters , [])) > 0 }
120
+ for_each = { for function_id , definition in local . function_resources : function_id => definition . ssm_parameter if length (try (definition. ssm_parameter , [])) > 0 }
111
121
112
122
dynamic "statement" {
113
123
for_each = merge ([
@@ -129,7 +139,7 @@ data "aws_iam_policy_document" "ssm_parameters_access" {
129
139
))
130
140
131
141
resources = [
132
- for parameter in each . value : var . resources . ssm_parameters [parameter . path ]. arn if parameter . actions_string == statement . key
142
+ for parameter in each . value : var . resources . ssm_parameter [parameter . path ]. arn if parameter . actions_string == statement . key
133
143
]
134
144
}
135
145
}
@@ -181,37 +191,41 @@ data "aws_iam_policy_document" "dynamodb_access" {
181
191
for_each = merge ([
182
192
for table in each . value : zipmap (
183
193
[table . actions_string ],
184
- [table . actions ]
194
+ [{ actions = table.actions, iamActions = table.iamActions } ]
185
195
)
186
196
]... )
187
197
188
198
content {
189
199
effect = " Allow"
190
200
191
201
actions = toset (concat (
192
- anytrue ([for action in [" read" , " get" ] : contains (statement. value , action)]) ? [
202
+ anytrue ([for action in [" read" , " get" ] : contains (statement. value . actions , action)]) ? [
193
203
" dynamodb:GetItem" ,
194
204
] : [],
195
- anytrue ([for action in [" read" , " query" ] : contains (statement. value , action)]) ? [
205
+ anytrue ([for action in [" read" , " query" ] : contains (statement. value . actions , action)]) ? [
196
206
" dynamodb:Query" ,
197
207
] : [],
198
- anytrue ([for action in [" write" , " put" ] : contains (statement. value , action)]) ? [
208
+ anytrue ([for action in [" write" , " put" ] : contains (statement. value . actions , action)]) ? [
199
209
" dynamodb:PutItem" ,
200
210
] : [],
201
- anytrue ([for action in [" write" , " update" ] : contains (statement. value , action)]) ? [
211
+ anytrue ([for action in [" write" , " update" ] : contains (statement. value . actions , action)]) ? [
202
212
" dynamodb:UpdateItem" ,
203
213
] : [],
204
- contains (statement. value , " delete" ) ? [
214
+ contains (statement. value . actions , " delete" ) ? [
205
215
" dynamodb:DeleteItem" ,
206
216
] : [],
207
- contains (statement. value , " scan" ) ? [
217
+ contains (statement. value . actions , " scan" ) ? [
208
218
" dynamodb:Scan" ,
209
219
] : [],
220
+ statement. value . iamActions ,
210
221
))
211
222
212
- resources = [
213
- for table in each . value : var . resources . dynamodb [table . tableId ]. arn if table . actions_string == statement . key
214
- ]
223
+ resources = flatten ([
224
+ for arn in [for table in each . value : var . resources . dynamodb [table . tableId ]. arn if table . actions_string == statement . key ] : [
225
+ arn ,
226
+ " ${ arn } /index/*" ,
227
+ ]
228
+ ])
215
229
}
216
230
}
217
231
}
@@ -233,6 +247,30 @@ data "aws_iam_policy_document" "appconfig_configuration_profile_access" {
233
247
}
234
248
}
235
249
250
+ data "aws_iam_policy_document" "custom_access" {
251
+ for_each = { for function_id , definition in local . function_resources : function_id => definition . custom if length (try (definition. custom , [])) > 0 }
252
+
253
+ dynamic "statement" {
254
+ for_each = merge ([
255
+ for custom in each . value : zipmap (
256
+ [custom . iam_actions_string ],
257
+ [custom . iam_actions ]
258
+ )
259
+ ]... )
260
+
261
+ content {
262
+ effect = " Allow"
263
+
264
+ actions = statement. value
265
+
266
+ resources = flatten ([
267
+ for custom in each . value : custom . arn if custom . iam_actions_string == statement . key
268
+ ])
269
+ }
270
+
271
+ }
272
+ }
273
+
236
274
output "resources_env" {
237
275
value = local. resources_env
238
276
}
0 commit comments