...

/

Moving a Resource from One Project to Another

Moving a Resource from One Project to Another

Learn why and how to move resources from one Terraform project to another.

Moving a resource

When Terraform projects become large or you want to refactor them, you may need to move a resource from one Terraform project to another. To do this, you cannot simply delete the resource from the Terraform project and add it to the other. If you do, then Terraform will physically delete the resource and recreate it again in the other project. This is often not desirable as it will cause a loss of availability and/or data.

First project example

To solve this problem, we will learn how you can manipulate the Terraform state to move resources between projects:

Press + to interact
provider "aws" {
region = "us-east-2"
}
resource "aws_vpc" "my_vpc" {
cidr_block = "10.1.0.0/16"
tags = {
Name = "vpc"
}
}

Second project example

Next, we’ll create another project. This will be our second Terraform project where we are going to move the resource to:

Press + to interact
provider "aws" {
region = "us-east-2"
}
resource "aws_vpc" "main" {
cidr_block = "10.1.0.0/16"
tags = {
Name = "vpc"
}
}

Both of the projects are pretty similar. They both set up the AWS provider in the same way both define an AWS VPC with the same CIDR range (10.1.0.0/16), and both have a single name tag, vpc. The VPCs do have a different identifier though. In the first project, we use the identifier my_vpc while in the second project the identifier is main.

We ...