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

Moving a module with "terraform state mv" loses "data." prefix for aws_caller_identity address #9996

Closed
nickrw opened this issue Nov 9, 2016 · 10 comments · Fixed by #10279
Closed
Assignees

Comments

@nickrw
Copy link

nickrw commented Nov 9, 2016

Terraform Version

Terraform v0.7.9

Affected Resource(s)

Please list the resources as a list, for example:

  • data.aws_caller_identity
  • command line "terraform state mv"

Terraform Configuration Files

I created a reproduction case with this simple layout

./sourcemod/test.tf

provider "aws" {
  region = "eu-west-1"
}
data "aws_caller_identity" "current" { }

./root.tf

module "sourcemod" {
  source = "./sourcemod"
}

Expected Behavior

With the above module layout, after running terraform apply you should be able to rename a module, e.g.

terraform apply
terraform state mv module.sourcemod module.destmod
terraform plan

Actual Behavior

Subsequent plan/apply operations result in

module.sourcemod.data.aws_caller_identity.current: Refreshing state...
module.destmod.aws_caller_identity.current: Refreshing state... (ID: 2016-11-09 14:37:57.229678945 +0000 UTC)
Error refreshing state: 1 error(s) occurred:

* aws_caller_identity.current: unknown resource type: aws_caller_identity

Examining the diff between the state and the backup file shows that "data." is being dropped from the resource address:

diff --git a/terraform.tfstate.1478702283..backup b/terraform.tfstate
index 5189272..c706ebd 100644
--- a/terraform.tfstate.1478702283..backup
+++ b/terraform.tfstate
@@ -1,7 +1,7 @@
 {
     "version": 3,
     "terraform_version": "0.7.9",
-    "serial": 0,
+    "serial": 1,
     "lineage": "ba66147c-24c6-42e0-89b5-762458202496",
     "modules": [
         {
@@ -15,11 +15,11 @@
         {
             "path": [
                 "root",
-                "sourcemod"
+                "destmod"
             ],
             "outputs": {},
             "resources": {
-                "data.aws_caller_identity.current": {
+                "aws_caller_identity.current": {
                     "type": "aws_caller_identity",
                     "depends_on": [],
                     "primary": {

Manually adding the missing "data." back to the address in the state file fixes the issue, and subsequent plan/apply operations succeed.

@apparentlymart
Copy link
Contributor

Hi @nickrw. Thanks for reporting this!

The state commands and data source code were being worked on concurrently, so my bet is that this was missed because the extra attribute we added to distinguish data resources from managed resources wasn't around when the state mv code was written.

In some quick spelunking in the code I wasn't able to track it down exactly, but this problem suggests that somewhere inside the module-moving code we're copying and rewriting ResourceAddress objects and failing to preserve the Mode attribute, which is what records whether a resource is a managed or a data resource.

@apparentlymart
Copy link
Contributor

Aha... I think I found it:

addrCopy := *addr

I don't have a Go compiler handy to test right now but I expect adding an additional line to copy the Mode attribute here will fix it.

@jbardin jbardin self-assigned this Nov 21, 2016
jbardin added a commit that referenced this issue Nov 22, 2016
Set proper Mode when moving a data source in state
@hamstah
Copy link

hamstah commented Apr 18, 2017

Hi,
I've just had this happen with terraform 0.9.2 with terraform state mv

I used terraform state mv for all the resources in a module (I renamed the module and didn't want to delete/recreate all the resources in it).

When running plan I get

Error refreshing state: 1 error(s) occurred:
* module.client_account.aws_caller_identity.current: aws_caller_identity.current: unknown resource type: aws_caller_identity

I'm using the new state backend

Here is the relevant part of terraform state pull

           "resources": {
                "aws_caller_identity.current": {
                    "type": "aws_caller_identity",
                    "depends_on": [],
                    "primary": {
                        "id": "2017-03-31 13:51:02.764582214 +0000 UTC",
                        "attributes": {
                            "account_id": "xxxxx",
                            "id": "2017-03-31 13:51:02.764582214 +0000 UTC"
                        },
                        "meta": {},
                        "tainted": false
                    },
                    "deposed": [],
                    "provider": ""
                },
...

The data has been dropped from the resource as @nickrw reported

Using terraform state show works fine

terraform state show module.client_account.aws_caller_identity.current
id         = 2017-03-31 13:51:02.764582214 +0000 UTC
account_id = xxxxx

@hamstah
Copy link

hamstah commented Apr 18, 2017

Fixed the issue with

  • terraform state pull > state.json
  • Edit the file and add the data. prefix to the resource key
  • terraform state push state.json

@exolab
Copy link

exolab commented Oct 12, 2017

Having the exact same problem with aws_availability_zones and did exactly what hamstah did to fix it.

@themalkolm
Copy link

Same with aws_iam_policy_document

@frimik
Copy link

frimik commented Mar 5, 2018

Happened with every single data source in a state mv ... plenty of prefixing had to be done.

+ provider.aws v1.10.0```

@apparentlymart
Copy link
Contributor

Hi all,

Given that Terraform has changed a lot since this issue was opened and addressed, it's likely that new problems have different causes even if the symptoms look similar. For those of you who are seeing what appears to be a regression of this issue, it'd be great if you could open a fresh issue and fill out the requested information in the issue template so we can have the best chance of understanding what's going on.

Thanks!

@2rs2ts
Copy link
Contributor

2rs2ts commented Oct 17, 2018

@apparentlymart new issue: #18978

@ghost
Copy link

ghost commented Apr 1, 2020

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 Apr 1, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

8 participants