Destroy Default VPC

Hi all,
I want to destroy all default VPC for entire region, but i don’t want use lambda function or bash or python script. I would like to do it in terraform;
Maybe i will need a little module named destroy

aws_destroy “default-vpc” {
id = vpc-ieiehdkjkf
}

Hi, Terraform doesn’t work like that. and modules are not “functions”.

Terraform should have created the resource (VPC, in this case) to destroy it.
(or atleast it should be made to believe :wink: that it has created the resource)

*** You can always import an existing resource and then delete it.

As the “default vpc” is something that is “already present”, you should import it … using the “terraform import …” commands and then proceed to “terraform destroy” it.

I think you can start by checking the “terraform import …” syntax for the “vpc resource”.

HTH,
Shantanu Gadgil

1 Like

Hi @DRAKUN,

As @shantanugadgil noted, Terraform is designed to manage only objects that it created, so that you can use it in an account where some objects are managed by other software.

Because Terraform did not create the default VPCs it cannot destroy them. While you could use terraform import to tell Terraform to take ownership of the VPCs, in which case Terraform would then assume it is responsible for managing them moving foward, to do that just to delete them will be a lot of work compared to just a simple script using the AWS CLI or SDK, because that is not a use-case Terraform is designed to deal with. Specifically, you’d need to:

  • Write a resource "aws_vpc" block into a Terraform configuration with count set to zero.
  • Run terraform import to import each existing default VPC to an instance index for that resource.
  • Run terraform apply to have Terraform see that count = 0 and thus there should not be any instances of that resource, and thus plan to destroy them.

Step 2 here is not really any simpler than just scripting the AWS CLI, and so this approach doesn’t seem to have any benefit.

1 Like