77resource "aws_api_gateway_rest_api" "api" {
88 for_each = toset (local. stages )
99 name = " ${ module . this . id } -${ each . key } "
10+ description = var. description
1011
1112 endpoint_configuration {
1213 types = [
@@ -63,20 +64,22 @@ resource "aws_api_gateway_stage" "stage" {
6364 tags = module. this . tags
6465}
6566
66-
6767resource "aws_api_gateway_method_settings" "settings" {
6868 for_each = toset (local. stages )
6969 rest_api_id = aws_api_gateway_rest_api. api [each . key ]. id
7070 stage_name = aws_api_gateway_stage. stage [each . key ]. stage_name
7171 method_path = " */*"
7272
7373 settings {
74- metrics_enabled = local. enable_metrics
74+ metrics_enabled = local. enable_metrics
75+ throttling_rate_limit = var. stage_throttle_rate_limit
76+ throttling_burst_limit = var. stage_throttle_burst_limit
7577 }
7678}
7779
80+ # API Keys (conditional)
7881resource "aws_api_gateway_api_key" "default" {
79- for_each = toset (local. stages )
82+ for_each = var . create_usage_plan ? toset (local. stages ) : []
8083 name = join (" -" , [
8184 module . this . id ,
8285 " key" ,
@@ -86,7 +89,9 @@ resource "aws_api_gateway_api_key" "default" {
8689 tags = module. this . tags
8790}
8891
92+ # Usage Plan (conditional)
8993resource "aws_api_gateway_usage_plan" "default" {
94+ count = var. create_usage_plan ? 1 : 0
9095 name = " ${ module . this . id } -default-plan"
9196
9297 dynamic "api_stages" {
@@ -96,15 +101,32 @@ resource "aws_api_gateway_usage_plan" "default" {
96101 stage = api_stages. value
97102 }
98103 }
104+
105+ dynamic "throttle_settings" {
106+ for_each = var. throttle_rate_limit != null ? [1 ] : []
107+ content {
108+ rate_limit = var. throttle_rate_limit
109+ burst_limit = var. throttle_burst_limit
110+ }
111+ }
112+
113+ dynamic "quota_settings" {
114+ for_each = var. quota_limit != null ? [1 ] : []
115+ content {
116+ limit = var. quota_limit
117+ period = var. quota_period
118+ }
119+ }
99120
100121 depends_on = [aws_api_gateway_stage . stage ]
101122
102123 tags = module. this . tags
103124}
104125
126+ # Link API Keys to Usage Plan (conditional)
105127resource "aws_api_gateway_usage_plan_key" "default" {
106- for_each = toset (local. stages )
128+ for_each = var . create_usage_plan ? toset (local. stages ) : []
107129 key_id = aws_api_gateway_api_key. default [each . key ]. id
108130 key_type = " API_KEY"
109- usage_plan_id = aws_api_gateway_usage_plan. default . id
131+ usage_plan_id = aws_api_gateway_usage_plan. default [ 0 ] . id
110132}
0 commit comments