Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot move a resource to a nested module #22278

Closed
querry43 opened this issue Jul 31, 2019 · 2 comments · Fixed by #22299
Closed

Cannot move a resource to a nested module #22278

querry43 opened this issue Jul 31, 2019 · 2 comments · Fixed by #22299

Comments

@querry43
Copy link

I am unable to move one resource to another place as part of a refactor. This could be related to modules and submodules along with lists of resources.

Terraform plan shows that one resource will be deleted and another one like it will be created, however the state move fails because the new one does not exist yet. I have successfully renamed things in the past without this issue.

terraform plan
...
  # module.backend_service.aws_route53_record.dns[0] will be destroyed
  - resource "aws_route53_record" "dns" {
      - fqdn    = "my.dns" -> null
      - id      = "AAAAAAAAAAAAAA_backend_A" -> null
      - name    = "backend" -> null
      - records = [] -> null
      - ttl     = 0 -> null
      - type    = "A" -> null
      - zone_id = "AAAAAAAAAAAAAA" -> null

      - alias {
          - evaluate_target_health = true -> null
          - name                   = "my.load.balancer" -> null
          - zone_id                = "BBBBBBBBBBBBB" -> null
        }
    }

  # module.backend_service.module.dns.aws_route53_record.ipv4[0] will be created
  + resource "aws_route53_record" "ipv4" {
      + allow_overwrite = (known after apply)
      + fqdn            = (known after apply)
      + id              = (known after apply)
      + name            = "backend"
      + type            = "A"
      + zone_id         = "AAAAAAAAAAAAAA"

      + alias {
          + evaluate_target_health = true
          + name                   = "my.load.balancer"
          + zone_id                = "BBBBBBBBBBBBB"
        }
    }

terraform state mv module.backend_service.aws_route53_record.dns[0] module.backend_service.module.dns.aws_route53_record.ipv4[0]

Error: Invalid target address

Cannot move to module.backend_service.module.dns.aws_route53_record.ipv4[0]:
module.backend_service.module.dns does not exist in the current state.

Terraform Version

Terraform v0.12.5
+ provider.aws v2.21.1
+ provider.fastly v0.8.1
+ provider.template v2.1.2

Terraform Configuration Files

OLD modules/container/dns.tf

# should should be using modules/lb_dns_record
# this is an intrusive change which will be made later
resource aws_route53_record dns {
  count = var.create_load_balancer ? length(var.dns_zones.external_zones) : 0

  zone_id = var.dns_zones.external_zones[count.index].zone_id
  name    = var.name
  type    = "A"

  alias {
    name                   = aws_lb.lb[0].dns_name
    zone_id                = aws_lb.lb[0].zone_id
    evaluate_target_health = true
  }
}

resource aws_route53_record ipv6_dns {
  count = var.create_load_balancer ? length(var.dns_zones.external_zones) : 0

  zone_id = var.dns_zones.external_zones[count.index].zone_id
  name    = var.name
  type    = "AAAA"

  alias {
    name                   = aws_lb.lb[0].dns_name
    zone_id                = aws_lb.lb[0].zone_id
    evaluate_target_health = true
  }
}

NEW modules/container/dns.tf

module dns {
  source      = "./modules/lb_dns_record"
  name        = var.name
  zones       = var.dns_zones.external_zones
  lb_dns_name = aws_lb.lb[0].dns_name
  lb_zone_id  = aws_lb.lb[0].zone_id
  create      = var.create_load_balancer
}

modules/lb_dns_record/main.tf

resource aws_route53_record ipv4 {
  count   = var.create ? length(var.zones) : 0
  zone_id = var.zones[count.index].zone_id
  name    = var.name
  type    = "A"

  alias {
    name                   = var.lb_dns_name
    zone_id                = var.lb_zone_id
    evaluate_target_health = true
  }
}

resource aws_route53_record ipv6 {
  count   = var.create ? length(var.zones) : 0
  zone_id = var.zones[count.index].zone_id
  name    = var.name
  type    = "AAAA"

  alias {
    name                   = var.lb_dns_name
    zone_id                = var.lb_zone_id
    evaluate_target_health = true
  }
}

Expected Behavior

The state mv should be successful so that the resource does not need to be destroyed and recreated.

Actual Behavior

The state mv command fails with module.backend_service.module.dns does not exist in the current state.

Steps to Reproduce

  1. terraform init
  2. terraform plan
  3. terraform state mv module.backend_service.aws_route53_record.dns[0] module.backend_service.module.dns.aws_route53_record.ipv4[0]
@earchibald-lv
Copy link

Possibly related to #21346. It's been months and no-one from Hashicorp has even commented.

@ghost
Copy link

ghost commented Sep 2, 2019

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.

If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@ghost ghost locked and limited conversation to collaborators Sep 2, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants