Skip to content

Commit 93ce2bf

Browse files
authored
feat: rate limiting configuration (#5)
1 parent 1ecc16c commit 93ce2bf

File tree

2 files changed

+98
-5
lines changed

2 files changed

+98
-5
lines changed

main.tf

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ locals {
77
resource "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-
6767
resource "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)
7881
resource "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)
8993
resource "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)
105127
resource "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
}

variables.tf

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,75 @@ variable "enable_metrics" {
2020
type = bool
2121
description = "Enable API Gateway metrics"
2222
default = true
23+
}
24+
25+
variable "stage_throttle_rate_limit" {
26+
type = number
27+
description = "API Gateway stage throttle rate limit (requests per second)"
28+
default = null
29+
}
30+
31+
variable "stage_throttle_burst_limit" {
32+
type = number
33+
description = "API Gateway stage throttle burst limit"
34+
default = null
35+
}
36+
37+
variable "throttle_rate_limit" {
38+
type = number
39+
description = "API Gateway usage plan throttle rate limit (requests per second)"
40+
default = null
41+
}
42+
43+
variable "throttle_burst_limit" {
44+
type = number
45+
description = "API Gateway usage plan throttle burst limit"
46+
default = null
47+
}
48+
49+
variable "quota_limit" {
50+
type = number
51+
description = "API Gateway usage plan quota limit (requests per period)"
52+
default = null
53+
}
54+
55+
variable "quota_period" {
56+
type = string
57+
description = "API Gateway usage plan quota period (DAY, WEEK, MONTH)"
58+
default = "DAY"
59+
validation {
60+
condition = contains(["DAY", "WEEK", "MONTH"], var.quota_period)
61+
error_message = "Quota period must be DAY, WEEK, or MONTH."
62+
}
63+
}
64+
65+
variable "create_usage_plan" {
66+
description = "Whether to create usage plan and API keys"
67+
type = bool
68+
default = true
69+
}
70+
71+
variable "api_key_required" {
72+
description = "Whether to require an API key for API Gateway methods"
73+
type = bool
74+
default = false
75+
}
76+
77+
variable "description" {
78+
type = string
79+
description = "Description for the API Gateway"
80+
default = null
81+
}
82+
83+
variable "cors_configuration" {
84+
type = object({
85+
allow_credentials = bool
86+
allow_headers = list(string)
87+
allow_methods = list(string)
88+
allow_origins = list(string)
89+
expose_headers = list(string)
90+
max_age = number
91+
})
92+
description = "CORS configuration for the API Gateway"
93+
default = null
2394
}

0 commit comments

Comments
 (0)