Skip to content

Commit 9601dc4

Browse files
authored
refactor: improve resource naming (#1)
1 parent 09239c1 commit 9601dc4

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+926
-416
lines changed

modules/_common/group.tf

+16-14
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
resource "aws_resourcegroups_group" "stack" {
2-
count = var.default_tags["StackType"] != null ? 1 : 0
3-
name = "${var.config.stack}-${lower(var.default_tags["StackType"])}"
2+
name = try("${var.config.stack}-${lower(var.default_tags["StackType"])}", var.config.stack)
43
tags = {
54
Name = var.config.project_name
65
Stack = ""
@@ -9,20 +8,23 @@ resource "aws_resourcegroups_group" "stack" {
98
resource_query {
109
query = jsonencode({
1110
ResourceTypeFilters = ["AWS::AllSupported"],
12-
TagFilters = [
13-
{
14-
Key = "Name"
15-
Values = [var.config.project_name]
16-
},
17-
{
18-
Key = "Stack"
19-
Values = [var.config.stack]
20-
},
21-
{
11+
TagFilters = concat(
12+
[
13+
{
14+
Key = "Name"
15+
Values = [var.config.project_name]
16+
},
17+
{
18+
Key = "Stack"
19+
Values = [var.config.stack]
20+
},
21+
],
22+
try([{
2223
Key = "StackType"
2324
Values = [var.default_tags["StackType"]]
24-
}
25-
]
25+
}
26+
], [])
27+
)
2628
})
2729
}
2830
}

modules/_config/output.tf

-8
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,3 @@ output "domain" {
2121
output "repo_root" {
2222
value = var.repo_root
2323
}
24-
25-
output "stack_prefix" {
26-
value = var.stack
27-
}
28-
29-
output "resource_prefix" {
30-
value = "${var.environment}-${var.project_identifier}-${var.stack}"
31-
}

modules/aws-http-api/spec.tf

+5-4
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,15 @@ locals {
1515
[path_item.authorizer.name],
1616
[{
1717
type = "apiKey"
18-
"x-amazon-apigateway-authtype" : "custom"
1918
in = "header"
2019
name = coalesce(try(path_item.authorizer.header, null), "Authorization")
2120
"x-amazon-apigateway-authorizer" = merge(
2221
{
23-
type = coalesce(try(path_item.authorizer.authorizerType, null), "request")
24-
identitySource = coalesce(try(path_item.authorizer.identitySource, null), "method.request.header.${coalesce(try(path_item.authorizer.header, null), "Authorization")}")
25-
authorizerResultTtlInSeconds = coalesce(try(path_item.authorizer.resultTtlInSeconds, null), 0)
22+
type = coalesce(try(path_item.authorizer.authorizerType, null), "request")
23+
identitySource = coalesce(try(path_item.authorizer.identitySource, null), "method.request.header.${coalesce(try(path_item.authorizer.header, null), "Authorization")}")
24+
authorizerResultTtlInSeconds = coalesce(try(path_item.authorizer.resultTtlInSeconds, null), 0)
25+
enableSimpleResponses = coalesce(try(path_item.authorizer.enableSimpleResponses, null), false)
26+
authorizerPayloadFormatVersion = coalesce(try(path_item.authorizer.authorizerPayloadFormatVersion, null), "2.0")
2627
},
2728
try({ authorizerUri = local.invoke_arns[path_item.authorizer.lambda.function_name] }, {}),
2829
try(jsondecode(path_item.authorizer["x-amazon-apigateway-authorizer"]), {})

modules/aws-http-api/variables.tf

+8-6
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,14 @@ variable "definition" {
3131
lambda = optional(object({
3232
function_name = string
3333
}))
34-
name = string
35-
authorizerType = optional(string)
36-
identitySource = optional(string)
37-
header = optional(string)
38-
resultTtlInSeconds = optional(number)
39-
security = optional(list(any))
34+
name = string
35+
authorizerType = optional(string)
36+
identitySource = optional(string)
37+
header = optional(string)
38+
resultTtlInSeconds = optional(number)
39+
security = optional(list(any))
40+
authorizerPayloadFormatVersion = optional(string)
41+
enableSimpleResponses = optional(bool)
4042
}))
4143
})))
4244
}

modules/config-appsync/variables.tf

+4-4
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@ variable "template_variables" {
2828

2929
variable "resources" {
3030
type = object({
31-
secrets = optional(map(object({ arn = string })), {})
32-
ssm_parameters = optional(map(object({ arn = string })), {})
33-
s3 = optional(map(object({ arn = string, id = string })), {})
34-
dynamodb = optional(map(object({ arn = string, id = string })), {})
31+
secret = optional(map(object({ arn = string })), {})
32+
ssm_parameter = optional(map(object({ arn = string })), {})
33+
s3 = optional(map(object({ arn = string, id = string })), {})
34+
dynamodb = optional(map(object({ arn = string, id = string })), {})
3535
})
3636
default = {}
3737

modules/config-lambda/lambda.tf

+8-5
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ locals {
1717
for resource in try(local.publishes[function_id].eventbridge, [])
1818
: var.resources.eventbridge[resource.eventBusId].env
1919
]...),
20-
{
20+
merge([
2121
for event in try(local.publishes[function_id].sqs, [])
22-
: "STARCHART_SQS_${upper(replace(event.queueId, "/[^a-zA-Z0-9]+/", "_"))}_QUEUE_URL" => var.sqs[event.queueId].queue.url
23-
},
22+
: var.resources.sqs_queue[event.queueId].env
23+
]...),
2424
merge([
2525
for resource in try(local.function_resources[function_id].s3, [])
2626
: var.resources.s3[resource.bucketId].env
@@ -43,10 +43,10 @@ locals {
4343
length(try(local.function_resources[function_id].s3, [])) == 0 ? {} : {
4444
starchart_s3_access = data.aws_iam_policy_document.s3_access[function_id]
4545
},
46-
length(try(local.function_resources[function_id].ssm_parameters, [])) == 0 ? {} : {
46+
length(try(local.function_resources[function_id].ssm_parameter, [])) == 0 ? {} : {
4747
starchart_ssm_access = data.aws_iam_policy_document.ssm_parameters_access[function_id]
4848
},
49-
length(try(local.function_resources[function_id].secrets, [])) == 0 ? {} : {
49+
length(try(local.function_resources[function_id].secret, [])) == 0 ? {} : {
5050
starchart_secrets_access = data.aws_iam_policy_document.secrets_access[function_id]
5151
},
5252
length(try(local.function_resources[function_id].dynamodb, [])) == 0 ? {} : {
@@ -55,6 +55,9 @@ locals {
5555
length(try(local.function_resources[function_id].appconfig, [])) == 0 ? {} : {
5656
appconfig_configuration_profile_access = data.aws_iam_policy_document.appconfig_configuration_profile_access[function_id]
5757
},
58+
length(try(local.function_resources[function_id].custom, [])) == 0 ? {} : {
59+
starchart_custom_access = data.aws_iam_policy_document.custom_access[function_id]
60+
},
5861
)
5962
}
6063
}

modules/config-lambda/publishes.tf

+2-2
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,12 @@ data "aws_iam_policy_document" "sqs_publish" {
5050
]
5151

5252
resources = [
53-
for queue in each.value : var.sqs[queue.queueId].queue.arn
53+
for queue in each.value : var.resources.sqs_queue[queue.queueId].arn
5454
]
5555
}
5656

5757
dynamic "statement" {
58-
for_each = toset(flatten([for queue in each.value : var.sqs[queue.queueId].queue.kms_master_key_id if var.sqs[queue.queueId].queue.kms_master_key_id != "alias/aws/sqs"]))
58+
for_each = toset(flatten([for queue in each.value : var.resources.sqs_queue[queue.queueId].kms_master_key_id if var.resources.sqs_queue[queue.queueId].kms_master_key_id != "alias/aws/sqs"]))
5959

6060
content {
6161
effect = "Allow"

modules/config-lambda/resources.tf

+60-22
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
variable "resources" {
22
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) })), {})
810

911
appconfig = optional(object({
1012
configuration_profiles = map(object({ configuration_profile_id = string })),
@@ -36,14 +38,14 @@ variable "appconfig_application_arn" {
3638
locals {
3739
function_resources = {
3840
for function_id, definition in local.handlers : function_id => {
39-
secrets = [
41+
secret = [
4042
for resource in try(definition.resources, []) : {
4143
path = try(resource.secret.path, resource.secret)
4244
actions = try(resource.secret.actions, ["read"])
4345
actions_string = join(",", sort(toset(try(resource.secret.actions, ["read"]))))
4446
} if try(resource.secret, null) != null
4547
]
46-
ssm_parameters = [
48+
ssm_parameter = [
4749
for resource in try(definition.resources, []) : {
4850
path = try(resource.parameter.path, resource.parameter)
4951
actions = try(resource.parameter.actions, ["read"])
@@ -61,9 +63,17 @@ locals {
6163
for resource in try(definition.resources, []) : {
6264
tableId = resource.dynamodb.tableId
6365
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, [])]))))
6568
} if try(resource.dynamodb, null) != null
6669
]
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+
]
6777
appconfig = concat(try(var.resources.appconfig.configuration_profiles.default, null) != null ? [{ configuration_profile_id = "default" }] : [], [
6878
# TODO: Add support for multiple configuration profiles
6979
])
@@ -72,7 +82,7 @@ locals {
7282
}
7383

7484
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 }
7686

7787
dynamic "statement" {
7888
for_each = merge([
@@ -100,14 +110,14 @@ data "aws_iam_policy_document" "secrets_access" {
100110
))
101111

102112
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
104114
]
105115
}
106116
}
107117
}
108118

109119
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 }
111121

112122
dynamic "statement" {
113123
for_each = merge([
@@ -129,7 +139,7 @@ data "aws_iam_policy_document" "ssm_parameters_access" {
129139
))
130140

131141
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
133143
]
134144
}
135145
}
@@ -181,37 +191,41 @@ data "aws_iam_policy_document" "dynamodb_access" {
181191
for_each = merge([
182192
for table in each.value : zipmap(
183193
[table.actions_string],
184-
[table.actions]
194+
[{ actions = table.actions, iamActions = table.iamActions }]
185195
)
186196
]...)
187197

188198
content {
189199
effect = "Allow"
190200

191201
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)]) ? [
193203
"dynamodb:GetItem",
194204
] : [],
195-
anytrue([for action in ["read", "query"] : contains(statement.value, action)]) ? [
205+
anytrue([for action in ["read", "query"] : contains(statement.value.actions, action)]) ? [
196206
"dynamodb:Query",
197207
] : [],
198-
anytrue([for action in ["write", "put"] : contains(statement.value, action)]) ? [
208+
anytrue([for action in ["write", "put"] : contains(statement.value.actions, action)]) ? [
199209
"dynamodb:PutItem",
200210
] : [],
201-
anytrue([for action in ["write", "update"] : contains(statement.value, action)]) ? [
211+
anytrue([for action in ["write", "update"] : contains(statement.value.actions, action)]) ? [
202212
"dynamodb:UpdateItem",
203213
] : [],
204-
contains(statement.value, "delete") ? [
214+
contains(statement.value.actions, "delete") ? [
205215
"dynamodb:DeleteItem",
206216
] : [],
207-
contains(statement.value, "scan") ? [
217+
contains(statement.value.actions, "scan") ? [
208218
"dynamodb:Scan",
209219
] : [],
220+
statement.value.iamActions,
210221
))
211222

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+
])
215229
}
216230
}
217231
}
@@ -233,6 +247,30 @@ data "aws_iam_policy_document" "appconfig_configuration_profile_access" {
233247
}
234248
}
235249

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+
236274
output "resources_env" {
237275
value = local.resources_env
238276
}

modules/config-lambda/variables.tf

-9
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,3 @@ variable "inline_policies" {
2626
description = "The inline policies to be attached to the functions."
2727
default = {}
2828
}
29-
30-
variable "sqs" {
31-
type = map(object({
32-
queue = object({ arn = string, url = string, kms_master_key_id = string })
33-
dlq = object({ arn = string, url = string, kms_master_key_id = string })
34-
}))
35-
description = "The SQS queues that can possibly be used by the functions."
36-
default = {}
37-
}

0 commit comments

Comments
 (0)