AWS API Gateway 和静态 HTML:“由于配置错误而执行失败:statusCode 应该是请求模板中定义的整数”

fcr*_*r79 9 amazon-web-services terraform aws-api-gateway terraform-provider-aws

我正在尝试使用 AWS API Gateway 提供静态内容。当我尝试从测试页面和 调用 URL 时curl,出现错误:

“由于配置错误,执行失败:statusCode 应该是请求模板中定义的整数”。

这是我在 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"
}

Run Code Online (Sandbox Code Playgroud)

我已按照此博客中的说明进行操作,但没有成功。

Ber*_*mus 7

只是为了在这里重新发布TheClassic 的优秀答案,格式似乎是:

request_templates = {
  "application/json" = jsonencode(
    {
      statusCode = 200
    }
  )
}
Run Code Online (Sandbox Code Playgroud)

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


The*_*sic 6

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

尝试status_code = 200(不带引号)


Les*_*dge 5

我遇到了同样的错误,因为我的代码之前看起来像这样 - 受到 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"
}
Run Code Online (Sandbox Code Playgroud)

读完这篇文章后,它现在的工作原理如下:

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
      }
    )
  }
}
Run Code Online (Sandbox Code Playgroud)