I cannot destroy resources from my terraform 
Tags: terraform, aws, errors, study
Published: 2021-06-06 13:18:07

I am learning the well-known infrastructure as code from Hashicorp. You know it is terraform. 
I applied the IaC now I want to destroy it but I couldn't. It said the resources not found.

```bash
➜  02_01_variables git:(master) ✗ tf apply -destroy
aws_vpc.vpc1: Refreshing state... [id=vpc-00ff6b23017c33b36]
aws_internet_gateway.gateway1: Refreshing state... [id=igw-048ff50b04984c8c9]
aws_subnet.subnet1: Refreshing state... [id=subnet-092c7562b5217f94b]
aws_security_group.sg-nodejs-instance: Refreshing state... [id=sg-016d21995017adf82]
aws_route_table.route_table1: Refreshing state... [id=rtb-05e51e942674c90b0]
aws_instance.nodejs1: Refreshing state... [id=i-089b073c3bbbfeaa1]
aws_route_table_association.route-subnet1: Refreshing state... [id=rtbassoc-0f0ae78f78ee34ae2]
^C
Interrupt received.
Please wait for Terraform to exit or data loss may occur.
Gracefully shutting down...

^C
Two interrupts received. Exiting immediately. Note that data loss may have occurred.

╷
│ Error: operation canceled
│
│
╵
╷
│ Error: Plugin did not respond
│
│   with aws_route_table_association.route-subnet1,
│   on main.tf line 113, in resource "aws_route_table_association" "route-subnet1":
│  113: resource "aws_route_table_association" "route-subnet1" {
│
│ The plugin encountered an error, and failed to respond to the plugin.(*GRPCProvider).ReadResource call. The plugin logs may contain more details.

```

It looked like hang unexpectedly to me. I got nothing from the CLI. Oh, OK I did enable the log here.
```bash
export TF_LOG=TRACE
export TF_LOG_PATH=tf.log
```

OK, let me check the log quickly. 
```log
HTTP/1.1 400 Bad Request
...
2021-06-06T19:57:32.393+0700 [INFO]  provider.terraform-provider-aws_v3.44.0_x5: 2021/06/06 19:57:32 [DEBUG] [aws-sdk-go] <?xml version="1.0" encoding="UTF-8"?>
<Response><Errors><Error><Code>InvalidRouteTableID.NotFound</Code><Message>The routeTable ID 'rtb-05e51e942674c90b0' does not exist</Message></Error></Errors><RequestID>5b99646a-0e04-4f87-85aa-708e4a537852</RequestID></Response>: timestamp=2021-06-06T19:57:32.393+0700
```
**InvalidRouteTableID.NotFound**

I then checked my resource. 
```bash
➜  02_01_variables git:(master) ✗ tf state list
data.aws_ami.aws-linux
data.aws_availability_zones.available
aws_instance.nodejs1
aws_internet_gateway.gateway1
aws_route_table.route_table1
aws_route_table_association.route-subnet1
aws_security_group.sg-nodejs-instance
aws_subnet.subnet1
aws_vpc.vpc1
➜  02_01_variables git:(master) ✗ tf state show -state=terraform.tfstate aws_route_table.route_table1
# aws_route_table.route_table1:
resource "aws_route_table" "route_table1" {
    arn              = "arn:aws:ec2:ap-southeast-1:624836204311:route-table/rtb-05e51e942674c90b0"
    id               = "rtb-05e51e942674c90b0"
    owner_id         = "624836204311"
    propagating_vgws = []
    route            = [
        {
            carrier_gateway_id         = ""
            cidr_block                 = "0.0.0.0/0"
            destination_prefix_list_id = ""
            egress_only_gateway_id     = ""
            gateway_id                 = "igw-048ff50b04984c8c9"
            instance_id                = ""
            ipv6_cidr_block            = ""
            local_gateway_id           = ""
            nat_gateway_id             = ""
            network_interface_id       = ""
            transit_gateway_id         = ""
            vpc_endpoint_id            = ""
            vpc_peering_connection_id  = ""
        },
    ]
    tags             = {}
    tags_all         = {}
    vpc_id           = "vpc-00ff6b23017c33b36"
}
```
I checked it, and found it.
```bash
aws ec2 describe-route-tables --route-table-ids rtb-05e51e942674c90b0
```

Then I went away with my dog, dog walking. That helped.
I did the variable default to us-east-2 but I did apply them with ap-southeast-1. Let me try it.
```terraform
variable "region" {
  default = "us-east-2"
}
```

In the log, us-east-2 everywhere
```bash
DEBUG: Request ec2/DescribeAccountAttributes Details:
---[ REQUEST POST-SIGN ]-----------------------------
POST / HTTP/1.1
Host: ec2.us-east-2.amazonaws.com
...
2021-06-06T19:57:16.084+0700 [WARN]  Provider "provider[\"registry.terraform.io/hashicorp/aws\"]" produced an unexpected new value for data.aws_availability_zones.available.
      - .zone_ids[0]: was cty.StringVal("apse1-az1"), but now cty.StringVal("use2-az1")
      - .zone_ids[1]: was cty.StringVal("apse1-az2"), but now cty.StringVal("use2-az2")
      - .zone_ids[2]: was cty.StringVal("apse1-az3"), but now cty.StringVal("use2-az3")
      - .group_names: planned set element cty.StringVal("ap-southeast-1") does not correlate with any element in actual
      - .id: was cty.StringVal("ap-southeast-1"), but now cty.StringVal("us-east-2")
      - .names[0]: was cty.StringVal("ap-southeast-1a"), but now cty.StringVal("us-east-2a")
      - .names[1]: was cty.StringVal("ap-southeast-1b"), but now cty.StringVal("us-east-2b")
      - .names[2]: was cty.StringVal("ap-southeast-1c"), but now cty.StringVal("us-east-2c")
...
```
One that obviously seen is that `[WARN]  Provider "provider[\"registry.terraform.io/hashicorp/aws\"]" produced an unexpected new value for data`

and this one.
```bash
2021-06-06T19:57:09.758+0700 [DEBUG] ReferenceTransformer: "provider[\"registry.terraform.io/hashicorp/aws\"]" references: [var.region]
```

So I have had to supply the `-var region=ap-southeast-1` to the cli. `tf apply -destroy -var region=ap-southeast-1`  and all gone.