首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >terraform aws api网关配置方法按每个api键进行节流

terraform aws api网关配置方法按每个api键进行节流
EN

Stack Overflow用户
提问于 2020-02-08 11:11:34
回答 1查看 1.4K关注 0票数 2

我希望像下面这样配置使用计划、api密钥和方法。

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

代码语言:javascript
复制
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中,我只能找到舞台的使用计划设置。

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

我想实现以下功能配置方法节流

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-02-11 02:14:35

在检查之后,我认为到目前为止,terraform不支持这个特性。

但是,使用aws推荐是有解决办法的。

请参阅此链接:

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

我引用了这里的作品

代码语言:javascript
复制
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"
  ]
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60126179

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档