我想在启用了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"
}
}
例外:
aws_api_gateway_stage.test: Error creating API Gateway Stage: ConflictException: Stage already exists
答案 0 :(得分:3)
我发现我们不需要明确创建一个阶段。 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"
}
}
答案 1 :(得分:0)
您可以使用以下代码在Terraform中启用API网关访问日志:
locals {
operations = <<TXT
'[ { "op" : "replace", "path" : "/accessLogSettings/destinationArn", "value" : "aws_cloudwatch_log_group_arn"},
{ "op" : "replace", "path" : "/accessLogSettings/format", "value" : log_format}]'
TXT
}
resource "null_resource" "access_log" {
provisioner "local-exec" {
command =<<CMD
aws apigateway update-stage --rest-api-id api_gw_id --stage-name stage_name --patch-operations ${local.operations}
CMD
}
}