Maria Rona
Maria Rona

Reputation: 179

Generate terraform import commands from a terraform state file

I have a bit of a dummy question. First of all a bit of context:

I am using terraform cloud and have two workspaces called mydata_old and mydata_new. My goal is to do a partial workspace migration from mydata_old to mydata_new meaning that I want some of the configuration to now only exist to mydata_new and get removed from mydata_old. Is there any way just by looking at a state file example here to know exactly which import commands you have to execute?

Upvotes: 1

Views: 595

Answers (2)

LoganMzz
LoganMzz

Reputation: 1623

All resources have an id attribute in their state.

This id is mostly the second argument (after resource address) which is requested by Terraform import command.

As it may vary from one resource type to another, it may not be a general case. But a good starting point as you might be able to derive import id from state one.

Upvotes: 0

Helder Sepulveda
Helder Sepulveda

Reputation: 17594

Just looking at the old state is not complete information to know exactly which import command to execute... but it can help you build it, you need to know the Terraform resources in the new workspace.

Let's use as an example that state file example you provided:
https://github.com/mdb/terraform-example/blob/master/terraform/terraform.tfstate

One of the resources there is:

...
                "aws_s3_bucket_object.css_file": {
                    "type": "aws_s3_bucket_object",
                    "depends_on": [
                        "aws_s3_bucket.site"
                    ],
                    "primary": {
                        "id": "assets/stylesheets/application.css",
                        "attributes": {
                            "bucket": "mikeball.me",
                            "cache_control": "",
                            "content_disposition": "",
                            "content_encoding": "",
                            "content_language": "",
                            "content_type": "text/css",
                            "etag": "cbb4dfe4ff59c374f4d131f6f043e27e",
                            "id": "assets/stylesheets/application.css",
                            "key": "assets/stylesheets/application.css",
                            "kms_key_id": "",
                            "source": "../dist/assets/stylesheets/application.css",
                            "version_id": ""
                        }
                    }
                },
...

and let's assume your new terraform code for that resource is something like:

resource "aws_s3_bucket_object" "css" {
  bucket = "your_bucket_name"
  key    = "assets/stylesheets/application.css"
  source = "../dist/assets/stylesheets/application.css"
  etag = filemd5("../dist/assets/stylesheets/application.css")
}

According to the documentation we can import using the S3 url...
https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/s3_bucket_object#import

here is how that import command will look like

terraform import aws_s3_bucket_object.css s3://your_bucket_name/assets/stylesheets/application.css

Upvotes: 0

Related Questions