Comments (4)
Resolved my issue. Thanks!
from tfmigrate.
Hi @okkez, thank you for reporting this!
I can use * with terraform state rm command with quote like below.
Is it correct? Which terraform version are you using?
As I understand, the native terraform state rm doesn’t accept [*]
, but you can remove a list of resources by pointing to the list address.
https://developer.hashicorp.com/terraform/cli/commands/state/rm
Here is what I can see with the latest Terraform v1.4.6:
# terraform -v
Terraform v1.4.6
on linux_amd64
+ provider registry.terraform.io/hashicorp/aws v4.67.0
# cat main.tf
resource "aws_s3_bucket" "test" {
for_each = toset(["foo", "bar"])
bucket = each.key
}
# terraform state list
aws_s3_bucket.test["bar"]
aws_s3_bucket.test["foo"]
# terraform state rm 'aws_s3_bucket.test[*]'
╷
│ Error: Attribute name required
│
│ on line 1:
│ (source code not available)
│
│ Splat expressions ([*]) may not be used here.
╵
# terraform state rm aws_s3_bucket.test
Removed aws_s3_bucket.test["bar"]
Removed aws_s3_bucket.test["foo"]
Successfully removed 2 resource instance(s).
# terraform state list
Thus, you can write a migration file as follows:
migration "state" "test" {
actions = [
"rm aws_s3_bucket.test",
]
}
from tfmigrate.
Is it correct? Which terraform version are you using?
❯ terraform version
Terraform v1.4.2
on linux_amd64
Your version of Terraform is out of date! The latest version
is 1.4.6. You can update by downloading from https://www.terraform.io/downloads.html
As I understand, the native terraform state rm doesn’t accept [*], but you can remove a list of resources by pointing to the list address.
Actually, I use MySQL provider and Terraform modules. Sorry, I gave a non-real example in my previous comment.
So I create a simple example using aws_s3_bucket below:
resource "aws_s3_bucket" "test" {
for_each = toset(["*", "*.*", "abc"])
bucket = format("okkez-%s", sha1(each.value))
}
I create following resources:
❯ terraform apply
...snip
❯ terraform state list
aws_s3_bucket.test["*"]
aws_s3_bucket.test["*.*"]
aws_s3_bucket.test["abc"]
I can run terraform state rm
like below:
❯ terraform state rm 'aws_s3_bucket.test["*"]'
Removed aws_s3_bucket.test["*"]
Successfully removed 1 resource instance(s).
# no quotes
❯ terraform state rm aws_s3_bucket.test["*.*"]
zsh: no matches found: aws_s3_bucket.test[*.*]
❯ terraform state rm 'aws_s3_bucket.test["*.*"]'
Removed aws_s3_bucket.test["*.*"]
Successfully removed 1 resource instance(s).
I can also run terraform import
:
❯ envchain okkez-test terraform import 'aws_s3_bucket.test["*"]' okkez-df58248c414f342c81e056b40bee12d17a08bf61
aws_s3_bucket.test["*"]: Importing from ID "okkez-df58248c414f342c81e056b40bee12d17a08bf61"...
aws_s3_bucket.test["*"]: Import prepared!
Prepared aws_s3_bucket for import
aws_s3_bucket.test["*"]: Refreshing state... [id=okkez-df58248c414f342c81e056b40bee12d17a08bf61]
Import successful!
The resources that were imported are shown above. These resources are now in
your Terraform state and will henceforth be managed by Terraform.
❯ envchain okkez-test terraform import 'aws_s3_bucket.test["*.*"]' okkez-4c9e993a0d9a0518e2c981f46ff2341e8a36e47a
aws_s3_bucket.test["*.*"]: Importing from ID "okkez-4c9e993a0d9a0518e2c981f46ff2341e8a36e47a"...
aws_s3_bucket.test["*.*"]: Import prepared!
Prepared aws_s3_bucket for import
aws_s3_bucket.test["*.*"]: Refreshing state... [id=okkez-4c9e993a0d9a0518e2c981f46ff2341e8a36e47a]
Import successful!
The resources that were imported are shown above. These resources are now in
your Terraform state and will henceforth be managed by Terraform.
This is an invalid HCL file for tfmigrate. How to write the configuration in this situation?
migration "state" "rm" {
actions = [
"rm aws_s3_bucket.test["*"]",
"rm aws_s3_bucket.test["*.*"]",
]
}
from tfmigrate.
You can escape it as follows. Give it a try! It works fine for me.
migration "state" "rm" {
actions = [
"rm 'aws_s3_bucket.test[\"*\"]'",
"rm 'aws_s3_bucket.test[\"*.*\"]'",
]
}
from tfmigrate.
Related Issues (20)
- Migrating states from codebases with different terraform version HOT 3
- tfmigrate reports success despite backend not properly configured HOT 1
- Feature request: Display of state plan for to_dir in multi-state HOT 3
- tfmigrate reports migration plan success when Terraform plan command returns an error HOT 2
- Feature request: add an option to skip `terraform plan`-ing HOT 4
- Feature request: support replace-provider HOT 5
- Change tfmigrate-storage license from MPL2 to MIT HOT 3
- Feature request: support skip_plan in state migration HOT 2
- Feature request: support actions spanning multiple HCL files targeting the same projects HOT 4
- multi_state xmv attempts to migrate data sources? HOT 4
- Possible to optimise multi_state_mv execution time?
- Possible to optimise multi_state_mv execution time? HOT 1
- OpenTofu support HOT 3
- Add terraform >=1.1 moved resource documentation? HOT 1
- [bug] Does not work when working with terragrunt & dynamic backend config
- Error executing ftmigrate on MacOS 14.0 (ARM) HOT 1
- split resources with dependencies. HOT 1
- Bug when you have module that have the same resource names
- High vulnerability in golang library HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from tfmigrate.