Repository: localstack/localstack

Issue:
Error terraform destroy vpc module using localstack
<!-- Love localstack? Please consider supporting our collective:
👉  https://opencollective.com/localstack/donate -->

# Type of request: This is a ...

[x] bug report
[ ] feature request

# Detailed description

Hi, I try to run some test with localstack to test my module terraform VPC and the terraform apply works but when I run terraform destroy I get the error and terraform crash:

```
Error: rpc error: code = Unavailable desc = transport is closin
```

# Steps to reproduce

```
provider "aws" {
  access_key                  = "mock_access_key"
  region                      = "us-east-1"
  s3_force_path_style         = true
  secret_key                  = "mock_secret_key"
  skip_credentials_validation = true
  skip_metadata_api_check     = true
  skip_requesting_account_id  = true

  endpoints {
    apigateway     = "http://localhost:4567"
    cloudformation = "http://localhost:4581"
    cloudwatch     = "http://localhost:4582"
    dynamodb       = "http://localhost:4569"
    es             = "http://localhost:4578"
    firehose       = "http://localhost:4573"
    iam            = "http://localhost:4593"
    kinesis        = "http://localhost:4568"
    lambda         = "http://localhost:4574"
    route53        = "http://localhost:4580"
    redshift       = "http://localhost:4577"
    s3             = "http://localhost:4572"
    secretsmanager = "http://localhost:4584"
    ses            = "http://localhost:4579"
    sns            = "http://localhost:4575"
    sqs            = "http://localhost:4576"
    ssm            = "http://localhost:4583"
    stepfunctions  = "http://localhost:4585"
    sts            = "http://localhost:4592"
    ec2            = "http://localhost:4597"
  }
}

data "aws_security_group" "default" {
  name   = "default"
  vpc_id = module.vpc.vpc_id
}

module "vpc" {
  source = "../../"

  name = "vpc-terratest"

  cidr = "10.120.0.0/16"

  azs                     = ["eu-west-1a", "eu-west-1c"]
  compute_public_subnets  = ["10.120.3.0/24", "10.120.4.0/24"]
  compute_private_subnets = ["10.120.0.0/24", "10.120.1.0/24"]
  lb_subnets              = ["10.120.5.0/24", "10.120.6.0/24"]
  database_subnets        = ["10.120.7.0/24", "10.120.8.0/24"]

  create_database_subnet_group = false
  enable_nat_gateway           = true
  single_nat_gateway           = true

  create_database_subnet_route_table = true

  tags = {
    Owner       = "user"
    Environment = "dev"
  }
}
```

The module is
https://github.com/youse-seguradora/terraform-aws-vpc

## Command used to start LocalStack

localstack start

When I run in mey own aws account terraform apply and terraform destroy works fine.

Can you guys help?

Thanks



Discussion:

Resolving patch:
diff --git a/localstack/services/ec2/ec2_starter.py b/localstack/services/ec2/ec2_starter.py
--- a/localstack/services/ec2/ec2_starter.py
+++ b/localstack/services/ec2/ec2_starter.py
@@ -19,8 +19,16 @@ def revoke_security_group_egress(*args, **kwargs):
 
         return revoke_security_group_egress
 
+    def patch_delete_nat_gateway(backend):
+        def delete_nat_gateway(nat_gateway_id):
+            gateway = backend.nat_gateways.get(nat_gateway_id)
+            if gateway:
+                gateway.state = 'deleted'
+        return delete_nat_gateway
+
     for region, backend in ec2_models.ec2_backends.items():
         backend.revoke_security_group_egress = patch_revoke_security_group_egress(backend)
+        backend.delete_nat_gateway = patch_delete_nat_gateway(backend)
 
     # TODO Implement Reserved Instance backend
     # https://github.com/localstack/localstack/issues/2435
diff --git a/localstack/services/s3/s3_listener.py b/localstack/services/s3/s3_listener.py
--- a/localstack/services/s3/s3_listener.py
+++ b/localstack/services/s3/s3_listener.py
@@ -479,7 +479,6 @@ def fix_metadata_key_underscores(request_headers={}, response=None):
 def fix_creation_date(method, path, response):
     if method != 'GET' or path != '/':
         return
-
     response._content = re.sub(r'([0-9])</CreationDate>', r'\1Z</CreationDate>', to_str(response._content))
 
 
@@ -1130,7 +1129,7 @@ def return_response(self, method, path, data, headers, response, request_handler
                 # Pass on the 404 as usual
                 pass
 
-        if response:
+        if response is not None:
             reset_content_length = False
             # append CORS headers and other annotations/patches to response
             append_cors_headers(bucket_name, request_method=method, request_headers=headers, response=response)


Test patch: