AWS API Gateway и stati c HTML: «Выполнение не выполнено из-за ошибки конфигурации: statusCode должен быть целым числом, определенным в шаблоне запроса» - PullRequest
0 голосов
/ 25 января 2020

Я пытаюсь обработать содержимое c, используя AWS API Gateway. Когда я пытаюсь вызвать URL-адрес как с тестовой страницы, так и с curl, я получаю сообщение об ошибке:

"Выполнение не выполнено из-за ошибки конфигурации: statusCode должен быть целым числом, определенным в запросе 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"
}

Я следовал инструкциям, как в этом блоге , но безуспешно.

Ответы [ 2 ]

1 голос
/ 27 января 2020

"200" (с кавычками) считается строкой, а не целым числом

try status_code = 200 (без кавычек)

0 голосов
/ 18 апреля 2020

Просто чтобы опубликовать отличный ответ Магнуса здесь, формат выглядит следующим образом:

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

У меня тоже была такая же проблема, но похоже, что это работает.

...