Home > Software engineering >  Modularised terraform code wants to destroy and recreate an S3 bucket
Modularised terraform code wants to destroy and recreate an S3 bucket

Time:10-26

This is a simplified example of my problem, for the sake of understanding.

My team used to manage an S3 bucket through the module1, which invoked module2, which contained the code of the bucket.

production_account/ --(invokes)--> module1/ --(invokes)--> module2/s3.tf

But then they decided to refactor the code, and directly invoke module2.

production_account/ --(invokes)--> module2/s3.tf

As a result, when I terraform plan now I get:

module.module1.module.module2.aws_s3_bucket.bucket will be destroyed { 
...
}

module.module2.aws_s3_bucket.bucket will be created { 
...
}

In short, terraform wants to recreate the s3 bucket. The problem with this is that all data will be lost.

Is there a way to import the existing bucket to the state and to manage it through module 2?

terraform import module.module1.module.module2.aws_s3_bucket.bucket

solves the first half of my question.

CodePudding user response:

Yes. Use terraform state mv not import.

terraform state mv module.module1.module2.aws_s3_bucket.bucket module.module2.aws_s3_bucket.bucket
  • Related