How do you move existing resources into a module without destroying them?
AdvancedConceptModules
Answer
Wrap the resources in the module, then add moved blocks in the root mapping old addresses to new ones.
moved {
from = aws_s3_bucket.logs
to = module.logs.aws_s3_bucket.this
}
Plan must show moves and no replacements. For many resources or across state files, terraform state mv is the manual alternative.
More on Modules
What is a Terraform module and why use one?BeginnerHow do you call a module?BeginnerHow do inputs and outputs work with modules?BeginnerWhich module source types exist and where does `version` apply?IntermediateHow should you version and pin modules?IntermediateCan you use `count` and `for_each` on modules?Intermediate