如何将api_gateway_base_path_mapping与terraform一起使用?

时间:2017-06-23 15:20:22

标签: amazon-web-services aws-api-gateway terraform

我试图在aws上为api网关中的api设置自定义域名。我使用terraform设置了api。但是,当我尝试设置自定义域时,它失败并出现以下错误。

应用计划时出错:

发生1个错误:

  • module.BillingMetrics.aws_api_gateway_base_path_mapping.billing:发生了1个错误:

  • aws_api_gateway_base_path_mapping.billing:创建网关基本路径映射时出错:创建网关基本路径映射时出错:BadRequestException:指定了无效的REST API标识符     状态代码:400,请求ID:b14bbd4c-5823-11e7-a4ea-93525a34b321

我可以在terraform日志中看到它确实获得了正确的api_id。但我不明白为什么它说其余api标识符无效。

下面是我的terraform文件的摘录,显示了我如何配置api_gateway_base_path_mapping。

resource "aws_api_gateway_resource" "views_resource" {
  provider = "aws.regional"
  rest_api_id = "${aws_api_gateway_rest_api.billing_api.id}"
  parent_id   = "${aws_api_gateway_rest_api.billing_api.root_resource_id}"
  path_part   = "views"
}

resource "aws_api_gateway_method" "views-get" {
  provider = "aws.regional"
  rest_api_id   = "${aws_api_gateway_rest_api.billing_api.id}"
  resource_id   = "${aws_api_gateway_resource.views_resource.id}"
  http_method   = "GET"
  authorization = "NONE"
}

resource "aws_api_gateway_method_response" "views_200" {
  provider = "aws.regional"
  rest_api_id = "${aws_api_gateway_rest_api.billing_api.id}"
  resource_id = "${aws_api_gateway_resource.views_resource.id}"
  http_method = "${aws_api_gateway_method.views-get.http_method}"
  status_code = "200"
}

resource "aws_api_gateway_integration" "views-integration" {
  provider = "aws.regional"
  rest_api_id             = "${aws_api_gateway_rest_api.billing_api.id}"
  resource_id             = "${aws_api_gateway_resource.views_resource.id}"
  http_method             = "${aws_api_gateway_method.views-get.http_method}"
  type                    = "AWS"
  uri                     = "arn:aws:apigateway:${var.region}:lambda:path/2015-03-31/functions/arn:aws:lambda:${var.region}:${var.account_id}:function:${aws_lambda_function.get_views.function_name}/invocations"
  credentials             = "${var.metrics_role_arn}"
  http_method             = "${aws_api_gateway_method.views-get.http_method}"
  integration_http_method = "POST"
}

resource "aws_api_gateway_integration_response" "Views_Get_IntegrationResponse" {
  provider = "aws.regional"
  rest_api_id = "${aws_api_gateway_rest_api.billing_api.id}"
  resource_id = "${aws_api_gateway_resource.views_resource.id}"
  http_method = "${aws_api_gateway_method.views-get.http_method}"
  status_code = "${aws_api_gateway_method_response.views_200.status_code}"
 }

/* Deploy api */
resource "aws_api_gateway_deployment" "metric_deploy" {
  provider = "aws.regional"
  depends_on  = ["aws_api_gateway_integration.metrics-integration", "aws_api_gateway_integration.hours-integration"]
  stage_name  = "beta"
  rest_api_id = "${aws_api_gateway_rest_api.billing_api.id}"
}

resource "aws_api_gateway_domain_name" "billing" {
  domain_name       = "billing.example.com"
  certificate_arn   = "arn:aws:acm:us-east-1:6--:certificate/5--"
}

resource "aws_api_gateway_base_path_mapping" "billing" {
  api_id      = "${aws_api_gateway_rest_api.billing_api.id}"
  stage_name  = "${aws_api_gateway_deployment.metric_deploy.stage_name}"
  domain_name = "${aws_api_gateway_domain_name.billing.domain_name}"
}

resource "aws_route53_record" "billing" {
  zone_id = "Z-------"

  name = "${aws_api_gateway_domain_name.billing.domain_name}"
  type = "A"

  alias {
    name                   = "${aws_api_gateway_domain_name.billing.cloudfront_domain_name}"
    zone_id                = "${aws_api_gateway_domain_name.billing.cloudfront_zone_id}"
    evaluate_target_health = true
  }
}

是否还需要配置任何元素才能正确应用base_path_mapping?还有其他提示我可能做错了吗?

我还应该提到我在terraform 0.9.7。

0 个答案:

没有答案