Terraform AWS - moving state to another module
If your infrastructure grows and you find that certain resources should be moved to its own module because they need to be shared with others (or you made a mistake by putting them in the wrong module in the first place), you can move the state using CLI rather than recreating resources from scratch.
let’s say you have:
module "s3" {
source = "./modules/s3"
...
}
and inside you defined user with access policy:
resource "aws_iam_user" "portal" {...}
resource "aws_iam_user_policy" "portal" {...}
Use:
terraform state mv module.s3.aws_iam_user.portal module.iam
terraform state mv module.s3.aws_iam_user_policy.portal module.iam
After that you can move your resource definitions from s3
to iam
module. At the end, run terraform plan
- terraform shouldn’t detect any changes.
Documentation here.
Tweet