AWS API Gateway and static HTML: "Execution failed due to configuration error: statusCode should be an integer which defined in request template"

AWS API Gateway and static HTML: "Execution failed due to configuration error: statusCode should be an integer which defined in request template"

我正在尝试使用 AWS API 网关提供静态内容。 当我尝试从测试页和 curl 调用 URL 时,我收到错误:

"Execution failed due to configuration error: statusCode should be an integer which defined in request template".

这是我在 Terraform 上的配置:

resource "aws_api_gateway_rest_api" "raspberry_api" {
  name        = "raspberry_api"
}

resource "aws_acm_certificate" "raspberry_alexa_mirko_io" {
  domain_name       = "raspberry.alexa.mirko.io"
  validation_method = "DNS"

  lifecycle {
    create_before_destroy = true
  }
}

resource "aws_route53_record" "raspberry_alexa_mirko_io_cert_validation" {
  name    = aws_acm_certificate.raspberry_alexa_mirko_io.domain_validation_options.0.resource_record_name
  type    = aws_acm_certificate.raspberry_alexa_mirko_io.domain_validation_options.0.resource_record_type
  zone_id = var.route53_zone_id
  records = [aws_acm_certificate.raspberry_alexa_mirko_io.domain_validation_options.0.resource_record_value]
  ttl     = 60
}

resource "aws_route53_record" "raspberry_alexa_mirko_io" {
  zone_id = var.route53_zone_id
  name = aws_acm_certificate.raspberry_alexa_mirko_io.domain_name
  type = "A"
  alias {
    name = aws_api_gateway_domain_name.raspberry_alexa_mirko_io.cloudfront_domain_name
    zone_id = aws_api_gateway_domain_name.raspberry_alexa_mirko_io.cloudfront_zone_id
    evaluate_target_health = true
  }
}

resource "aws_acm_certificate_validation" "raspberry_alexa_mirko_io" {
  certificate_arn         = aws_acm_certificate.raspberry_alexa_mirko_io.arn
  validation_record_fqdns = [aws_route53_record.raspberry_alexa_mirko_io_cert_validation.fqdn]
  provider = aws.useast1
}

resource "aws_api_gateway_domain_name" "raspberry_alexa_mirko_io" {
  certificate_arn = aws_acm_certificate_validation.raspberry_alexa_mirko_io.certificate_arn
  domain_name     = aws_acm_certificate.raspberry_alexa_mirko_io.domain_name
}

resource "aws_api_gateway_base_path_mapping" "raspberry_alexa_mirko_io_base_path_mapping" {
  api_id      = aws_api_gateway_rest_api.raspberry_api.id
  domain_name = aws_api_gateway_domain_name.raspberry_alexa_mirko_io.domain_name
}

resource "aws_api_gateway_resource" "home" {
  rest_api_id = aws_api_gateway_rest_api.raspberry_api.id
  parent_id   = aws_api_gateway_rest_api.raspberry_api.root_resource_id
  path_part   = "login"
}

resource "aws_api_gateway_method" "login" {
  rest_api_id   = aws_api_gateway_rest_api.raspberry_api.id
  resource_id   = aws_api_gateway_resource.home.id
  http_method   = "GET"
  authorization = "NONE"
}

resource "aws_api_gateway_integration" "integration" {
  rest_api_id             = aws_api_gateway_rest_api.raspberry_api.id
  resource_id             = aws_api_gateway_resource.subscribe_raspberry.id
  http_method             = aws_api_gateway_method.subscribe.http_method
  integration_http_method = "POST"
  type                    = "AWS_PROXY"
  uri                     = aws_lambda_function.raspberry_lambda.invoke_arn
  # This was just a failed attempt. It did not fix anything
  request_templates = {
    "text/html" = "{\"statusCode\": 200}"
  }
}

resource "aws_api_gateway_integration" "login_page" {
  rest_api_id          = aws_api_gateway_rest_api.raspberry_api.id
  resource_id          = aws_api_gateway_resource.home.id
  http_method          = aws_api_gateway_method.login.http_method
  type                 = "MOCK"
  timeout_milliseconds = 29000
}

resource "aws_api_gateway_method_response" "response_200" {
  rest_api_id = aws_api_gateway_rest_api.raspberry_api.id
  resource_id = aws_api_gateway_resource.home.id
  http_method = aws_api_gateway_method.login.http_method
  status_code = "200"
}

resource "aws_api_gateway_integration_response" "login_page" {
  rest_api_id = aws_api_gateway_rest_api.raspberry_api.id
  resource_id = aws_api_gateway_resource.home.id
  http_method = aws_api_gateway_method.login.http_method
  status_code = aws_api_gateway_method_response.response_200.status_code
  response_templates = {
    "text/html" = data.template_file.login_page.rendered
  }
}

resource "aws_api_gateway_deployment" "example" {
  depends_on = [
    aws_api_gateway_integration.login_page
  ]
  rest_api_id = aws_api_gateway_rest_api.raspberry_api.id
  stage_name  = "production"
}

我已按照 this blog 中的说明进行操作,但没有成功。

“200”(带引号)被认为是字符串,而不是整数

尝试 status_code = 200(不带引号)

转载这里,格式好像是:

request_templates = {
  "application/json" = jsonencode(
    {
      statusCode = 200
    }
  )
}

我也遇到了同样的问题,但看起来这行得通。

根据 Bernie 的评论,需要在 aws_api_gateway_integration terraform 资源的 request_templates 属性中明确提供此状态代码。

添加后,我终于得到了 200 个通过 MOCK 端点集成的选项。

我遇到了同样的错误,因为我的代码事先看起来像这样 - 灵感来自 terraform 文档。

resource "aws_api_gateway_integration" "api_gateway" {
  http_method = aws_api_gateway_method.api_gateway.http_method
  resource_id = aws_api_gateway_resource.api_gateway.id
  rest_api_id = aws_api_gateway_rest_api.api_gateway.id
  type        = "MOCK"
}

读完这篇文章后,它现在看起来像这样:

resource "aws_api_gateway_integration" "api_gateway" {
  http_method = aws_api_gateway_method.api_gateway.http_method
  resource_id = aws_api_gateway_resource.api_gateway.id
  rest_api_id = aws_api_gateway_rest_api.api_gateway.id
  type        = "MOCK"
  request_templates = {
    "application/json" = jsonencode(
      {
        statusCode = 200
      }
    )
  }
}

对于可能看到此问题的其他人,此错误也可能是由于需要验证当您使用 Mock 作为您的集成类型时,您确认您的 RequestTemplates 包含 statusCode 并且 statusCode 的值等于以下之一你的 IntegrationResponses/ResponseTemplates/StatusCode

类似于:

    requestTemplates: {
      "application/json": "{\"statusCode\": 200}"
    }