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

terraform state rm always displays success message as long as you give it a "valid" resource name #17800

Closed
kaofelix opened this issue Apr 6, 2018 · 5 comments · Fixed by #22300
Labels
breaking-change bug cli v0.11 Issues (primarily bugs) reported against v0.11 releases v0.12 Issues (primarily bugs) reported against v0.12 releases

Comments

@kaofelix
Copy link

kaofelix commented Apr 6, 2018

Terraform Version

Terraform v0.11.5

Terraform Command

$ terraform state rm foo.bar
1 items removed.
Item removal successful.

Expected Behavior

Terraform should give me an error saying the resource does not exist.

Actual Behavior

Terraform says it removed 1 item successfully.

Steps to Reproduce

In any folder, even one that does not contain state run terraform state rm random_type.random_name. If there's no . Terraform will complain:

$ terraform state rm foo
Error removing items from the state: Error parsing address 'foo': invalid resource address "foo"

but beyond that it always prints the success message.

@tdmalone
Copy link

tdmalone commented May 17, 2018

You can also replicate this by trying to remove the same item over and over again:

$ terraform state rm aws_eip.foo
1 items removed.
Item removal successful.
$ terraform state rm aws_eip.foo
1 items removed.
Item removal successful.
$ terraform state rm aws_eip.foo
1 items removed.
Item removal successful.

This also becomes a problem when accidentally mistyping a resource name that doesn't exist, and not realising:

$ terraform state rm aws_eip.my_serveer
1 items removed.
Item removal successful.

@pillarsdotnet
Copy link

pillarsdotnet commented Feb 15, 2019

Furthermore, terraform state rm reports success even when it fails. Here's an example:

+ terraform state rm aws_iam_role.tower
Item removal successful.
+ terraform import -no-color aws_iam_role.tower tower
aws_iam_role.tower: Import complete!
  Imported aws_iam_role (ID: tower)

Error: aws_iam_role.tower (import id: tower): Can't import aws_iam_role.tower, would collide with an existing resource.

Please remove or rename this resource before continuing.

The full TF_LOG=trace output is here

@marcelloromani
Copy link

Seen this also with Terraform v0.11.10

@jgpacker
Copy link
Contributor

The output was corrected. I'm using Terraform v0.12.2.
However, the exit code is zero, instead of one.

$ terraform state rm module.that.aws_ecs_task_definition.this_non_created_resource
Acquiring state lock. This may take a few moments...
No matching resource instances found.
Releasing state lock. This may take a few moments...
$ echo $?
0

Should the exit code be in the scope of this bug?
The subcommand terraform state show does exit with code one for the same non-existent resource.

$ terraform state show module.that.aws_ecs_task_definition.this_non_created_resource
No instance found for the given address!

This command requires that the address references one specific instance.
To view the available instances, use "terraform state list". Please modify 
the address to reference a specific instance.
$ echo $?
1

jgpacker pushed a commit to jgpacker/terraform that referenced this issue Aug 1, 2019
Commands `state rm` and `state mv` will now exit with code 1 when the
target resource is not found in the current state.
This is consistent with `terraform state show non_existent_resource`.

Fixes hashicorp#17800
@hashibot hashibot added v0.11 Issues (primarily bugs) reported against v0.11 releases v0.12 Issues (primarily bugs) reported against v0.12 releases breaking-change labels Aug 29, 2019
pselle pushed a commit that referenced this issue Jun 24, 2020
* command: adjust exit code of state rm and state mv

Commands `state rm` and `state mv` will now exit with code 1 when the
target resource is not found in the current state.
This is consistent with `terraform state show non_existent_resource`.

Fixes #17800
@ghost
Copy link

ghost commented Jul 25, 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 Jul 25, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
breaking-change bug cli v0.11 Issues (primarily bugs) reported against v0.11 releases v0.12 Issues (primarily bugs) reported against v0.12 releases
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants