Skip to content

Commit 09239c1

Browse files
committed
feat: add default authorizer configuration
1 parent ea12623 commit 09239c1

File tree

6 files changed

+33
-16
lines changed

6 files changed

+33
-16
lines changed

modules/config-lambda/events.tf

+3-3
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@ locals {
2525
])
2626
api_definition = {
2727
for http_path in flatten([for event in local.http_events : event.http.path]) : http_path => {
28-
for event in local.http_events : upper(event.http.method) => {
28+
for event in local.http_events : upper(event.http.method) => merge({
2929
function_name = event.function_name
30-
authorizer = try(event.http.authorizer, null)
31-
} if event.http.path == http_path
30+
}, try(event.http.authorizer, null) != null ? { authorizer = event.http.authorizer } : {})
31+
if event.http.path == http_path
3232
}
3333
}
3434
scheduled_events = merge({

terraform/bootstrap/variables.tf

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
variable "starchart" {
22
type = object({
33
aws_account_id = string
4+
aws_region = string
45
config = any
56
})
67
}

terraform/persistent/main.tf

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ variable "starchart" {
33

44
default_tags = map(string)
55
aws_account_id = string
6+
aws_region = string
67
bootstrap = object({
78
eventing_kms_key_arn = string
89
appconfig_application_id = string

terraform/runtime/api-http.tf

+15-4
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ variable "http_api" {
1818
enable_simple_responses = optional(bool)
1919
})), {})
2020

21+
default_authorizer = optional(string)
2122
})
2223
description = "The custom configuration for the API Gateway. Most of it will be inferred from Lambda events."
2324
default = null
@@ -27,8 +28,17 @@ locals {
2728
http_api_name = try(coalesce(var.http_api.name, local.config.project_name), null)
2829

2930
_http_api_definition_parsed = try(jsondecode(var.http_api.definition), {})
31+
32+
_http_api_lambda_api_definition = {
33+
for http_path, path_items in module.config_lambda.api_definition : http_path => {
34+
for http_method, path_item in path_items : http_method => merge(path_item, {
35+
authorizer = try(path_item.authorizer, var.http_api.default_authorizer, null)
36+
})
37+
}
38+
}
39+
3040
http_api_definition = merge(local._http_api_definition_parsed, {
31-
for http_path, path_items in module.config_lambda.api_definition : http_path => merge(try(local._http_api_definition_parsed[http_path], {}), {
41+
for http_path, path_items in local._http_api_lambda_api_definition : http_path => merge(try(local._http_api_definition_parsed[http_path], {}), {
3242
for http_method, path_item in path_items : http_method => {
3343

3444
lambda = {
@@ -38,14 +48,15 @@ locals {
3848
authorizer = try(path_item.authorizer, null) == null ? null : {
3949
name = try(var.http_api.authorizers[path_item.authorizer].name, path_item.authorizer)
4050
lambda = {
41-
function_name = module.config_lambda.functions[var.http_api.authorizers[path_item.authorizer].function_id].function_name
51+
function_name = module.config_lambda.lambda_definitions[var.http_api.authorizers[path_item.authorizer].function_id].function_name
4252
}
4353
header = try(var.http_api.authorizers[path_item.authorizer].header, null)
4454
authorizerType = try(var.http_api.authorizers[path_item.authorizer].type, null)
4555
identitySource = try(join(",", var.http_api.authorizers[path_item.authorizer].identity_source), null)
4656
resultTtlInSeconds = try(var.http_api.authorizers[path_item.authorizer].ttl_in_seconds, null)
4757
}
4858

59+
4960
}
5061
})
5162
})
@@ -57,8 +68,8 @@ module "http_api" {
5768

5869
name = local.http_api_name
5970

60-
region = data.aws_region.current.name
61-
account_id = data.aws_caller_identity.current.account_id
71+
region = var.starchart.aws_region
72+
account_id = var.starchart.aws_account_id
6273
definition = local.http_api_definition
6374

6475
disable_execute_api_endpoint = var.http_api.disable_execute_api_endpoint

terraform/runtime/api-rest.tf

+12-9
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,16 @@ locals {
2727
rest_api_name = try(coalesce(var.rest_api.name, local.config.project_name), null)
2828

2929
_rest_api_definition_parsed = try(jsondecode(var.rest_api.definition), {})
30+
31+
_rest_api_lambda_api_definition = {
32+
for http_path, path_items in module.config_lambda.api_definition : http_path => {
33+
for http_method, path_item in path_items : http_method => merge(path_item, {
34+
authorizer = try(path_item.authorizer, var.rest_api.default_authorizer, null)
35+
})
36+
}
37+
}
3038
rest_api_definition = merge(local._rest_api_definition_parsed, {
31-
for http_path, path_items in module.config_lambda.api_definition : http_path => merge(try(local._rest_api_definition_parsed[http_path], {}), {
39+
for http_path, path_items in local._rest_api_lambda_api_definition : http_path => merge(try(local._rest_api_definition_parsed[http_path], {}), {
3240
for http_method, path_item in path_items : http_method => {
3341

3442
lambda = {
@@ -38,7 +46,7 @@ locals {
3846
authorizer = try(path_item.authorizer, null) == null ? null : {
3947
name = try(var.rest_api.authorizers[path_item.authorizer].name, path_item.authorizer)
4048
lambda = {
41-
function_name = module.config_lambda.functions[var.rest_api.authorizers[path_item.authorizer].function_id].function_name
49+
function_name = module.config_lambda.lambda_definitions[var.rest_api.authorizers[path_item.authorizer].function_id].function_name
4250
}
4351
header = try(var.rest_api.authorizers[path_item.authorizer].header, null)
4452
authorizerType = try(var.rest_api.authorizers[path_item.authorizer].type, null)
@@ -51,19 +59,14 @@ locals {
5159
})
5260
}
5361

54-
55-
56-
data "aws_caller_identity" "current" {}
57-
data "aws_region" "current" {}
58-
5962
module "rest_api" {
6063
count = length(keys(local.rest_api_definition)) > 0 && var.rest_api != null ? 1 : 0
6164
source = "git@github.com:skyleague/aws-rest-api.git?ref=v3.1.0"
6265

6366
name = local.rest_api_name
6467

65-
region = data.aws_region.current.name
66-
account_id = data.aws_caller_identity.current.account_id
68+
region = var.starchart.aws_region
69+
account_id = var.starchart.aws_account_id
6770
definition = local.rest_api_definition
6871

6972
disable_execute_api_endpoint = var.rest_api.disable_execute_api_endpoint

terraform/runtime/main.tf

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ variable "starchart" {
33

44
default_tags = map(string)
55
aws_account_id = string
6+
aws_region = string
67
bootstrap = object({
78
eventing_kms_key_arn = string
89
artifacts_bucket_id = string

0 commit comments

Comments
 (0)