How can I list Terraform resources with their IDs
Asked Answered
J

3

7

I want to move some Terraform resources from one repository to another. I reckon the simplest way to do that would be terraform import in one repository and terraform rm in the other.

With terraform state list I can get a list of resources, but without their ID. I thought I can combine that output with terraform state show but it's very slow and I have to parse out the ID from the HCL. Unfortunately there is no JSON output.

With terraform show -json I can get the whole state in JSON format, but there can be a lot of nesting of modules, so parsing that wouldn't be trivial either.

Are there any other options?

Edit 1:

terraform show -json for the relatively small repository I am working on gives me 2000 lines (too much to be sure of the format) starting with:

{
    "format_version": "0.1",
    "terraform_version": "0.14.9",
    "values": {
        "root_module": {
            "child_modules": [
                {
                    "address": "module.cloudtrail",
                    "child_modules": [
                        {
                            "address": "module.cloudtrail.module.cloudtrail_bucket",
                            "resources": [
                                {

I think there is at least one more level of nesting of child_modules. I think resources can be on every level. It's possible to get the list from that, but I don't think it's trivial.

Japonica answered 20/5, 2021 at 1:0 Comment(4)
Can you shown an example of the output from terraform show -json to get an idea why its non-trival?Creighton
You could use jq to process the json and get the values you require.Creighton
I started off with jq and then I saw that I don't get a flat list, but all this nesting.Japonica
You have to provide full example json and jq command you tried, and what you got and what do you want to get.Creighton
J
5

I just realised that I kind of get what I want every time I do a terraform plan. But I don't want the plan, so I can do terraform refresh | sed s'|: Refreshing state... \[id=| |' | sed s'|\]$||'. It's a bit slow, but I get a list of resources with their ID.

I can even add sed s'|^|terraform import |' > import.sh and import all the resources (or a part of them after an edit) with bash import.sh

Japonica answered 20/5, 2021 at 2:33 Comment(1)
Building on your solution, I came up with the following: terraform refresh -no-color | sed -E s'|: Refreshing state... \[id=(.*)\]| \1|' | sed s'|^|terraform import |' > import.sh Also, if you use a -var-file when applying, don't forget to use it on the terraform refresh - otherwise it will appear to hang as it sits there waiting for input!Lesleylesli
O
0

I write an open source script to do exactly that.

First terraform state pull and terraform state show each item and abstract the id then print out

https://github.com/amazingandyyy/terraform-utils/blob/main/terraform-state-list-ids.sh

Oarsman answered 1/4, 2024 at 3:0 Comment(0)
R
0

List all Terraform resources from this root module:

$ terraform show -json | jq -c '.values.root_module.resources[] | .address + " " + .values.id' -r

data.aws_iam_policy_document.ecr_policy 3636726594
data.aws_iam_policy_document.server 2362652690
aws_ecr_repository.server backend
aws_ecr_repository.ui frontend
Ribal answered 25/9, 2024 at 11:36 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.