3
votes

I'm learning terraform, and want to setup an AWS infrastructure using the tool.

We have 3 AWS environments, sandbox, staging, and production, and have existing infrastructure to support these environments. For example, we have 3 separate VPCs for each environment.

I want to use terraform import to import the states of these resources, based on the environment I'm trying to setup. So I essentially want to do this, though I know this is not syntactically correct, but you get the idea.

$ terraform import aws_vpc.my_vpc -var 'environment=sandbox'

I therefore have my module setup like this

vpc/main.tf
-----------
provider "aws" {
  region = "us-east-1"
}
resource "aws_vpc" "my_vpc" {
  cidr_block = ""
}

vpc/variables.tf
----------------
variable "environment" {
  type map = map(string)
  default {
    sandbox    = "vpc-1234"
    staging    = "vpc-2345"
    production = "vpc-3456"
  }
}

So this means I essentially want to do

$ terraform import aws_vpc.my_vpc vpc-1234

How can I achieve this?

2
I think the root cause here is that workspaces are not m=being mapped to environments. That may be the problem based on information provided. - Matt Schuchard

2 Answers

4
votes

I had the same issue and figured out that the order is important. This command works:

$ terraform import -var 'environment=sandbox' aws_vpc.my_vpc vpc-1234
1
votes

Just for information

The terraform import command is used to import existing infrastructure.

The command currently can only import one resource at a time. This means you can't yet point Terraform import to an entire collection of resources such as an AWS VPC and import all of it. This workflow will be improved in a future version of Terraform import usage.

The one you are passing these will be considered as input variables for sub-sequent modules, not the get value from Environment variables file but to set value for variables.

Input variables serve as parameters for a Terraform module, allowing aspects of the module to be customized without altering the module's own source code, and allowing modules to be shared between different configurations. configuration variables

Import syantax:

terraform import aws_instance.example i-abcd1234

This command locates the AWS instance with ID i-abcd1234 and attaches its existing settings, as described by the EC2 API, to the name aws_instance.example in the Terraform state.

So the way around to deal with import using variables can be System environment variables, As ec2 can be imported, so the example is based on importing instance using the variable.

instance.tf

provider "aws" {
  region              = "us-west-2"
  profile             = "test"
}

resource "aws_instance" "ec2" {
  ami                         = "ami-0f2176987ee50226e"
  instance_type               = "t2.micro"
  associate_public_ip_address = false
  subnet_id                   = "subnet-example"
  vpc_security_group_ids      = ["sg-example"]
  key_name                    = "mytest-ec2key"
  tags = {
    Name = "Test EC2 Instance"
  }
}

To pass variables to the above script you can System environment variable.

 export stage_instance=i-abcexample && terraform import aws_instance.ec2 $stage_instance

or you can also try

export stage_instance=abc && export prod_instance=abc

then try to import

terraform import aws_instance.ec2 $stage_instance

enter image description here