【问题标题】:How to enable HEAD method on API Gateway with Terraform如何使用 Terraform 在 API 网关上启用 HEAD 方法
【发布时间】:2018-08-09 23:00:18
【问题描述】:

我已尝试将以下内容添加到我当前工作的 apigateway API 设置中

resource "aws_api_gateway_method" "enable_head_request" {
  provider         = "aws.default"
  rest_api_id      = "${aws_api_gateway_rest_api.petshop.id}"
  resource_id      = "${aws_api_gateway_rest_api.petshop.root_resource_id}"
  http_method      = "HEAD"
  authorization    = "NONE"
#  api_key_required = "False"
}

resource "aws_api_gateway_integration" "enable_head_request" {
  provider                = "aws.default"
  rest_api_id             = "${aws_api_gateway_rest_api.petshop.id}"
  resource_id             = "${aws_api_gateway_rest_api.petshop.root_resource_id}"
  http_method             = "${aws_api_gateway_method.enable_head_request.http_method}"
  integration_http_method = "POST"
  type                    = "AWS_PROXY"
  uri                     = "${aws_lambda_function.petshop.invoke_arn}"
}

resource "aws_api_gateway_method_response" "200_for_head_request" {
  provider    = "aws.default"
  rest_api_id = "${aws_api_gateway_rest_api.petshop.id}"
  resource_id = "${aws_api_gateway_rest_api.petshop.root_resource_id}"
  http_method = "${aws_api_gateway_method.enable_head_request.http_method}"
  status_code = "200"
}

但是在部署并尝试 curl 我得到的端点之后;

curl --head https://test.com
HTTP/1.1 403 Forbidden
Date: Thu, 01 Mar 2018 18:47:07 GMT
Content-Type: application/json
Content-Length: 42
Connection: keep-alive
x-amzn-RequestId: f1811ce9-1d80-11e8-b15c-cf44af523470
x-amzn-ErrorType: MissingAuthenticationTokenException

编辑: 问题确实是部署没有重新部署。但正如我的回答中提到的那样,我找到了一种更好的方法。

【问题讨论】:

  • curl 命令应该类似于:curl --head "xxx: xxx" https://test.com
  • 取消授权后需要重新部署。最近有一个问题有完全相同的问题。 Terraform 在您申请时不会自动重新部署 API Gateway,因为它根本无法很好地处理 API Gateway 发布模型。
  • @ydaetskcoR 我已经能够毫无问题地将新的 GET 请求添加到不同的路径

标签: aws-api-gateway terraform terraform-provider-aws


【解决方案1】:

问题是部署没有被重新部署。这是比链接问题更好的方法

resource "aws_api_gateway_deployment" "petshop" {
  provider    = "aws.default"
  stage_description = "${md5(file("apigateway.tf"))}"
  rest_api_id = "${aws_api_gateway_rest_api.petshop.id}"
  stage_name  = "prod"
}

这可以节省您在每次微小更改时重新部署,并且只会由 apigateway.tf 文件中的更改触发

【讨论】:

    猜你喜欢
    • 2022-11-26
    • 2020-07-19
    • 2020-04-30
    • 2022-01-24
    • 2021-09-16
    • 2021-08-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多