Reputation: 4562
I want to deploy this in a stage with cloudwatch metrics enabled. For that i need to use aws_api_gateway_method_settings
which needs stage name. If don't create a stage using aws_api_gateway_stage
it is throwing an error saying stage not exists. When i am trying to create a stage its saying stage already exists.
One solution i tried is creating two stages one using aws_api_gateway_deployment
and another using aws_api_gateway_stage
with two different names. Is there any other solution for this?
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
Upvotes: 7
Views: 4358
Reputation: 1648
You can enable API gateway access log in Terraform using the following code:
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
}
}
Upvotes: 0
Reputation: 4562
I figured out that we don't need to create a stage explicitly. aws_api_gateway_deployment
creates a stage, but need to set depends_on
. I tried this earlier without depends_on
which throws an error saying 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"
}
}
Upvotes: 4