terraform 控制台 - 如何获取模块属性值

use*_*524 6 terraform

尝试使用新功能 terraform 控制台。我使用 tfstate chdir 到我的项目并运行“terraform 控制台”。我能够使用常规插值系统税来获取变量值、数据和资源。但是,模块很难破解,我无法正确使用它 - 我试过:module.name.attribute 但它不知道,网上使用控制台的例子不多:-/

感谢期待。

min*_*oyo 12

虽然文档中没有明确说明,但是好像我们不能在模块中引用局部变量,只能看到模块的输出。

下面是一个例子:

$ tree
.
??? foo
?   ??? bar.tf
??? main.tf
??? terraform.tfstate

1 directory, 3 files
Run Code Online (Sandbox Code Playgroud)

主文件

provider "null" {}

module "foo" {
  source = "./foo"
}
Run Code Online (Sandbox Code Playgroud)

foo/bar.tf

resource "null_resource" "bar" {}

output "bar_id" {
  value = "${null_resource.bar.id}"
}
Run Code Online (Sandbox Code Playgroud)

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": []
        }
    ]
}
Run Code Online (Sandbox Code Playgroud)

它可以被称为module.foo.bar_id

$ terraform console
> module.foo.bar_id
1810628649389143335
Run Code Online (Sandbox Code Playgroud)

其他不起作用:

$ 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
Run Code Online (Sandbox Code Playgroud)