如何在带有Terraform的API网关上启用HEAD方法

时间:2018-03-01 19:14:06

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

我已尝试将以下内容添加到我目前正在使用的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 --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

修改 问题确实是没有重新部署部署。但是我在答案中提到了一种更好的方法。

1 个答案:

答案 0 :(得分:0)

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

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文件中的更改触发