首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >API和静态HTML:“由于配置错误执行失败: statusCode应该是在请求模板中定义的整数”

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

Stack Overflow用户
提问于 2020-01-25 17:38:59
回答 5查看 4.7K关注 0票数 5

我试图使用API网关提供静态内容。当我试图从测试页面和curl调用URL时,我会得到以下错误:

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

这是我在Terraform上的配置:

代码语言:javascript
复制
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"
}

我遵循了这个博客中的指令,但没有成功。

EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2020-01-27 02:02:36

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

尝试status_code = 200 (没有引号)

票数 3
EN

Stack Overflow用户

发布于 2020-04-17 21:26:03

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

代码语言:javascript
复制
request_templates = {
  "application/json" = jsonencode(
    {
      statusCode = 200
    }
  )
}

我也有同样的问题,但看起来很有效。

票数 4
EN

Stack Overflow用户

发布于 2021-11-06 03:10:38

我也犯了同样的错误,因为我的代码看起来是这样的--受terraform文档的启发。

代码语言:javascript
复制
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"
}

在阅读了这个帖子之后,它现在的工作方式如下所示:

代码语言:javascript
复制
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
      }
    )
  }
}
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59911777

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档