terraform console - how to get module attribute value

10,640

Although it is not clearly stated in the document, it seems that we can not refer to local variables in the module and we can only see the output of the module.

The following is an example:

$ tree
.
├── foo
│   └── bar.tf
├── main.tf
└── terraform.tfstate

1 directory, 3 files

main.tf

provider "null" {}

module "foo" {
  source = "./foo"
}

foo/bar.tf

resource "null_resource" "bar" {}

output "bar_id" {
  value = "${null_resource.bar.id}"
}

terraform.tfstate

{
    "version": 3,
    "terraform_version": "0.8.4",
    "serial": 4,
    "lineage": "9e66cc40-5dfa-4c4e-929e-bc02fa7db57e",
    "modules": [
        {
            "path": [
                "root"
            ],
            "outputs": {},
            "resources": {},
            "depends_on": []
        },
        {
            "path": [
                "root",
                "foo"
            ],
            "outputs": {
                "bar_id": {
                    "sensitive": false,
                    "type": "string",
                    "value": "1810628649389143335"
                }
            },
            "resources": {
                "null_resource.bar": {
                    "type": "null_resource",
                    "depends_on": [],
                    "primary": {
                        "id": "1810628649389143335",
                        "attributes": {
                            "id": "1810628649389143335"
                        },
                        "meta": {},
                        "tainted": false
                    },
                    "deposed": [],
                    "provider": ""
                }
            },
            "depends_on": []
        },
        {
            "path": [
                "root",
                "hoge"
            ],
            "outputs": {},
            "resources": {},
            "depends_on": []
        }
    ]
}

It can be referred to as module.foo.bar_id:

$ terraform console
> module.foo.bar_id
1810628649389143335

Others don't work:

$ terraform console
> null_resource.bar.id
Resource 'null_resource.bar' not found for variable 'null_resource.bar.id'

> module.foo.null_resource.bar.id
Couldn't find output "null_resource.bar.id" for module var: module.foo.null_resource.bar.id
Share:
10,640

Related videos on Youtube

user1619524
Author by

user1619524

Updated on August 15, 2022

Comments

  • user1619524
    user1619524 over 1 year

    Trying to put terraform console, new feature, into use. I chdir to my project with tfstate and run 'terraform console'. I am able get variable values, data and resources using regular interpolatation systax. However, modules are hard to crack, i can't get to put it to correct use - I tried: module.name.attribute and it does not know, not many examples on using console on the web :-/

    Thanks in anticipation.