Terraform Ambiguous move statement错误及资源类型不匹配问题求助
Terraform状态迁移:解决Ambiguous move与资源类型不匹配问题
问题背景
重构Terraform仓库后,多数资源改用新模块创建。执行terraform plan时工具会删除并重建资源,但我无法接受这种操作,因此通过moved块迁移状态中的资源(无直接访问状态文件权限)。
最初编写的moved块如下:
moved { from = module.my_zones_ecr.aws_route53_zone.my_zone[0] to = module.custom_eu_west_1.aws_route53_zone.my_zone[0] } moved { from = module.my_zones_s3.aws_route53_zone.my_zone[0] to = module.custom_eu_west_1.aws_route53_zone.my_zone[0] } moved { from = module.my_zones_splunk_dev[0].aws_route53_zone.my_zone[0] to = module.custom_eu_west_1.aws_route53_zone.my_zone[0] }
第一个错误:Ambiguous move statements
多个moved块的to地址重复,触发以下错误:
Error: Ambiguous move statements on moved.tf line 771: moved { A statement at moved.tf:756,1 declared that module.my_zones_ecr.aws_route53_zone.my_zone[0] moved to module.custom_eu_west_1.aws_route53_zone.my_zone[0], but this statement instead declares that module.my_zones_splunk_dev[0].aws_route53_zone.my_zone[0] moved there. Each resource instance can have moved from only one source instance.
尝试解决后遇到资源类型不匹配
为避免地址冲突,给每个目标指定唯一地址,但出现资源类型不匹配错误:
moved { from = module.my_zones_ecr.aws_route53_zone.my_zone[0] to = module.regional_eu_west_1.ecr_aws_route53_zone.hosted_zone[0] } moved { from = module.my_zones_s3.aws_route53_zone.my_zone[0] to = module.regional_eu_west_1.s3_aws_route53_zone.hosted_zone[0] }
错误信息:
Error: Resource type mismatch This statement declares a move from module.hosted_zones_ecr.aws_route53_zone.hosted_zone[0] to module.regional_eu_west_1.ecr_aws_route53_zone.hosted_zone[0], which is a resource instance of a different type. Error: Resource type mismatch This statement declares a move from module.hosted_zones_s3.aws_route53_zone.hosted_zone[0] to module.regional_eu_west_1.s3_aws_route53_zone.hosted_zone[0], which is a resource instance of a different type.
解决方案
确保目标地址资源类型完全匹配
类型不匹配说明module.regional_eu_west_1.ecr_aws_route53_zone.hosted_zone[0]的资源类型并非aws_route53_zone。检查新模块regional_eu_west_1中ecr_aws_route53_zone和s3_aws_route53_zone的定义,确认内部资源类型确实是aws_route53_zone,且资源名称、索引与代码一致。每个目标地址仅对应一个源资源
不能将多个源资源迁移到同一目标地址,这会导致Terraform无法确定状态映射关系。需调整新模块custom_eu_west_1或regional_eu_west_1的结构,为每个原资源提供独立的aws_route53_zone实例,保证每个源对应唯一的目标地址。验证状态地址正确性
若有权限,执行terraform state list确认源资源地址准确,同时检查新模块中目标资源的地址格式是否符合module.<模块名>.<资源类型>.<资源名>[<索引>]的规范,确保与实际定义一致。
内容的提问来源于stack exchange,提问作者parax
相关产品推荐
相关产品推荐

