3

I am using Hashicorp Terraform to define an AWS API Gateway to hit a Lambda function. I have a requirement that I need to tag my AWS resources with a particular tag so that costs can be tracked. Terraform seems to allow this for most resources. However, when creating an API Gateway stage using aws_api_gateway_deployment I do not have the option to specify tags.

I see that Terraform recently added the resource aws_api_gateway_stage. This one does allow tags to be specified. But, aws_api_gateway_stage requires an aws_api_gateway_deployment. If I give them the same "stage_name" as so:

resource "aws_api_gateway_stage" "PlayLambdaApiGatewayStage" {
  stage_name = "${environment}"
  rest_api_id = "${aws_api_gateway_rest_api.PlayLambdaApiGateway.id}"
  deployment_id = "${aws_api_gateway_deployment.PlayLambdaApiGatewayDeployment.id}"
  tags = {
    cost-allocation = "play-${var.environment}"
  }
}

resource "aws_api_gateway_deployment" "PlayLambdaApiGatewayDeployment" {
  depends_on = [
    "aws_api_gateway_integration.PlayLambdaApiLambdaIntegration",
    "aws_api_gateway_integration.PlayLambdaApiLambdaIntegrationRoot"
  ]

  rest_api_id = "${aws_api_gateway_rest_api.PlayLambdaApiGateway.id}"
  stage_name  = "${var.environment}"
}

Then they both resources try to create the stage and I get an error:

aws_api_gateway_stage.PlayLambdaApiGatewayStage: Error creating API Gateway Stage: ConflictException: Stage already exists status code: 409, request id: f67a10c4-8aad-11e8-b486-c337ea2d214f

Here it would seem that the aws_api_gateway_deployment already created the stage, so the aws_api_gateway_stage resource failed to create it also. If I add the stage to the deployment's "depends_on" so that the stage gets created first, it complains about there being a cycle between the two.

So, it seems like:

  • aws_api_gateway_stage is only intended to add additional stages to a deployment, rather than creating a stage to use for the deployment
  • aws_api_gateway_deployment does not allow tags to be specified when it creates the stage.

Any ideas? What am I missing?

1
  • Looks like this issue. Commented Jul 18, 2018 at 19:52

1 Answer 1

2

It seems that the stage_name field in the api_gateway_deployment should actually be optional. There is a PR open to fix the fact that its not at the moment. A workaround is at the moment to set stage_name to an empty string like this:

resource "aws_api_gateway_deployment" "PlayLambdaApiGatewayDeployment" {
  depends_on = [
    "aws_api_gateway_integration.PlayLambdaApiLambdaIntegration",
    "aws_api_gateway_integration.PlayLambdaApiLambdaIntegrationRoot"
  ]

  rest_api_id = "${aws_api_gateway_rest_api.PlayLambdaApiGateway.id}"
  stage_name  = ""
}

Like this there will be no additional stage created other than the one you specify in your aws_api_gateway_stage which you can set your tags for.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.