You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

是否有Terraform资源可用于上传AWS API Gateway密钥文件?

AWS API Gateway密钥文件上传的Terraform资源支持情况

Terraform的AWS Provider没有原生支持批量上传API密钥文件的资源,AWS API Gateway的批量导入API密钥功能主要通过AWS CLI或原生API调用实现。不过你可以通过以下两种方式在Terraform中完成这个操作:

  • 方式一:用null_resource配合local-exec调用AWS CLI
    这种方式适合批量导入密钥,需要提前准备好符合格式要求的密钥文件。示例代码如下:

    resource "null_resource" "import_api_keys" {
      triggers = {
        # 当密钥文件内容变化时,重新执行导入操作
        key_file_hash = filebase64sha256("${path.module}/api-keys.json")
      }
    
      provisioner "local-exec" {
        command = "aws apigateway import-api-keys --body file://${path.module}/api-keys.json --format json --fail-on-warnings"
      }
    }
    

    密钥文件需要包含apiKey、name等必填字段,格式需符合AWS的规范要求。

  • 方式二:用aws_api_gateway_api_key逐个创建密钥
    如果密钥数量不多,直接用Terraform的原生资源逐个创建更易维护,示例:

    resource "aws_api_gateway_api_key" "demo_key" {
      name        = "demo-api-key"
      enabled     = true
      description = "Demo API key for testing"
      # 不指定value时,Terraform会自动生成随机密钥
      # value = "your-custom-key-value"
    }
    

注意事项

  • 使用local-exec时,运行Terraform的环境必须配置好AWS凭证,且拥有apigateway:ImportApiKeys权限。
  • triggers中的哈希值会在密钥文件更新时触发重新执行,避免重复导入或遗漏更新。

内容的提问来源于stack exchange,提问作者Ajith Ahangama Vitharanalage

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.16 21:34:54