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

如何用Terraform创建含路径参数{date1}/{date2}的AWS API Gateway?

解决AWS API Gateway路径参数Terraform配置问题

问题原因

你当前的代码将路径参数部分的path_part设为普通字符串(如date1),而AWS API Gateway识别路径参数的要求是将path_part设置为{参数名}的格式。

修改后的代码

只需把date1和date2对应的资源path_part改成{date1}和{date2}即可,完整配置如下:

resource "aws_api_gateway_rest_api" "api" {
  name = "my_api_gateway"
}

resource "aws_api_gateway_resource" "api" {
  path_part   = "api"
  parent_id   = aws_api_gateway_rest_api.api.root_resource_id
  rest_api_id = aws_api_gateway_rest_api.api.id
}

resource "aws_api_gateway_resource" "from" {
  path_part   = "from"
  parent_id   = aws_api_gateway_rest_api.api.root_resource_id
  rest_api_id = aws_api_gateway_rest_api.api.id
}

resource "aws_api_gateway_resource" "date1" {
  path_part   = "{date1}"  # 修改为路径参数格式
  parent_id   = aws_api_gateway_resource.from.id
  rest_api_id = aws_api_gateway_rest_api.api.id
}

resource "aws_api_gateway_resource" "to" {
  path_part   = "to"
  parent_id   = aws_api_gateway_resource.date1.id
  rest_api_id = aws_api_gateway_rest_api.api.id
}

resource "aws_api_gateway_resource" "date2" {
  path_part   = "{date2}"  # 修改为路径参数格式
  parent_id   = aws_api_gateway_resource.to.id
  rest_api_id = aws_api_gateway_rest_api.api.id
}

resource "aws_api_gateway_method" "method" {
  rest_api_id   = aws_api_gateway_rest_api.api.id
  resource_id   = aws_api_gateway_resource.date2.id
  http_method   = "POST"
  authorization = "NONE"

  # 可选:强制要求请求携带路径参数,避免无效请求
  request_parameters = {
    "method.request.path.date1" = true
    "method.request.path.date2" = true
  }
}

resource "aws_api_gateway_deployment" "api" {
  rest_api_id = aws_api_gateway_rest_api.api.id

  triggers = {
    redeployment = sha1(jsonencode([
      aws_api_gateway_resource.api.id,
      aws_api_gateway_method.method.id,
      aws_api_gateway_integration.lambda.id,
    ]))
  }

  lifecycle {
    create_before_destroy = true
  }
}

resource "aws_api_gateway_stage" "api" {
  deployment_id = aws_api_gateway_deployment.api.id
  rest_api_id   = aws_api_gateway_rest_api.api.id
  stage_name    = "dev"
}

resource "aws_api_gateway_method_settings" "example" {
  rest_api_id = aws_api_gateway_rest_api.api.id
  stage_name  = aws_api_gateway_stage.api.stage_name
  method_path = "*/*"

  settings {
    metrics_enabled = true
    logging_level   = "INFO"
  }
}

resource "aws_api_gateway_integration_response" "response" {
  rest_api_id = aws_api_gateway_rest_api.api.id
  resource_id = aws_api_gateway_resource.date2.id
  http_method = aws_api_gateway_method.method.http_method
  status_code = aws_api_gateway_method_response.response.status_code

  depends_on = [
    aws_api_gateway_integration.lambda
  ]
}

resource "aws_api_gateway_method_response" "response" {
  rest_api_id = aws_api_gateway_rest_api.api.id
  resource_id = aws_api_gateway_resource.date2.id
  http_method = aws_api_gateway_method.method.http_method
  status_code = "200"

  response_models = {
    "application/json" = "Empty"
  }
}

# 注意:原代码引用了未定义的aws_api_gateway_integration.lambda,需补充Lambda集成配置,示例如下:
# resource "aws_api_gateway_integration" "lambda" {
#   rest_api_id = aws_api_gateway_rest_api.api.id
#   resource_id = aws_api_gateway_resource.date2.id
#   http_method = aws_api_gateway_method.method.http_method
#   type        = "AWS_PROXY"
#   uri         = aws_lambda_function.your_lambda.invoke_arn
# }

额外说明

  • 将path_part设为{参数名}是API Gateway识别路径参数的核心规则,Terraform会直接将该格式同步到控制台。
  • 添加request_parameters可强制校验请求中的路径参数,提升接口安全性。
  • 原代码缺失Lambda集成资源定义,需补充后才能完成完整的API部署。

内容的提问来源于stack exchange,提问作者Alderven

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.04 14:35:26