Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

terraform console - how to get module attribute value

Tags:

terraform

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.

like image 434
user1619524 Avatar asked Jan 30 '17 16:01

user1619524


People also ask

How do I find the output value of a Terraform?

Terraform Output Command Output values are stored in the state Terraform file. Since we have successfully applied our plan, we can now access these output values at will. We can leverage the terraform output command for this purpose. To get the raw value without quotes, use the -raw flag.

How do you access the output variable in Terraform?

Using a variableA variable's value can be accessed from within the terraform module block by using var. <variable_name> . Below we have an example demonstrating this. The variable's value can only be accessed in an expression within the modules where it was declared.

How would you output returned values from a child module?

Accessing Child Module Outputs In a parent module, outputs of child modules are available in expressions as module. <MODULE NAME>. <OUTPUT NAME> . For example, if a child module named web_server declared an output named instance_ip_addr , you could access that value as module.

How do you refer a module in Terraform?

Modules on the public Terraform Registry can be referenced using a registry source address of the form <NAMESPACE>/<NAME>/<PROVIDER> , with each module's information page on the registry site including the exact address to use. The above example will use the Consul module for AWS from the public registry.


1 Answers

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
like image 80
minamijoyo Avatar answered Nov 01 '22 08:11

minamijoyo