amazon-web-services - 如何使用 terraform 在启用了 cloudwatch 指标的 API Gateway 中创建阶段?

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

我想在启用了 cloudwatch 指标的阶段部署它。为此,我需要使用 aws_api_gateway_method_settings这需要艺名。如果不使用 aws_api_gateway_stage 创建舞台它抛出一个错误,说阶段不存在。当我尝试创建一个舞台时,它所说的舞台已经存在。

我尝试过的一种解决方案是使用 aws_api_gateway_deployment 创建两个阶段。另一个使用 aws_api_gateway_stage有两个不同的名字。有没有其他解决方案?

resource "aws_api_gateway_deployment" "test-deploy" {
  depends_on = [ /*something goes here*/]

  rest_api_id = "${aws_api_gateway_rest_api.test.id}"
  stage_name  = "${var.stage_name}"

  variables = {
    "function" = "${var.lambda_function_name}"
  }
}

resource "aws_api_gateway_stage" "test" {
  stage_name = "${var.stage_name}"
  rest_api_id = "${aws_api_gateway_rest_api.test.id}"
  deployment_id = "${aws_api_gateway_deployment.test-deploy.id}"
}

resource "aws_api_gateway_method_settings" "settings" {
  rest_api_id = "${aws_api_gateway_rest_api.test.id}"
  stage_name  = "${aws_api_gateway_stage.test.stage_name}"
  method_path = "*/*"

  settings {
    metrics_enabled = true
    logging_level = "INFO"
  }
}

异常(exception):
aws_api_gateway_stage.test: Error creating API Gateway Stage: ConflictException: Stage already exists

最佳答案

我发现我们不需要明确地创建一个舞台。 aws_api_gateway_deployment创建舞台,但需要设置depends_on .我之前没有尝试过 depends_on抛出一个错误说 stage not exists .

resource "aws_api_gateway_deployment" "test-deploy" {
  depends_on = [ /*something goes here*/]
  rest_api_id = "${aws_api_gateway_rest_api.test.id}"
  stage_name  = "${var.stage_name}"
  variables = {
    "function" = "${var.lambda_function_name}"
  }
}

resource "aws_api_gateway_method_settings" "settings" {
  depends_on  = ["aws_api_gateway_deployment.test-deploy"]
  rest_api_id = "${aws_api_gateway_rest_api.test.id}"
  stage_name  = "${var.stage_name}"
  method_path = "*/*"
  settings {
    metrics_enabled = true
    logging_level = "INFO"
  }
}

关于amazon-web-services - 如何使用 terraform 在启用了 cloudwatch 指标的 API Gateway 中创建阶段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45096307/

相关文章:

linux - 从 AWS CLI 管道输出到特定行上的现有文件

amazon-web-services - 保护外部最终用户和内部系统的 AWS API Gateway 安全

amazon-web-services - AWS Lambda 链接最佳实践

amazon-web-services - 有没有办法为 CloudWatch 日志组过滤器生成 AWS 控制台 URL?

amazon-web-services - 如何在 AWS CloudWatch 中为 AWS SQS 添加偶数模式

amazon-web-services - 在不登录的情况下在AWS EC2实例上部署docker容器

.net - 如何使用 DynamoDBContext 批处理结果

amazon-web-services - 将查询字符串参数从 API 网关传递到 AWS Lambda c#

amazon-web-services - 尝试使用 golang 在 cloudwatch 上 PutLogEvents 时获取 SerializationException

amazon-web-services - 应用程序负载均衡器重定向