@@ -52,7 +52,7 @@ resource "aws_acm_certificate_validation" "main" {
5252resource "aws_cloudwatch_log_group" "main" {
5353 count = var. log_config != null ? 1 : 0
5454 name = " ${ var . identifier } -api-gw"
55- retention_in_days = try ( var. log_config [" retention_in_days" ], null )
55+ retention_in_days = var. log_config [" retention_in_days" ]
5656
5757 tags = var. tags
5858}
@@ -81,9 +81,9 @@ resource "aws_apigatewayv2_api" "main" {
8181 dynamic "cors_configuration" {
8282 for_each = var. cors_config != null ? [1 ] : []
8383 content {
84- allow_methods = try ( var. cors_config [" allow_methods" ], null )
85- allow_origins = try ( var. cors_config [" allow_origins" ], null )
86- allow_headers = try ( var. cors_config [" allow_headers" ], null )
84+ allow_methods = var. cors_config [" allow_methods" ]
85+ allow_origins = var. cors_config [" allow_origins" ]
86+ allow_headers = var. cors_config [" allow_headers" ]
8787 }
8888 }
8989
@@ -93,25 +93,33 @@ resource "aws_apigatewayv2_api" "main" {
9393resource "aws_apigatewayv2_integration" "main" {
9494 count = length (var. routes )
9595 api_id = aws_apigatewayv2_api. main . id
96- integration_uri = try ( var. routes [count . index ][" invoke_arn" ], null )
96+ integration_uri = var. routes [count . index ][" invoke_arn" ]
9797 integration_type = " AWS_PROXY"
9898 integration_method = " POST"
9999}
100100
101101resource "aws_apigatewayv2_route" "main" {
102102 count = length (var. routes )
103103 api_id = aws_apigatewayv2_api. main . id
104- route_key = " ${ try ( var. routes [count . index ][" method" ], null ) } ${ try ( var. routes [count . index ][" route" ], null ) } "
104+ route_key = " ${ var . routes [count . index ][" method" ]} ${ var . routes [count . index ][" route" ]} "
105105 target = " integrations/${ aws_apigatewayv2_integration . main [count . index ]. id } "
106106}
107107
108108resource "aws_lambda_permission" "main" {
109109 count = length (var. routes )
110110 statement_id = " AllowExecutionFromAPIGateway"
111111 action = " lambda:InvokeFunction"
112- function_name = try ( var. routes [count . index ][" function_arn" ], null )
112+ function_name = var. routes [count . index ][" function_arn" ]
113113 principal = " apigateway.amazonaws.com"
114- source_arn = " ${ aws_apigatewayv2_api . main . execution_arn } /*/*${ try (var. routes [count . index ][" route" ], null )} "
114+ source_arn = " ${ aws_apigatewayv2_api . main . execution_arn } /*/*${ var . routes [count . index ][" route" ]} "
115+ }
116+
117+ locals {
118+ rate_limited_routes = [for v in var . routes : {
119+ route_key = " ${ v [" method" ]} ${ v [" route" ]} "
120+ burst_limit = v[" burst_limit" ]
121+ rate_limit = v[" rate_limit" ]
122+ } if v [" burst_limit" ] != null && v [" rate_limit" ] != null ]
115123}
116124
117125resource "aws_apigatewayv2_stage" "main" {
@@ -120,12 +128,12 @@ resource "aws_apigatewayv2_stage" "main" {
120128 auto_deploy = true
121129
122130 dynamic "route_settings" {
123- for_each = var . routes
131+ for_each = local . rate_limited_routes
124132
125133 content {
126- route_key = " ${ try ( route_settings. value [" method " ], null ) } ${ try (route_settings . value [ " route " ], null ) } "
127- throttling_burst_limit = try ( route_settings. value [" burst_limit" ], null )
128- throttling_rate_limit = try ( route_settings. value [" rate_limit" ], null )
134+ route_key = route_settings. value [" route_key " ]
135+ throttling_burst_limit = route_settings. value [" burst_limit" ]
136+ throttling_rate_limit = route_settings. value [" rate_limit" ]
129137 }
130138 }
131139
0 commit comments