# Type of request: This is a ...

[x] bug report
[ ] feature request

# Detailed description

We're having trouble testing our serverless resources with Terraform and LocalStack. The problem seems related to API Gateway Stages, however I'm not able to narrow it down. I was able to create a minimal Terraform setup to reproduce the error. 

## Expected behavior

The below code works in conjunction with LocalStack, as it does with AWS.

## Actual behavior

There seem to be two errors, but I'm not certain whether they are related or not. On the first run of Terraform against a fresh LocalStack container Terraform quits saying the REST API doesn't have any methods defined. When running Terraform again it now crashes with, at least to me, little information on what happened. 

On the other hand LocalStack does not generate any relevant output at all, not even when the debugging flag is set.

### Output from frist `terraform apply`

```
An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
  + create

Terraform will perform the following actions:

  # aws_api_gateway_deployment.service_api_deployment will be created
  + resource "aws_api_gateway_deployment" "service_api_deployment" {
      + created_date  = (known after apply)
      + execution_arn = (known after apply)
      + id            = (known after apply)
      + invoke_url    = (known after apply)
      + rest_api_id   = (known after apply)
    }

  # aws_api_gateway_integration.service_proxy_method_integration will be created
  + resource "aws_api_gateway_integration" "service_proxy_method_integration" {
      + cache_namespace         = (known after apply)
      + connection_type         = "INTERNET"
      + http_method             = "ANY"
      + id                      = (known after apply)
      + integration_http_method = "POST"
      + passthrough_behavior    = (known after apply)
      + resource_id             = (known after apply)
      + rest_api_id             = (known after apply)
      + timeout_milliseconds    = 29000
      + type                    = "AWS_PROXY"
      + uri                     = (known after apply)
    }

  # aws_api_gateway_method.service_proxy_method will be created
  + resource "aws_api_gateway_method" "service_proxy_method" {
      + api_key_required = false
      + authorization    = "NONE"
      + http_method      = "ANY"
      + id               = (known after apply)
      + resource_id      = (known after apply)
      + rest_api_id      = (known after apply)
    }

  # aws_api_gateway_resource.service_proxy_resource will be created
  + resource "aws_api_gateway_resource" "service_proxy_resource" {
      + id          = (known after apply)
      + parent_id   = (known after apply)
      + path        = (known after apply)
      + path_part   = "{proxy+}"
      + rest_api_id = (known after apply)
    }

  # aws_api_gateway_rest_api.service_api will be created
  + resource "aws_api_gateway_rest_api" "service_api" {
      + api_key_source           = "HEADER"
      + arn                      = (known after apply)
      + created_date             = (known after apply)
      + execution_arn            = (known after apply)
      + id                       = (known after apply)
      + minimum_compression_size = -1
      + name                     = "test"
      + root_resource_id         = (known after apply)

      + endpoint_configuration {
          + types            = (known after apply)
          + vpc_endpoint_ids = (known after apply)
        }
    }

  # aws_api_gateway_stage.service_api_stage will be created
  + resource "aws_api_gateway_stage" "service_api_stage" {
      + arn                = (known after apply)
      + cache_cluster_size = "0.5"
      + deployment_id      = (known after apply)
      + execution_arn      = (known after apply)
      + id                 = (known after apply)
      + invoke_url         = (known after apply)
      + rest_api_id        = (known after apply)
      + stage_name         = "test"
    }

  # aws_iam_role.service_function_role will be created
  + resource "aws_iam_role" "service_function_role" {
      + arn                   = (known after apply)
      + assume_role_policy    = jsonencode(
            {
              + Statement = [
                  + {
                      + Action    = "sts:AssumeRole"
                      + Effect    = "Allow"
                      + Principal = {
                          + Service = "lambda.amazonaws.com"
                        }
                      + Sid       = ""
                    },
                ]
              + Version   = "2012-10-17"
            }
        )
      + create_date           = (known after apply)
      + force_detach_policies = false
      + id                    = (known after apply)
      + max_session_duration  = 3600
      + name                  = "test"
      + path                  = "/"
      + unique_id             = (known after apply)
    }

  # aws_lambda_alias.main will be created
  + resource "aws_lambda_alias" "main" {
      + arn              = (known after apply)
      + description      = "points to latest version"
      + function_name    = (known after apply)
      + function_version = (known after apply)
      + id               = (known after apply)
      + invoke_arn       = (known after apply)
      + name             = "main"
    }

  # aws_lambda_function.service_function will be created
  + resource "aws_lambda_function" "service_function" {
      + arn                            = (known after apply)
      + function_name                  = "test"
      + handler                        = "test"
      + id                             = (known after apply)
      + invoke_arn                     = (known after apply)
      + last_modified                  = (known after apply)
      + memory_size                    = 256
      + publish                        = true
      + qualified_arn                  = (known after apply)
      + reserved_concurrent_executions = -1
      + role                           = (known after apply)
      + runtime                        = "dotnetcore3.1"
      + s3_bucket                      = "test"
      + s3_key                         = "test.zip"
      + source_code_hash               = (known after apply)
      + source_code_size               = (known after apply)
      + timeout                        = 300
      + version                        = (known after apply)

      + tracing_config {
          + mode = "Active"
        }
    }

  # aws_lambda_permission.service_function_api_invocation_permission will be created
  + resource "aws_lambda_permission" "service_function_api_invocation_permission" {
      + action        = "lambda:InvokeFunction"
      + function_name = "test"
      + id            = (known after apply)
      + principal     = "apigateway.amazonaws.com"
      + qualifier     = "main"
      + source_arn    = (known after apply)
      + statement_id  = "AllowExecutionFromAPIGateway"
    }

  # aws_s3_bucket.local-devops-bucket will be created
  + resource "aws_s3_bucket" "local-devops-bucket" {
      + acceleration_status         = (known after apply)
      + acl                         = "private"
      + arn                         = (known after apply)
      + bucket                      = "test"
      + bucket_domain_name          = (known after apply)
      + bucket_regional_domain_name = (known after apply)
      + force_destroy               = false
      + hosted_zone_id              = (known after apply)
      + id                          = (known after apply)
      + region                      = (known after apply)
      + request_payer               = (known after apply)
      + website_domain              = (known after apply)
      + website_endpoint            = (known after apply)

      + versioning {
          + enabled    = (known after apply)
          + mfa_delete = (known after apply)
        }
    }

  # aws_s3_bucket_object.service_function_archive will be created
  + resource "aws_s3_bucket_object" "service_function_archive" {
      + acl                    = "private"
      + bucket                 = "test"
      + content_type           = (known after apply)
      + etag                   = (known after apply)
      + force_destroy          = false
      + id                     = (known after apply)
      + key                    = "test.zip"
      + kms_key_id             = (known after apply)
      + server_side_encryption = (known after apply)
      + source                 = "test.zip"
      + storage_class          = (known after apply)
      + version_id             = (known after apply)
    }

Plan: 12 to add, 0 to change, 0 to destroy.

Do you want to perform these actions?
  Terraform will perform the actions described above.
  Only 'yes' will be accepted to approve.

  Enter a value: yes

aws_api_gateway_rest_api.service_api: Creating...
aws_iam_role.service_function_role: Creating...
aws_s3_bucket_object.service_function_archive: Creating...
aws_s3_bucket.local-devops-bucket: Creating...
aws_iam_role.service_function_role: Creation complete after 0s [id=test]
aws_api_gateway_rest_api.service_api: Creation complete after 0s [id=ep4yjxqb8x]
aws_api_gateway_resource.service_proxy_resource: Creating...
aws_api_gateway_deployment.service_api_deployment: Creating...
aws_s3_bucket_object.service_function_archive: Creation complete after 0s [id=test.zip]
aws_lambda_function.service_function: Creating...
aws_api_gateway_resource.service_proxy_resource: Creation complete after 0s [id=4b9q58vlzd]
aws_api_gateway_method.service_proxy_method: Creating...
aws_api_gateway_method.service_proxy_method: Creation complete after 1s [id=agm-ep4yjxqb8x-4b9q58vlzd-ANY]
aws_s3_bucket.local-devops-bucket: Creation complete after 1s [id=test]
aws_lambda_function.service_function: Creation complete after 7s [id=test]
aws_lambda_alias.main: Creating...
aws_lambda_alias.main: Creation complete after 0s [id=arn:aws:lambda:eu-central-1:000000000000:function:test:main]
aws_lambda_permission.service_function_api_invocation_permission: Creating...
aws_api_gateway_integration.service_proxy_method_integration: Creating...
aws_api_gateway_integration.service_proxy_method_integration: Creation complete after 0s [id=agi-ep4yjxqb8x-4b9q58vlzd-ANY]
aws_lambda_permission.service_function_api_invocation_permission: Creation complete after 0s [id=AllowExecutionFromAPIGateway]

Error: Error creating API Gateway Deployment: : The REST API doesn't contain any methods
	status code: 400, request id: 
```

### Output from second `terraform apply`

```
aws_iam_role.service_function_role: Refreshing state... [id=test]
aws_s3_bucket_object.service_function_archive: Refreshing state... [id=test.zip]
aws_api_gateway_rest_api.service_api: Refreshing state... [id=ep4yjxqb8x]
aws_s3_bucket.local-devops-bucket: Refreshing state... [id=test]
aws_api_gateway_resource.service_proxy_resource: Refreshing state... [id=4b9q58vlzd]
aws_lambda_function.service_function: Refreshing state... [id=test]
aws_api_gateway_method.service_proxy_method: Refreshing state... [id=agm-ep4yjxqb8x-4b9q58vlzd-ANY]
aws_lambda_alias.main: Refreshing state... [id=arn:aws:lambda:eu-central-1:000000000000:function:test:main]
aws_lambda_permission.service_function_api_invocation_permission: Refreshing state... [id=AllowExecutionFromAPIGateway]
aws_api_gateway_integration.service_proxy_method_integration: Refreshing state... [id=agi-ep4yjxqb8x-4b9q58vlzd-ANY]

An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
  + create
  ~ update in-place
-/+ destroy and then create replacement

Terraform will perform the following actions:

  # aws_api_gateway_deployment.service_api_deployment will be created
  + resource "aws_api_gateway_deployment" "service_api_deployment" {
      + created_date  = (known after apply)
      + execution_arn = (known after apply)
      + id            = (known after apply)
      + invoke_url    = (known after apply)
      + rest_api_id   = "ep4yjxqb8x"
    }

  # aws_api_gateway_integration.service_proxy_method_integration will be updated in-place
  ~ resource "aws_api_gateway_integration" "service_proxy_method_integration" {
        cache_key_parameters    = []
        cache_namespace         = "96ba41d5"
        connection_type         = "INTERNET"
        http_method             = "ANY"
        id                      = "agi-ep4yjxqb8x-4b9q58vlzd-ANY"
        integration_http_method = "POST"
        passthrough_behavior    = "WHEN_NO_MATCH"
        request_parameters      = {}
        request_templates       = {}
        resource_id             = "4b9q58vlzd"
        rest_api_id             = "ep4yjxqb8x"
      ~ timeout_milliseconds    = 0 -> 29000
        type                    = "AWS_PROXY"
        uri                     = "arn:aws:apigateway:eu-central-1:lambda:path/2015-03-31/functions/arn:aws:lambda:eu-central-1:000000000000:function:test:main/invocations"
    }

  # aws_api_gateway_stage.service_api_stage will be created
  + resource "aws_api_gateway_stage" "service_api_stage" {
      + arn                = (known after apply)
      + cache_cluster_size = "0.5"
      + deployment_id      = (known after apply)
      + execution_arn      = (known after apply)
      + id                 = (known after apply)
      + invoke_url         = (known after apply)
      + rest_api_id        = "ep4yjxqb8x"
      + stage_name         = "test"
    }

  # aws_lambda_alias.main will be updated in-place
  ~ resource "aws_lambda_alias" "main" {
        arn              = "arn:aws:lambda:eu-central-1:000000000000:function:test:main"
        description      = "points to latest version"
        function_name    = "arn:aws:lambda:eu-central-1:000000000000:function:test"
      ~ function_version = "1" -> (known after apply)
        id               = "arn:aws:lambda:eu-central-1:000000000000:function:test:main"
        invoke_arn       = "arn:aws:apigateway:eu-central-1:lambda:path/2015-03-31/functions/arn:aws:lambda:eu-central-1:000000000000:function:test:main/invocations"
        name             = "main"
    }

  # aws_lambda_function.service_function will be updated in-place
  ~ resource "aws_lambda_function" "service_function" {
        arn                            = "arn:aws:lambda:eu-central-1:000000000000:function:test"
        function_name                  = "test"
        handler                        = "test"
        id                             = "test"
        invoke_arn                     = "arn:aws:apigateway:eu-central-1:lambda:path/2015-03-31/functions/arn:aws:lambda:eu-central-1:000000000000:function:test/invocations"
        last_modified                  = "2020-11-02T12:02:20.471+0000"
        layers                         = []
        memory_size                    = 256
        publish                        = true
      ~ qualified_arn                  = "arn:aws:lambda:eu-central-1:000000000000:function:test:1" -> (known after apply)
        reserved_concurrent_executions = -1
        role                           = "arn:aws:iam::000000000000:role/test"
        runtime                        = "dotnetcore3.1"
        s3_bucket                      = "test"
        s3_key                         = "test.zip"
        source_code_hash               = "b8UrP6K2Q/w+4t0ZUTTh/AoI8bzMljmKvc/i1Ka+/Ho="
        source_code_size               = 2875930
        tags                           = {}
        timeout                        = 300
      ~ version                        = "1" -> (known after apply)

      ~ tracing_config {
          ~ mode = "PassThrough" -> "Active"
        }
    }

  # aws_lambda_permission.service_function_api_invocation_permission must be replaced
-/+ resource "aws_lambda_permission" "service_function_api_invocation_permission" {
        action        = "lambda:InvokeFunction"
        function_name = "test"
      ~ id            = "AllowExecutionFromAPIGateway" -> (known after apply)
        principal     = "apigateway.amazonaws.com"
      + qualifier     = "main" # forces replacement
        source_arn    = "arn:aws:execute-api:eu-central-1::ep4yjxqb8x/*/*/*"
        statement_id  = "AllowExecutionFromAPIGateway"
    }

Plan: 3 to add, 3 to change, 1 to destroy.

Do you want to perform these actions?
  Terraform will perform the actions described above.
  Only 'yes' will be accepted to approve.

  Enter a value: yes

aws_lambda_permission.service_function_api_invocation_permission: Destroying... [id=AllowExecutionFromAPIGateway]
aws_api_gateway_deployment.service_api_deployment: Creating...
aws_lambda_function.service_function: Modifying... [id=test]
aws_api_gateway_deployment.service_api_deployment: Creation complete after 0s [id=053vuojcjh]
aws_api_gateway_stage.service_api_stage: Creating...

Error: rpc error: code = Unavailable desc = transport is closing



Error: rpc error: code = Unavailable desc = transport is closing



Error: rpc error: code = Unavailable desc = transport is closing


panic: runtime error: invalid memory address or nil pointer dereference
2020-11-02T13:02:36.909+0100 [DEBUG] plugin.terraform-provider-aws_v3.13.0_x5: [signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x48d844c]
2020-11-02T13:02:36.909+0100 [DEBUG] plugin.terraform-provider-aws_v3.13.0_x5: 
2020-11-02T13:02:36.909+0100 [DEBUG] plugin.terraform-provider-aws_v3.13.0_x5: goroutine 89 [running]:
2020-11-02T13:02:36.909+0100 [DEBUG] plugin.terraform-provider-aws_v3.13.0_x5: github.com/terraform-providers/terraform-provider-aws/aws.resourceAwsApiGatewayStageCreate(0xc001674780, 0x5f0a640, 0xc000179b80, 0x0, 0xffffffffffffffff)
2020-11-02T13:02:36.909+0100 [DEBUG] plugin.terraform-provider-aws_v3.13.0_x5: 	/opt/teamcity-agent/work/5d79fe75d4460a2f/src/github.com/terraform-providers/terraform-provider-aws/aws/resource_aws_api_gateway_stage.go:172 +0x85c
2020-11-02T13:02:36.909+0100 [DEBUG] plugin.terraform-provider-aws_v3.13.0_x5: github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema.(*Resource).create(0xc00041b3f0, 0x7688d40, 0xc0010d6700, 0xc001674780, 0x5f0a640, 0xc000179b80, 0x0, 0x0, 0x0)
2020-11-02T13:02:36.909+0100 [DEBUG] plugin.terraform-provider-aws_v3.13.0_x5: 	/opt/teamcity-agent/work/5d79fe75d4460a2f/pkg/mod/github.com/hashicorp/terraform-plugin-sdk/v2@v2.1.0/helper/schema/resource.go:268 +0x88
2020-11-02T13:02:36.909+0100 [DEBUG] plugin.terraform-provider-aws_v3.13.0_x5: github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema.(*Resource).Apply(0xc00041b3f0, 0x7688d40, 0xc0010d6700, 0xc0004b88c0, 0xc00218cb20, 0x5f0a640, 0xc000179b80, 0x0, 0x0, 0x0, ...)
2020-11-02T13:02:36.909+0100 [DEBUG] plugin.terraform-provider-aws_v3.13.0_x5: 	/opt/teamcity-agent/work/5d79fe75d4460a2f/pkg/mod/github.com/hashicorp/terraform-plugin-sdk/v2@v2.1.0/helper/schema/resource.go:386 +0x681
2020-11-02T13:02:36.909+0100 [DEBUG] plugin.terraform-provider-aws_v3.13.0_x5: github.com/hashicorp/terraform-plugin-sdk/v2/internal/helper/plugin.(*GRPCProviderServer).ApplyResourceChange(0xc000c91780, 0x7688d40, 0xc0010d6700, 0xc0004b8770, 0xc000c91780, 0xc000c91790, 0x6c492b0)
2020-11-02T13:02:36.909+0100 [DEBUG] plugin.terraform-provider-aws_v3.13.0_x5: 	/opt/teamcity-agent/work/5d79fe75d4460a2f/pkg/mod/github.com/hashicorp/terraform-plugin-sdk/v2@v2.1.0/internal/helper/plugin/grpc_provider.go:952 +0x8b2
2020-11-02T13:02:36.909+0100 [DEBUG] plugin.terraform-provider-aws_v3.13.0_x5: github.com/hashicorp/terraform-plugin-sdk/v2/internal/tfplugin5._Provider_ApplyResourceChange_Handler.func1(0x7688d40, 0xc0010d6700, 0x6943a20, 0xc0004b8770, 0xc0010d6700, 0x6091660, 0xc000e58d01, 0xc00218c860)
2020-11-02T13:02:36.909+0100 [DEBUG] plugin.terraform-provider-aws_v3.13.0_x5: 	/opt/teamcity-agent/work/5d79fe75d4460a2f/pkg/mod/github.com/hashicorp/terraform-plugin-sdk/v2@v2.1.0/internal/tfplugin5/tfplugin5.pb.go:3312 +0x86
2020-11-02T13:02:36.909+0100 [DEBUG] plugin.terraform-provider-aws_v3.13.0_x5: github.com/hashicorp/terraform-plugin-sdk/v2/plugin.Serve.func3.1(0x7688e00, 0xc00119c300, 0x6943a20, 0xc0004b8770, 0xc00218c840, 0xc00218c860, 0xc000a77ba0, 0x1090018, 0x6737900, 0xc00119c300)
2020-11-02T13:02:36.909+0100 [DEBUG] plugin.terraform-provider-aws_v3.13.0_x5: 	/opt/teamcity-agent/work/5d79fe75d4460a2f/pkg/mod/github.com/hashicorp/terraform-plugin-sdk/v2@v2.1.0/plugin/serve.go:76 +0x87
2020-11-02T13:02:36.909+0100 [DEBUG] plugin.terraform-provider-aws_v3.13.0_x5: github.com/hashicorp/terraform-plugin-sdk/v2/internal/tfplugin5._Provider_ApplyResourceChange_Handler(0x69ca620, 0xc000c91780, 0x7688e00, 0xc00119c300, 0xc000e58de0, 0xc0018c7ea0, 0x7688e00, 0xc00119c300, 0xc00211aa00, 0x265)
2020-11-02T13:02:36.909+0100 [DEBUG] plugin.terraform-provider-aws_v3.13.0_x5: 	/opt/teamcity-agent/work/5d79fe75d4460a2f/pkg/mod/github.com/hashicorp/terraform-plugin-sdk/v2@v2.1.0/internal/tfplugin5/tfplugin5.pb.go:3314 +0x14b
2020-11-02T13:02:36.909+0100 [DEBUG] plugin.terraform-provider-aws_v3.13.0_x5: google.golang.org/grpc.(*Server).processUnaryRPC(0xc000bb6700, 0x76ac4e0, 0xc001064480, 0xc0002a8c00, 0xc000d264b0, 0xac87d80, 0x0, 0x0, 0x0)
2020-11-02T13:02:36.909+0100 [DEBUG] plugin.terraform-provider-aws_v3.13.0_x5: 	/opt/teamcity-agent/work/5d79fe75d4460a2f/pkg/mod/google.golang.org/grpc@v1.30.0/server.go:1171 +0x50a
2020-11-02T13:02:36.909+0100 [DEBUG] plugin.terraform-provider-aws_v3.13.0_x5: google.golang.org/grpc.(*Server).handleStream(0xc000bb6700, 0x76ac4e0, 0xc001064480, 0xc0002a8c00, 0x0)
2020-11-02T13:02:36.909+0100 [DEBUG] plugin.terraform-provider-aws_v3.13.0_x5: 	/opt/teamcity-agent/work/5d79fe75d4460a2f/pkg/mod/google.golang.org/grpc@v1.30.0/server.go:1494 +0xccd
2020-11-02T13:02:36.909+0100 [DEBUG] plugin.terraform-provider-aws_v3.13.0_x5: google.golang.org/grpc.(*Server).serveStreams.func1.2(0xc000f96e20, 0xc000bb6700, 0x76ac4e0, 0xc001064480, 0xc0002a8c00)
2020-11-02T13:02:36.909+0100 [DEBUG] plugin.terraform-provider-aws_v3.13.0_x5: 	/opt/teamcity-agent/work/5d79fe75d4460a2f/pkg/mod/google.golang.org/grpc@v1.30.0/server.go:834 +0xa1
2020-11-02T13:02:36.909+0100 [DEBUG] plugin.terraform-provider-aws_v3.13.0_x5: created by google.golang.org/grpc.(*Server).serveStreams.func1
2020-11-02T13:02:36.909+0100 [DEBUG] plugin.terraform-provider-aws_v3.13.0_x5: 	/opt/teamcity-agent/work/5d79fe75d4460a2f/pkg/mod/google.golang.org/grpc@v1.30.0/server.go:832 +0x204
2020-11-02T13:02:36.912+0100 [WARN]  plugin.stdio: received EOF, stopping recv loop: err="rpc error: code = Unavailable desc = transport is closing"
2020/11/02 13:02:36 [DEBUG] aws_lambda_permission.service_function_api_invocation_permission: apply errored, but we're indicating that via the Error pointer rather than returning it: rpc error: code = Unavailable desc = transport is closing
2020/11/02 13:02:36 [TRACE] eval: *terraform.EvalWriteState
2020/11/02 13:02:36 [DEBUG] aws_lambda_function.service_function: apply errored, but we're indicating that via the Error pointer rather than returning it: rpc error: code = Unavailable desc = transport is closing
2020/11/02 13:02:36 [TRACE] EvalWriteState: writing current state object for aws_lambda_permission.service_function_api_invocation_permission
2020/11/02 13:02:36 [TRACE] eval: *terraform.EvalMaybeTainted
2020/11/02 13:02:36 [TRACE] eval: *terraform.EvalWriteState
2020/11/02 13:02:36 [TRACE] EvalWriteState: recording 2 dependencies for aws_lambda_function.service_function
2020/11/02 13:02:36 [TRACE] EvalWriteState: writing current state object for aws_lambda_function.service_function
2020/11/02 13:02:36 [DEBUG] aws_api_gateway_stage.service_api_stage: apply errored, but we're indicating that via the Error pointer rather than returning it: rpc error: code = Unavailable desc = transport is closing
2020/11/02 13:02:36 [TRACE] eval: *terraform.EvalMaybeTainted
2020/11/02 13:02:36 [TRACE] EvalMaybeTainted: aws_api_gateway_stage.service_api_stage encountered an error during creation, so it is now marked as tainted
2020/11/02 13:02:36 [TRACE] eval: *terraform.EvalWriteState
2020/11/02 13:02:36 [TRACE] eval: *terraform.EvalApplyPost
2020/11/02 13:02:36 [TRACE] EvalWriteState: removing state object for aws_api_gateway_stage.service_api_stage
2020/11/02 13:02:36 [TRACE] eval: *terraform.EvalApplyProvisioners
2020/11/02 13:02:36 [TRACE] EvalApplyProvisioners: aws_api_gateway_stage.service_api_stage has no state, so skipping provisioners
2020/11/02 13:02:36 [TRACE] eval: *terraform.EvalMaybeTainted
2020/11/02 13:02:36 [TRACE] EvalMaybeTainted: aws_api_gateway_stage.service_api_stage encountered an error during creation, so it is now marked as tainted
2020/11/02 13:02:36 [ERROR] eval: *terraform.EvalApplyPost, err: rpc error: code = Unavailable desc = transport is closing
2020/11/02 13:02:36 [TRACE] eval: *terraform.EvalWriteState
2020/11/02 13:02:36 [ERROR] eval: *terraform.EvalSequence, err: rpc error: code = Unavailable desc = transport is closing
2020/11/02 13:02:36 [TRACE] EvalWriteState: removing state object for aws_api_gateway_stage.service_api_stage
2020/11/02 13:02:36 [ERROR] eval: *terraform.EvalOpFilter, err: rpc error: code = Unavailable desc = transport is closing
2020/11/02 13:02:36 [TRACE] eval: *terraform.EvalIf
2020/11/02 13:02:36 [TRACE] eval: *terraform.EvalIf
2020/11/02 13:02:36 [TRACE] [walkApply] Exiting eval tree: aws_lambda_permission.service_function_api_invocation_permission (destroy)
2020/11/02 13:02:36 [TRACE] eval: *terraform.EvalWriteDiff
2020/11/02 13:02:36 [TRACE] eval: *terraform.EvalApplyPost
2020/11/02 13:02:36 [TRACE] vertex "aws_lambda_permission.service_function_api_invocation_permission (destroy)": visit complete
2020/11/02 13:02:36 [ERROR] eval: *terraform.EvalApplyPost, err: rpc error: code = Unavailable desc = transport is closing
2020/11/02 13:02:36 [ERROR] eval: *terraform.EvalSequence, err: rpc error: code = Unavailable desc = transport is closing
2020/11/02 13:02:36 [TRACE] [walkApply] Exiting eval tree: aws_api_gateway_stage.service_api_stage
2020/11/02 13:02:36 [TRACE] vertex "aws_api_gateway_stage.service_api_stage": visit complete
2020/11/02 13:02:36 [TRACE] eval: *terraform.EvalApplyProvisioners
2020/11/02 13:02:36 [TRACE] EvalApplyProvisioners: aws_lambda_function.service_function is not freshly-created, so no provisioning is required
2020/11/02 13:02:36 [TRACE] eval: *terraform.EvalMaybeTainted
2020/11/02 13:02:36 [TRACE] eval: *terraform.EvalWriteState
2020/11/02 13:02:36 [TRACE] EvalWriteState: recording 2 dependencies for aws_lambda_function.service_function
2020/11/02 13:02:36 [TRACE] EvalWriteState: writing current state object for aws_lambda_function.service_function
2020-11-02T13:02:36.912+0100 [DEBUG] plugin: plugin process exited: path=.terraform/plugins/registry.terraform.io/hashicorp/aws/3.13.0/darwin_amd64/terraform-provider-aws_v3.13.0_x5 pid=5341 error="exit status 2"
2020/11/02 13:02:36 [TRACE] eval: *terraform.EvalIf
2020/11/02 13:02:36 [TRACE] eval: *terraform.EvalIf
2020/11/02 13:02:36 [TRACE] eval: *terraform.EvalWriteDiff
2020/11/02 13:02:36 [TRACE] eval: *terraform.EvalApplyPost
2020/11/02 13:02:36 [ERROR] eval: *terraform.EvalApplyPost, err: rpc error: code = Unavailable desc = transport is closing
2020/11/02 13:02:36 [ERROR] eval: *terraform.EvalSequence, err: rpc error: code = Unavailable desc = transport is closing
2020/11/02 13:02:36 [TRACE] [walkApply] Exiting eval tree: aws_lambda_function.service_function
2020/11/02 13:02:36 [TRACE] vertex "aws_lambda_function.service_function": visit complete
2020/11/02 13:02:36 [TRACE] dag/walk: upstream of "aws_lambda_alias.main" errored, so skipping
2020/11/02 13:02:36 [TRACE] dag/walk: upstream of "aws_lambda_permission.service_function_api_invocation_permission" errored, so skipping
2020/11/02 13:02:36 [TRACE] dag/walk: upstream of "aws_api_gateway_integration.service_proxy_method_integration" errored, so skipping
2020/11/02 13:02:36 [TRACE] dag/walk: upstream of "meta.count-boundary (EachMode fixup)" errored, so skipping
2020/11/02 13:02:36 [TRACE] dag/walk: upstream of "provider[\"registry.terraform.io/hashicorp/aws\"] (close)" errored, so skipping
2020/11/02 13:02:36 [TRACE] dag/walk: upstream of "root" errored, so skipping
2020/11/02 13:02:36 [TRACE] statemgr.Filesystem: have already backed up original terraform.tfstate to terraform.tfstate.backup on a previous write
2020/11/02 13:02:36 [TRACE] statemgr.Filesystem: state has changed since last snapshot, so incrementing serial to 13
2020/11/02 13:02:36 [TRACE] statemgr.Filesystem: writing snapshot at terraform.tfstate
2020/11/02 13:02:36 [TRACE] statemgr.Filesystem: removing lock metadata file .terraform.tfstate.lock.info
2020/11/02 13:02:36 [TRACE] statemgr.Filesystem: unlocking terraform.tfstate using fcntl flock
2020-11-02T13:02:36.922+0100 [DEBUG] plugin: plugin exited



!!!!!!!!!!!!!!!!!!!!!!!!!!! TERRAFORM CRASH !!!!!!!!!!!!!!!!!!!!!!!!!!!!

Terraform crashed! This is always indicative of a bug within Terraform.
A crash log has been placed at "crash.log" relative to your current
working directory. It would be immensely helpful if you could please
report the crash with Terraform[1] so that we can fix this.

When reporting bugs, please include your terraform version. That
information is available on the first line of crash.log. You can also
get it by running 'terraform --version' on the command line.

SECURITY WARNING: the "crash.log" file that was created may contain 
sensitive information that must be redacted before it is safe to share 
on the issue tracker.

[1]: https://github.com/hashicorp/terraform/issues

!!!!!!!!!!!!!!!!!!!!!!!!!!! TERRAFORM CRASH !!!!!!!!!!!!!!!!!!!!!!!!!!!!
```

# Steps to reproduce

## Command used to start LocalStack

`LOCALSTACK_API_KEY=xxx DEBUG=1 localstack start`

## Client code (AWS SDK code snippet, or sequence of "awslocal" commands)

This is the minimal Terraform code I could write that reproduces the error. Steps to reproduce:
1. Copy below code into a new file
2. Run `terraform init`
3. Run `TF_LOG=DEBUG terraform apply`. Terraform will create some resources and abort with:

`Error: Error creating API Gateway Deployment: : The REST API doesn't contain any methods
	status code: 400, request id: `

4. Run `TF_LOG=DEBUG terraform apply` again. This time the AWS provider will crash as stated before.


```hcl
provider "aws" {
  region                      = "eu-central-1"
  access_key                  = "test"
  secret_key                  = "test"
  s3_force_path_style         = true
  skip_credentials_validation = true
  skip_metadata_api_check     = true
  skip_requesting_account_id  = true

  endpoints {
    apigateway     = "http://localhost:4566"
    cloudformation = "http://localhost:4566"
    cloudwatch     = "http://localhost:4566"
    dynamodb       = "http://localhost:4566"
    es             = "http://localhost:4566"
    firehose       = "http://localhost:4566"
    iam            = "http://localhost:4566"
    kinesis        = "http://localhost:4566"
    lambda         = "http://localhost:4566"
    route53        = "http://localhost:4566"
    redshift       = "http://localhost:4566"
    s3             = "http://localhost:4566"
    secretsmanager = "http://localhost:4566"
    ses            = "http://localhost:4566"
    sns            = "http://localhost:4566"
    sqs            = "http://localhost:4566"
    ssm            = "http://localhost:4566"
    stepfunctions  = "http://localhost:4566"
    sts            = "http://localhost:4566"
  }
}

##############################
# API GATEWAY
##############################

resource "aws_api_gateway_rest_api" "service_api" {
  name           = "test"
  api_key_source = "HEADER"
}

resource "aws_api_gateway_deployment" "service_api_deployment" {
  rest_api_id = aws_api_gateway_rest_api.service_api.id

  lifecycle {
    create_before_destroy = true
  }
}

resource "aws_api_gateway_stage" "service_api_stage" {
  stage_name           = "test"
  rest_api_id          = aws_api_gateway_rest_api.service_api.id
  deployment_id        = aws_api_gateway_deployment.service_api_deployment.id
  # needed to prevent Terraform from updating this resource every time
  cache_cluster_size = "0.5"
}

resource "aws_api_gateway_resource" "service_proxy_resource" {
  rest_api_id = aws_api_gateway_rest_api.service_api.id
  parent_id   = aws_api_gateway_rest_api.service_api.root_resource_id
  path_part   = "{proxy+}"
}

resource "aws_api_gateway_method" "service_proxy_method" {
  rest_api_id   = aws_api_gateway_rest_api.service_api.id
  resource_id   = aws_api_gateway_resource.service_proxy_resource.id
  authorization = "NONE"
  http_method   = "ANY"
}

resource "aws_api_gateway_integration" "service_proxy_method_integration" {
  rest_api_id = aws_api_gateway_rest_api.service_api.id
  resource_id = aws_api_gateway_resource.service_proxy_resource.id
  http_method = aws_api_gateway_method.service_proxy_method.http_method
  /* credentials             = "DO NOT SET" */
  integration_http_method = "POST"
  type                    = "AWS_PROXY"
  uri                     = aws_lambda_alias.main.invoke_arn
}


##############################
# S3 ARCHIVE
##############################
resource "aws_s3_bucket" "local-devops-bucket" {
  bucket = "test"
}

resource "aws_s3_bucket_object" "service_function_archive" {
  bucket = "test"
  key    = "test.zip"
  source = "test.zip"
}


##############################
# LAMBDA FUNCTION
##############################


resource "aws_lambda_function" "service_function" {
  s3_bucket     = "test"
  s3_key        = aws_s3_bucket_object.service_function_archive.key
  function_name = "test"
  role          = aws_iam_role.service_function_role.arn
  handler       = "test"
  runtime       = "dotnetcore3.1"
  memory_size   = 256
  timeout       = "300"
  publish       = true

  tracing_config {
    mode = "Active"
  }
}

resource "aws_lambda_alias" "main" {
  name             = "main"
  description      = "points to latest version"
  function_name    = aws_lambda_function.service_function.arn
  function_version = aws_lambda_function.service_function.version
}

resource "aws_lambda_permission" "service_function_api_invocation_permission" {
  statement_id  = "AllowExecutionFromAPIGateway"
  action        = "lambda:InvokeFunction"
  function_name = aws_lambda_function.service_function.function_name
  principal     = "apigateway.amazonaws.com"
  qualifier     = aws_lambda_alias.main.name

  # More: http://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-control-access-using-iam-policies-to-invoke-api.html
  //source_arn = "${aws_api_gateway_rest_api.service_api.execution_arn}/*/*/"
  source_arn = "${aws_api_gateway_rest_api.service_api.execution_arn}/*/*/*" // must contain full path as defined by API
}

resource "aws_iam_role" "service_function_role" {
  name = "test"

  assume_role_policy = <<EOF
{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Action": "sts:AssumeRole",
      "Principal": {
        "Service": "lambda.amazonaws.com"
      },
      "Effect": "Allow",
      "Sid": ""
    }
  ]
}
EOF
}
```
rest_api = self.get_rest_api(function_id)
        
        rest_api.resources = {}
        for path in body['paths']:
            child_id = create_id()
            child = Resource(
                id=child_id,
                region_name=rest_api.region_name,
                api_id=rest_api.id,
                path_part=path,
                parent_id='',
            )
            for m, payload in body['paths'][path].items():
                m = m.upper()
                payload = payload['x-amazon-apigateway-integration']
                child.add_method(
                    m, None, None
                )
                integration = Integration(
                    http_method=m,