title: Terraform destroy API Gateway with VPC Link fails
body: <p><strong>Setup</strong></p>
  
  <ul>
  <li>Terraform v 0.11.14</li>
  <li>OpenAPI spec 3.0 to define the body of my API Gateway</li>
  </ul>
  
  <p><strong>Terraform Resources</strong></p>
  
  <p>I've created a Private API Gateway which routes traffic to an NLB via VPC Links. I have deployed the API, but for brevity I've omitted that resource from the below since it is trivial.</p>
  
  <pre class="lang-sh prettyprint-override"><code>resource "aws_api_gateway_rest_api" "this" {
    name        = "MyAPI"
    body        = "${file("./api-spec.yaml")}"
  
    endpoint_configuration {
      types = ["PRIVATE"]
    }
  }
  
  resource "aws_lb" "app" {
    name               = "MyNLB"
    internal           = true
    load_balancer_type = "network"
    subnets            = ["MySubnetIds"]
  }
  
  resource "aws_api_gateway_vpc_link" "nlb" {
    name        = "api-gateway-to-nlb"
    target_arns = ["${aws_lb.app.arn}"]
  }
  </code></pre>
  
  <p>The VPC Link is referenced in the <code>api-spec.yaml</code> file.  The relevant section is:</p>
  
  <pre><code>paths:
    /items:
      get:
        summary: Gets a collection of items
        responses:
          '200':
            description: Ok
            content:
              application/json:
                schema:
                  type: array
                  items:
                    $ref: '#/components/schemas/Item'
        x-amazon-apigateway-integration:
          type:              http_proxy
          httpMethod:        GET
          uri:               https://my-internal-nlb/api/v1/items
          connectionType:    "VPC_LINK"
          connectionId:      "${vpclink_id}"
          responses:
            default:
              statusCode: '200'
  </code></pre>
  
  <p><strong>Issue</strong></p>
  
  <p>When I run <code>terraform destroy</code>, I get the following message:</p>
  
  <blockquote>
    <p>Error: Error applying plan:</p>
    
    <p>1 error occurred:<br>
            * aws_api_gateway_vpc_link.nlb (destroy): 1 error occurred:<br>
            * aws_api_gateway_vpc_link.nlb: error deleting API Gateway VPC Link (bgzpv1): BadRequestException: Cannot delete vpc link. Vpc link 'bgzpv1', is referenced in deployed stages [POST:50f55s:development, GET:50f55s:development] in format of [Method:Resource:Stage] and also in undeployed integration [GET:50f55s, POST:50f55s] in format of
    [Method:Resource].
            status code: 400, request id: d9a9667b-8099-11e9-98d1-9f899674f4b9</p>
  </blockquote>
  
  <p>If I wait a few minutes and then run <code>terraform destroy</code> again, the following resources are destroyed:</p>
  
  <ul>
  <li><code>aws_lb.app</code></li>
  <li><code>aws_api_gateway_vpc_link.nlb</code></li>
  </ul>
  
  <p>I've had a google, but can't find much information on the topic.  There was a <a href="https://forums.aws.amazon.com/thread.jspa?messageID=819286" rel="nofollow noreferrer">ticket raised</a> on the AWS support forums, but this was a while ago and AWS indicated they were going to fix the problem</p>