Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a stage in API Gateway with enabled cloudwatch metrics using terraform?

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
like image 510
Gangaraju Avatar asked Jul 14 '17 06:07

Gangaraju


People also ask

How do I create API gateway stage?

Sign in to the API Gateway console at https://console.aws.amazon.com/apigateway . From the APIs navigation pane, choose Stages under an API. From the Stages navigation pane, choose Create. Under Create Stage, enter a stage name, for example, prod , for Stage name.

What is AWS API gateway stage?

A stage is a named reference to a deployment, which is a snapshot of the API. You use a Stage to manage and optimize a particular deployment. For example, you can configure stage settings to enable caching, customize request throttling, configure logging, define stage variables, or attach a canary release for testing.

How do I generate an API AWS API gateway?

To document your API, you can call the API Gateway REST API, use one of the AWS SDKs or AWS CLIs for API Gateway, or use the API Gateway console. In addition, you can import or export the documentation parts that are defined in an external OpenAPI file.


1 Answers

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"
  }
}
like image 183
Gangaraju Avatar answered Nov 06 '22 07:11

Gangaraju