Terraform Resource没有变量

时间:2019-07-11 02:10:04

标签: terraform

运行Terraform 0.11.7并收到以下错误:

module.frontend_cfg.var.web_acl: Resource 'data.terraform_remote_state.waf' does not have attribute 'waf_nonprod_id' for variable 'data.terraform_remote_state.waf.waf_nonprod_id'

下面是terraform文件:

module "frontend_cfg"
{
    source        = "../../../../modules/s3_fe/developers"
    region        = "us-east-1"
    dev_shortname = "cfg"
    web_acl       = "${data.terraform_remote_state.waf.waf_nonprod_id}"
}

data "terraform_remote_state" "waf" {
  backend = "local"
  config = {
    name = "../../../global/waf/terraform.tfstate"
  }
}

创建上面引用的tfstate文件的文件如下。该文件没有任何问题。

resource "aws_waf_web_acl" "waf_fe_nonprod"
{
    name        = "fe_nonprod_waf"
    metric_name = "fenonprodwaf"

    default_action
    {
        type = "ALLOW"
    }
}

output waf_nonprod_id
{
    value = "${aws_waf_web_acl.waf_fe_nonprod.id}"
}

我将保留cloudfront文件的完整输出,但是,以下内容涵盖了文本:

resource "aws_cloudfront_distribution" "fe_distribution"
{
    web_acl_id          = "${var.web_acl}"
}

如果我将waf ID的ID放入web_acl变量中,则可以正常工作,因此我怀疑问题与我调用数据的方式有关。不过,这似乎与文档相匹配。

1 个答案:

答案 0 :(得分:0)

在terraform_remote_state中使用路径代替名称, https://www.terraform.io/docs/backends/types/local.html

data "terraform_remote_state" "waf" {
  backend = "local"
  config = {
    path = "../../../global/waf/terraform.tfstate"
  }
}

data "terraform_remote_state" "waf" {
  backend = "local"
  config = {
    path = "${path.module}/../../../global/waf/terraform.tfstate"
  }
}

我使用terraform版本0.11.7和0.11.14对其进行了测试


如果将terraform升级到版本0.12.x,则使用remote_state输出的语法已更改。 因此,更改

web_acl       = "${data.terraform_remote_state.waf.waf_nonprod_id}"

web_acl       = data.terraform_remote_state.waf.outputs.waf_nonprod_id

web_acl       = "${data.terraform_remote_state.waf.outputs.waf_nonprod_id}"