Search code examples
terraformterraform-provider-aws

terraform import aws_vpc.main vpc-0ea21234


When I import an existing vpc into terraform I get the following error when running my terraform script.

Error: error deleting EC2 VPC (vpc-0ea21234): DependencyViolation: The vpc 'vpc-0ea21234' has dependencies and cannot be deleted. status code: 400, request id: 4630706a-5378-4e72-a3df-b58c8c7fd09b

Why is it trying to delete the VPC? How can I make it use the VPC? I'll post the main file I used to make the import and the import command below.

import command (this succeeds)

terraform import aws_vpc.main vpc-0ea21234

main file

provider "aws" {
  region = "us-gov-west-1"
  profile = "int-pipe"
}

# terraform import aws_vpc.main vpc-0ea21234
resource "aws_vpc" "main" {
  name                 = "cred-int-pipeline-vpc"
  cidr                 = "10.25.0.0/25"
}

# terraform import aws_subnet.main subnet-030de2345
resource "aws_subnet" "main" {
  vpc_id = "vpc-0ea21234"
  name = "cred-int-pipeline-subnet-d-az2"
  cidr = "10.25.0.96/27"
}

Solution

  • You probably have differences between what you have in your terraform configuration file and the resource you imported. Run terraform plan, it will show you exactly what the differences are and the reason why it must be deleted/re-created.

    After that, either manually change the resource in AWS or in your configuration file, if both existing resource and file match configuration, than delete and re-create won't be triggered.