与其使用aws控制台简单地将一些预先存在的策略附加到预先存在的角色,我还需要通过模块中的Terraform对需要perms的特定系统执行此操作。
不过,我没什么运气吗?
variables.tf
variable "masterrole" {
description = "role already present within the cn-tio-tooling-acc"
default = "arn:aws-cn:iam::12345678910:role/Master"
}
variable "policies" {
description = "policies already present within the cn-tio-tooling-acc"
default = "arn:aws-cn:iam::12345678910:policy/Source-1,arn:aws-cn:iam::351767606935:policy/Source-2"
}
data.tf <-引用帐户中已经存在的角色和策略数据
data "aws_iam_role" "masterrole" {
name = "Master"
}
data "aws_iam_policy" "policies" {
arn = var.policies
}
IAM.tf
resource "aws_iam_role_policy_attachment" "Sources" {
role = aws_iam_role.masterrole.name
policy_arn = aws_iam_policy.policies.arn
}
这里可能有些非常简单的东西,但是为什么我要从“计划”结果中得到下面的结果呢?
错误:引用资源"aws_iam_role_policy_attachment“”aws_iam_role_policy_attachment“中的cn_cpm_iam.tf第3行中未声明的资源: 3: role = aws_iam_role.masterrole.name --托管资源"aws_iam_role”“aws_iam_role”“aws_iam_role”尚未在根模块中声明。
错误:对cn_cpm_iam.tf第4行中未声明的资源的引用,在资源"aws_iam_role_policy_attachment“源: 4: policy_arn = aws_iam_policy.cpmpolicies.arn管理资源"aws_iam_policy”策略“尚未在根模块中声明”。
发布于 2020-11-10 04:45:47
当引用terraform中的数据源时,您需要在它们的前缀加上data.
。所以试着用
resource "aws_iam_role_policy_attachment" "Sources" {
role = data.aws_iam_role.masterrole.name
policy_arn = data.aws_iam_policy.policies.arn
}
但是,正如您已经知道名称和ARN一样,您只需使用它们而无需查询数据源:
resource "aws_iam_role_policy_attachment" "Sources" {
role = "Master"
policy_arn = var.policies
}
如果我在这里遗漏了什么,请告诉我;)
https://stackoverflow.com/questions/64760068
复制相似问题