0
votes

This may be related to Terraform plan wants to destroy imported RDS resource, although in my case, terraform wants to create an imported resource.

I have the following directory structure

aws/spot-fleets/jenkins/main.tf,variables.tf
aws/vpc/sandbox.tf,variables.tf

I have in aws/vpc/sandbox.tf

provider "aws" {
  region = var.region
}

terraform {
  backend "s3" {
    bucket = "terraform-remote-states"
    key    = "vpc/terraform.tfstate"
    region = "us-east-1"
  }
}

resource "aws_vpc" "sandbox_vpc" {
  assign_generated_ipv6_cidr_block = var.assign_generated_ipv6_cidr_block
  cidr_block = var.cidr_block["sandbox"]
#  default_network_acl_id = var.default_network_acl_id["sandbox"]
#  default_route_table_id = var. default_route_table_id["sandbox"]
#  default_security_group_id = var.default_security_group_id["sandbox"]
#  dhcp_options_id = var.dhcp_options_id["sandbox"]
  enable_classiclink = var.enable_classiclink
  enable_classiclink_dns_support = var.enable_classiclink_dns_support
  enable_dns_hostnames = var.enable_dns_hostnames
  enable_dns_support = var.enable_dns_support
  instance_tenancy = var.instance_tenancy
#  ipv6_association_id = var.ipv6_association_id
#  ipv6_cidr_block = var.ipv6_cidr_block["sandbox"]
#  main_route_table_id = var.main_route_table_id["sandbox"]
#  owner_id = var.owner_id["sandbox"]
  tags = {
    Environment = var.tag_environment["sandbox"]
    Name = var.tag_name["sandbox"]
    Product = var.tag_product
  }
}

output "sandbox_vpc_id" {
  value = aws_vpc.sandbox_vpc.id
}

In aws/vpc I then do a...

$ terraform import aws_vpc.sandbox_vpc vpc-1234

and then this, so I know the import worked!

$ terraform show
# aws_vpc.sandbox_vpc:
resource "aws_vpc" "sandbox_vpc" {
    arn                              = "arn:...."
    assign_generated_ipv6_cidr_block = false
    cidr_block                       = "10.x.x.x/16"
    default_network_acl_id           = "acl-1234"
    default_route_table_id           = "rtb-1234"
    default_security_group_id        = "sg-1234"
    dhcp_options_id                  = "dopt-1234"
    enable_classiclink               = false
    enable_classiclink_dns_support   = false
    enable_dns_hostnames             = true
    enable_dns_support               = true
    id                               = "vpc-1234"
    instance_tenancy                 = "default"
    main_route_table_id              = "rtb-1234"
    owner_id                         = "123456789"
    tags                             = {
        "Environment" = "sandbox"
        "Name"        = "Sandbox VPC"
        "Product"     = "Company"
    }
}

Now in my aws/spot-fleets/jenkins/main.tf, I have

provider "aws" {
  region = var.region
}

terraform {
  backend "s3" {
    bucket = "terraform-remote-states"
    key    = "jenkins/terraform.tfstate"
    region = "us-east-1"
  }
}

module "vpc" {
  source     = "../../vpc"
}

resource "aws_spot_fleet_request" "jenkins_fleet" {
  // some key/value pairs
}

I then do a terraform get, which gets the vpc state right? But when I do this in the ``aws/spot-fleets/jenkins` dir

$ terraform plan
# aws_spot_fleet_request.jenkins_fleet will be created
+ resource "aws_spot_fleet_request" "jenkins_fleet" {
  // some key/value pairs
}

# module.vpc.aws_vpc.sandbox_vpc will be created
+ resource "aws_vpc" "sandbox_vpc" {
  // some key/value pairs
}

Why is it trying to create the sandbox_vpc resource? How can I prevent terraform from creating it?

1

1 Answers

0
votes

Ah ok I found the answer indirectly in this Terraform Github issue. The command to run in my aws/spot-fleets/jenkins dir is

$ terraform import module.vpc.aws_vpc.sandbox_vpc vpc-1234