【问题标题】:terraform aws api gateway configure method throttling per each api keyterraform aws api gateway 为每个 api 密钥配置方法限制
【发布时间】:2020-05-24 08:12:01
【问题描述】:

我想配置如下使用计划、api 密钥和方法。

基本上一个aws api网关有10种方法,我想为每个资源配置不同的速率

usage plan     api key    Resource  Method  Rate (requests per second)
usage plan1    apiKey1    /a        POST    1 qps
usage plan1    apiKey1    /b        POST    2 qps
usage plan2    apiKey2    /a        POST    4 qps
usage plan2    apiKey2    /b        POST    6 qps

但是在aws_api_gateway_usage_plan我只能找到stage的使用计划设置。

我可以使用什么 terraform 资源来配置使用计划

我想实现以下功能配置方法限制

【问题讨论】:

    标签: terraform aws-api-gateway


    【解决方案1】:

    经过检查,我认为直到现在,terraform 都不支持此功能。

    但是,使用 aws cli 推荐有解决方法。

    参考这个链接:

    https://github.com/terraform-providers/terraform-provider-aws/issues/5901

    我引用了这里的工作

    variable "method_throttling" {
      type        = "list"
      description = "example method throttling"
      default     = [
        "\\\"/<RESOURCE1>/<METHOD1>\\\":{\\\"rateLimit\\\":400,\\\"burstLimit\\\":150}",
        "\\\"/<RESOURCE2>/<METHOD2>\\\":{\\\"rateLimit\\\":1000,\\\"burstLimit\\\":303}"
      ]
    }
    
    # locals
    locals {
      # Delimiter for later usage
      delimiter      = "'"
    
      # Base aws cli command
      base_command   = "aws apigateway update-usage-plan --usage-plan-id ${aws_api_gateway_usage_plan.usage_plan.id} --patch-operations op"
    
      # Later aws cli command
      base_path      = "path=/apiStages/${var.api_gateway_rest_api_id}:${var.api_gateway_stage_name}/throttle,value"
    
      # Join method throttling variable to string
      methods_string = "${local.delimiter}\"{${join(",", var.method_throttling)}}\"${local.delimiter}"
    }
    
    resource "null_resource" "method_throttling" {
      count = "${length(var.method_throttling) != 0 ? 1 : 0}"
    
      # create method throttling
      provisioner "local-exec" {
        when       = "create"
        command    = "${local.base_command}=add,${local.base_path}=${local.methods_string}"
        on_failure = "continue"
      }
    
      # edit method throttling
      provisioner "local-exec" {
        command = "${local.base_command}=replace,${local.base_path}=${local.methods_string}"
        on_failure = "fail"
      }  
    
      # delete method throttling
      provisioner "local-exec" {
        when    = "destroy"
        command = "${local.base_command}=remove,${local.base_path}="    
        on_failure = "fail"
      }
    
      triggers = {
        usage_plan_change  = "${aws_api_gateway_usage_plan.usage_plan.id}"
        methods_change     = "${local.methods_string}"
      }
    
      depends_on = [
        "aws_api_gateway_usage_plan.usage_plan"
      ]
    }
    

    【讨论】:

      猜你喜欢
      • 2019-01-07
      • 1970-01-01
      • 2019-09-30
      • 2020-09-25
      • 1970-01-01
      • 2022-01-25
      • 2017-05-01
      • 2018-02-01
      • 1970-01-01
      相关资源
      最近更新 更多