Giter VIP home page Giter VIP logo

Comments (4)

okkez avatar okkez commented on June 1, 2024 1

Resolved my issue. Thanks!

from tfmigrate.

minamijoyo avatar minamijoyo commented on June 1, 2024

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.

okkez avatar okkez commented on June 1, 2024

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
...snipterraform 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 quotesterraform 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.

minamijoyo avatar minamijoyo commented on June 1, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.