繁体   English   中英

调用数据/资源时如何使用terraform模块中的变量?

[英]How can I use variables in terraform module when call data/resources?

我得到了一个基本的 Terraform 代码,其中 main.tf 调用一个模块来创建 AWS 组织策略。

** 主.tf **

module "SCP-L2-RegionRestriction" {
  source        = "github.com/awsmodulecode/scps.git"
  scp_name      = "SCP-L2-RegionRestriction"
} 

** 模块 **

resource "aws_organizations_policy" "SCP-L2-RegionRestriction" {
  name    = var.scp_name
  content = data.aws_iam_policy_document."${var.scp.name}".json
}

** 变量.tf **

variable "scp_name" {
  description = "Policy name."
}

** 错误 **

╷
│ Error: Invalid attribute name
│ 
│ On .terraform/modules/SCP-L2-RegionRestriction/main.tf line 4: An attribute name is required after a dot.
╵

有没有办法在数据调用中管理变量"${var.scp.name}"

这是我的数据源内容:

data "aws_iam_policy_document" "scp_fulladmin_deny" {
    statement {
            actions = ["*"]
            resources = ["*"]
            effect = "Deny"
            }
}
data "aws_iam_policy_document" "scp_fulladmin_allow" {
    statement {
            actions = ["*"]
            resources = ["*"]
            effect = "Allow"
            }
}

您不能使用变量来引用数据或资源; 但你可以这样做:

locals {
    policy_map = {
        scp_fulladmin_deny = data.aws_iam_policy_document.scp_fulladmin_deny.json,
        scp_fulladmin_allow = data.aws_iam_policy_document.scp_fulladmin_allow.json,
    }
}

resource "aws_organizations_policy" "this" {
    name = var.scp_name
    content = local.policy_map[var.scp_name]
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM