Terraform: aws_api_gateway_integration_response should depend on aws_api_gateway_integration

Created on 11 Apr 2016  ยท  9Comments  ยท  Source: hashicorp/terraform

Hello,

I'm running Terraform v0.6.14 on Ubuntu 14.04 LTS (trusty).

I'm experimenting with the new Terraform functionality for managing AWS Lambda and AWS API Gateway. I've run into an order/dependency issue, whereby Terraform tries to create the aws_api_gateway_integration_response before it creates the aws_api_gateway_integration, resulting in an error.

Please see the snippet below:

$ terraform apply
[...snip...]
aws_api_gateway_integration_response.mcs-test-api-integration-response: Creating...
  http_method:                         "" => "GET"
  resource_id:                         "" => "vaswdm"
  response_templates.#:                "" => "1"
  response_templates.application/json: "" => ""
  rest_api_id:                         "" => "477egnmtwh"
  status_code:                         "" => "200"
aws_lambda_function.mcs-test-lambda: Creation complete
aws_api_gateway_integration.mcs-test-api-integration: Creating...
  credentials:             "" => "arn:aws:iam::{REDACTED}:role/lambda-iam-mcs-test-lambda"
  http_method:             "" => "GET"
  integration_http_method: "" => "POST"
  resource_id:             "" => "vaswdm"
  rest_api_id:             "" => "477egnmtwh"
  type:                    "" => "AWS"
  uri:                     "" => "arn:aws:apigateway:us-east-1:lambda:path/2015-03-31/functions/arn:aws:lambda:us-east-1:{REDACTED}:function:mcs-test-lambda/invocations"
aws_api_gateway_integration.mcs-test-api-integration: Creation complete
Error applying plan:

1 error(s) occurred:

* aws_api_gateway_integration_response.mcs-test-api-integration-response: Error creating API Gateway Integration Response: NotFoundException: No integrationdefined for method
    status code: 404, request id: {REDACTED}

Terraform does not automatically rollback in the face of errors.
Instead, your Terraform state file has been partially updated with
any resources that successfully completed. Please address the error
above and apply again to incrementally change your infrastructure.

At this point, terraform apply can be run a second time, and this time succeed, because the aws_api_gateway_integration was created at the end of the above apply:

$ terraform apply
aws_api_gateway_rest_api.mcs-test-api: Refreshing state... (ID: 477egnmtwh)
aws_iam_role.lambda-iam-mcs-test-lambda: Refreshing state... (ID: lambda-iam-mcs-test-lambda)
aws_api_gateway_resource.mcs-test-api-resource: Refreshing state... (ID: vaswdm)
aws_iam_role_policy.lambda-iam-policy-mcs-test-lambda: Refreshing state... (ID: lambda-iam-mcs-test-lambda:lambda-iam-policy-mcs-test-lambda)
aws_lambda_function.mcs-test-lambda: Refreshing state... (ID: mcs-test-lambda)
aws_api_gateway_method.mcs-test-api-method: Refreshing state... (ID: agm-477egnmtwh-vaswdm-GET)
aws_api_gateway_integration.mcs-test-api-integration: Refreshing state... (ID: agi-477egnmtwh-vaswdm-GET)
aws_api_gateway_method_response.200: Refreshing state... (ID: agmr-477egnmtwh-vaswdm-GET-200)
aws_api_gateway_integration_response.mcs-test-api-integration-response: Creating...
  http_method:                         "" => "GET"
  resource_id:                         "" => "vaswdm"
  response_templates.#:                "" => "1"
  response_templates.application/json: "" => ""
  rest_api_id:                         "" => "477egnmtwh"
  status_code:                         "" => "200"
aws_lambda_function.mcs-test-lambda: Modifying...
  source_code_hash: "bgemyHv9Mn8FMOXVcrI+VULROngoc5Bwu06n4MFvDIg=" => "NmUwN2E2Yzg3YmZkMzI3ZjA1MzBlNWQ1NzJiMjNlNTU0MmQxM2E3ODI4NzM5MDcwYmI0ZWE3ZTBjMTZmMGM4OA=="
aws_lambda_function.mcs-test-lambda: Modifications complete
aws_api_gateway_integration_response.mcs-test-api-integration-response: Creation complete

Apply complete! Resources: 1 added, 1 changed, 0 destroyed.

The state of your infrastructure has been saved to the path
below. This state is required to modify and destroy your
infrastructure, so keep it safe. To inspect the complete state
use the `terraform show` command.

State path: terraform.tfstate

If it helps, the aws_api_gateway_integration_response is defined in my .tf configuration file at the very bottom, after the aws_api_gateway_integration is defined.

documentation provideaws

Most helpful comment

Actually, this issue can probably be closed. I realized I could use depends_on to specify the dependency:

# API Gateway: Integration Response
resource "aws_api_gateway_integration_response" "mcs-test-api-integration-response" {
  depends_on = ["aws_api_gateway_integration.mcs-test-api-integration"]
  rest_api_id = "${aws_api_gateway_rest_api.mcs-test-api.id}"
  resource_id = "${aws_api_gateway_resource.mcs-test-api-resource.id}"
  http_method = "${aws_api_gateway_method.mcs-test-api-method.http_method}"
  status_code = "${aws_api_gateway_method_response.200.status_code}"
  response_templates = { "application/json" = "" }
}

All 9 comments

Actually, this issue can probably be closed. I realized I could use depends_on to specify the dependency:

# API Gateway: Integration Response
resource "aws_api_gateway_integration_response" "mcs-test-api-integration-response" {
  depends_on = ["aws_api_gateway_integration.mcs-test-api-integration"]
  rest_api_id = "${aws_api_gateway_rest_api.mcs-test-api.id}"
  resource_id = "${aws_api_gateway_resource.mcs-test-api-resource.id}"
  http_method = "${aws_api_gateway_method.mcs-test-api-method.http_method}"
  status_code = "${aws_api_gateway_method_response.200.status_code}"
  response_templates = { "application/json" = "" }
}

Hi @mattx86

Thanks for letting us know that this can be closed :)

Paul

I'm too new at the API gateway to say if this is a bug, but at the very least the documentation should be updated to reflect the need for this dependency.

Agreed @blalor

We'd welcome PRs changing the documentation page.

As per https://github.com/hashicorp/terraform/pull/8049, this can be closed now :)

Closed via #8049

Thanks for the ping here @Ninir :)

How can we fix this if we are using modules to create API gateway. since there is no depends_on for module.

Thanx @mattx86

I was also facing same issue,fixed it by adding dependency as you said above

I'm going to lock this issue because it has been closed for _30 days_ โณ. This helps our maintainers find and focus on the active issues.

If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

Was this page helpful?
0 / 5 - 0 ratings