Terraform v0.11.5
$ terraform state rm foo.bar
1 items removed.
Item removal successful.
Terraform should give me an error saying the resource does not exist.
Terraform says it removed 1 item successfully.
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.
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.
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
Seen this also with Terraform v0.11.10
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
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.
Most helpful comment
You can also replicate this by trying to remove the same item over and over again:
This also becomes a problem when accidentally mistyping a resource name that doesn't exist, and not realising: