首页 > 解决方案 > 未为 aws_api_gateway_usage_plan 中的 api_stages 保留订单

问题描述

地形版本

v12.25

情况

我正在使用适用于多个阶段的 aws_api_gateway_usage_plan,由 api_stages 配置块定义。

Terraform 配置文件

模块 main.tf 文件

resource "aws_api_gateway_usage_plan" "usage_plan" {
  name         = var.usage_plan_name
  dynamic "api_stages" {
      for_each = var.api_stages
      content {
        api_id     = api_stages.value.api_id
        stage = api_stages.value.stage      
      }
    }
  throttle_settings {
    burst_limit = var.burst_limit
    rate_limit  = var.rate_limit
  }
  description = var.description
  tags = var.app_role_tags
}

模块 variable.tf 文件仅提供与此问题相关的变量

variable "api_stages" {
  type = set(object({
    api_id    = string
    stage = string
  }))
}

现在使用这个模块的资源

资源 main.tf 文件

module "SCCG_Usage_Plan" {
  source           = "/modulepath"
  usage_plan_name        = "usageplan name"
  burst_limit = 5
  rate_limit = 10
  api_stages = var.api_stages
  app_role_tags = var.app_role_tags
  description = "Desctiption"
}

Variable.tf 文件与模块一相同

我通过terraform.tfvars文件传递​​ api_id 和阶段

api_stages={
  {
      api_id= "9afgfdg"
      stage="sbx"
  },
  {
      api_id= "ukxydgdgd"
      stage="sbx"
  },
  {
      api_id= "8pa5gdgdg"
      stage="sbx"
  },
  {
      api_id= "zo0tkadad"
      stage="sbx"
  }  
}

现在,每当我有新的 API 时,我都会在此处添加 api_id 和阶段,这会将这些信息添加到使用计划中,效果很好。

当我尝试添加任何不存在的 api_id 或阶段时会出现问题。那时,我会给出错误

Error: error updating API Gateway Usage Plan: NotFoundException: API Stage not found: zo0tk1nhug:sandbox

  on .terraform/modules/main.tf line 1, in resource "aws_api_gateway_usage_plan" "usage_plan":
   1: resource "aws_api_gateway_usage_plan" "usage_plan" {

这很好,但是在删除有问题的 api_id/stages 之后,当我重新运行计划时,我看到其他 api_ids 已经添加并且运行良好从使用计划中删除,并且 terraform 正在尝试再次添加这些,

实际行为

 # module.Usage_Plan.aws_api_gateway_usage_plan.usage_plan will be updated in-place
  ~ resource "aws_api_gateway_usage_plan" "usage_plan" {
        arn         = "arn:aws:apigateway:us-east-1::/usageplans/xxxxx"
        description = "Desctiption
        id          = "xxxxx"
        name        = "usageplan"
        
      + api_stages {
          + api_id = "8pa5gdgdg"
          + stage  = "sandbox"
        }
        api_stages {
            api_id = "9afgfdg"
            stage  = "sandbox"
        }
      + api_stages {
          + api_id = "ukxydgdgd"
          + stage  = "sandbox"
        }
      
        throttle_settings {
            burst_limit = 5
            rate_limit  = 10
        }
    }

As you can see it's randomly deleting api_id and stages if error occurs and trying to add those back in next apply

预期行为

 # module.Usage_Plan.aws_api_gateway_usage_plan.usage_plan will be updated in-place
  ~ resource "aws_api_gateway_usage_plan" "usage_plan" {
        arn         = "arn:aws:apigateway:us-east-1::/usageplans/xxxxx"
        description = "Desctiption
        id          = "xxxxx"
        name        = "usageplan"
        
       api_stages {
           api_id = "8pa5gdgdg"
           stage  = "sandbox"
        }
        api_stages {
            api_id = "9afgfdg"
            stage  = "sandbox"
        }
       api_stages {
           api_id = "ukxydgdgd"
           stage  = "sandbox"
        }
            throttle_settings {
            burst_limit = 5
            rate_limit  = 10
        }
    }

Basically no changes as these  api_id and stages are already there

参考

https://github.com/hashicorp/terraform-provider-aws/issues/1675

标签: amazon-web-servicesterraformterraform-provider-aws

解决方案


推荐阅读