我正在尝试使用 terraform 模块创建 AWS kinesis firehose 数据流。但我收到错误。我尝试了多种选项,但仍然遇到相同的错误,有人可以帮助我了解这里缺少什么吗?
> Error: Reference to undeclared resource on
> modules/kinesis/main.tf in resource
> "aws_kinesis_firehose_delivery_stream" "firehose": 3:
> depends_on = [aws_iam_role.firehose] A managed resource
> "aws_iam_role" "firehose" has not been declared in module.firehose.
> Error: Reference to undeclared module on
> modules/kinesis/main.tf in resource
> "aws_kinesis_firehose_delivery_stream" "firehose":
> role_arn = module.iam_role.iam_role_arn No module call named
> "iam_role" is declared in module.firehose.
主.tf
module "iam_role" {
source = "./modules/iam_role"
role_name = "firehose-role"
}
module "firehose" {
source = "./modules/kinesis"
firehose_name = var.firehose_name
#destination = "splunk"
splunk_endpoint = var.splunk_hec_endpoint
splunk_token = var.splunk_hec_token
backup_bucket_arn = module.s3_bucket.s3_bucket_id
#role_arn = aws_iam_role.firehose_role.arn
role_arn = module.iam_role.iam_role_arn
}
模块/iam_role/main.tf
resource "aws_iam_role" "firehose_role" {
name = var.role_name
assume_role_policy = jsonencode({
Version = "2012-10-17",
Statement = [
{
Action = "sts:AssumeRole",
Effect = "Allow",
Principal = {
Service = "firehose.amazonaws.com"
}
}
]
})
}
output "iam_role_arn" {
value = aws_iam_role.firehose_role.arn
}
模块/kinesis/main.tf
resource "aws_kinesis_firehose_delivery_stream" "firehose" {
depends_on = [aws_iam_role.firehose]
#depends_on = [module.iam_role]
name = var.firehose_name
destination = "splunk"
role_arn = module.iam_role.iam_role_arn
splunk_configuration {
hec_endpoint = var.splunk_endpoint
hec_token = var.splunk_token
hec_acknowledgment_timeout = 600
hec_endpoint_type = "Event"
s3_backup_mode = "Disabled"
s3_configuration {
role_arn = module.iam_role.iam_role_arn
#role_arn = aws_iam_role.firehose_role.iam_role_arn
bucket_arn = var.backup_bucket_arn
prefix = "failed-events/"
}
}
}
错误消息准确地告诉您问题是什么:
托管资源 “aws_iam_role”“firehose”尚未在 module.firehose 中声明
和
没有命名的模块调用 “iam_role”在 module.firehose 中声明。
在我看来,您已经开始将所有内容都放在一个文件中,并开始将各个部分提取到单独的模块中,而无需更新模块和资源引用。模块有自己的命名空间,无法自动看到其他模块或父命名空间的资源。
您在
modules/kinesis/
中有一个模块,在 modules/iam_role/
中有一个单独的模块。在您的 kinesis 模块中,您尝试引用资源 aws_iam_role.firehose
,但该资源在 kinesis 模块中不存在。就我个人而言,我会将该角色包含在 kinesis 模块中,但如果您想将其保留为单独的模块,则需要将对角色资源的引用作为输入变量传递到模块中。另请注意,您正在使用不同的资源名称,尽管您将在更改为使用输入变量时修复该名称。
此外,如果您向流提供角色 ARN,例如
resource "aws_kinesis_firehose_delivery_stream" "firehose" {
...
role_arn = var.role.iam_role_arn
...
}
那么你就不需要使用
depends_on
参数,因为流将自动依赖于角色。