from constructs import Construct
from cdktf import TerraformStack
from imports.aws import ApiGatewayUsagePlan, ApiGatewayUsagePlanKey
class MyApiGatewayStack(TerraformStack):
def __init__(self, scope: Construct, id: str):
super().__init__(scope, id)
# 定义 API Keys 和 Usage Plan 配置
usage_plan_configs = {
"plan1": {
"key_id": "key-id-1", # 替换为实际 API Key ID
"rate_limit": 5,
"burst_limit": 10,
"quota_limit": 1000,
"quota_period": "MONTH"
},
"plan2": {
"key_id": "key-id-2", # 替换为实际 API Key ID
"rate_limit": 10,
"burst_limit": 20,
"quota_limit": 2000,
"quota_period": "MONTH"
}
}
# 用于存储创建的 Usage Plans
usage_plans = {}
# 循环创建 Usage Plans 和绑定 API Key
for plan_name, config in usage_plan_configs.items():
# 创建 Usage Plan
usage_plan = ApiGatewayUsagePlan(self, f"{plan_name}_usage_plan",
name=plan_name,
throttle={
"rate_limit": config["rate_limit"],
"burst_limit": config["burst_limit"]
},
quota={
"limit": config["quota_limit"],
"period": config["quota_period"]
}
)
usage_plans[plan_name] = usage_plan
# 创建 Usage Plan Key (绑定 API Key)
ApiGatewayUsagePlanKey(self, f"{plan_name}_usage_plan_key",
key_id=config["key_id"],
key_type="API_KEY",
usage_plan_id=usage_plan.id
)
# 输出 Usage Plans 的 ID(可选)
for plan_name, usage_plan in usage_plans.items():
print(f"Created Usage Plan: {plan_name} with ID: {usage_plan.id}")
Register as a new user and use Qiita more conveniently
- You get articles that match your needs
- You can efficiently read back useful information
- You can use dark theme