Terraform - 使用 depends_on 创建 aws_s3_bucket_notification 会出现 returns 错误,找不到匹配的 SNS 主题

Terraform - creating aws_s3_bucket_notification with depends_on does returns error no matching SNS Topic found

我有以下 S3 模块代码:

#------------------------------------------------------------
# variables
#------------------------------------------------------------
variable "ENV" {}

#------------------------------------------------------------
# data
#------------------------------------------------------------
data "aws_s3_bucket" "dlt_bucket" {
  bucket = "dlt-bucket-${var.ENV}"
}

data "aws_sns_topic" "dlt_sns_topic" {
  name = "dlt-sns-${var.ENV}"
}

#------------------------------------------------------------
# resources
#------------------------------------------------------------
resource "aws_s3_bucket_notification" "bucket_notification" {
  bucket = data.aws_s3_bucket.dlt_bucket.id

  topic {
    topic_arn     = data.aws_sns_topic.dlt_sns_topic.arn
    events        = ["s3:ObjectCreated:*"]
    filter_prefix = "raw-content-topic-${var.ENV}-dlt"
  }
}

这是我的主要模块:

#------------------------------------------------------------
# main.tf
#------------------------------------------------------------

locals {
  local_data = jsondecode(file("${path.module}/vars-${var.environment}.json"))
}

module "infrastructure_sns" {
  source                         = "./modules/infrastructure/resources/sns"
  ENV                            = var.environment
  DLT_NOTIFICATION_EMAIL_ADDRESS = local.local_data.dlt_notification_email_address
}

module "infrastructure_s3" {
  source = "./modules/infrastructure/resources/s3"
  ENV    = var.environment

  depends_on = [
    module.infrastructure_sns.dlt_sns_topic
  ]
}

但是当我运行计划时,我得到以下错误:

Error: no matching SNS Topic found
”‚ 
”‚   with module.infrastructure_s3.data.aws_sns_topic.dlt_sns_topic,
”‚   on modules/infrastructure/resources/s3/main.tf line 13, in data "aws_sns_topic" "dlt_sns_topic":
”‚   13: data "aws_sns_topic" "dlt_sns_topic" {

我尝试在 S3 模块中设置 data 并从那里调用 sns_topic,并在 S3 模块中添加本地 depends_on,但仍然出现相同的错误。

请告诉我如何解决这个问题?

通过将 SNS 主题作为变量传递给 S3 模块解决:

module "infrastructure_s3" {
  source                 = "./modules/infrastructure/resources/s3"
  ENV                    = var.environment
  SNS_DLT_TOPIC_ARN      = module.infrastructure_sns.dlt_sns_topic.arn
}

SNS模块output.tf

output "dlt_sns_topic" {
  value       = aws_sns_topic.dlt_sns_topic
  description = "dlt sns topic"
}

S3模块main.tf

#------------------------------------------------------------
# variables
#------------------------------------------------------------
variable "ENV" {}
variable "SNS_DLT_TOPIC_ARN" {}

#------------------------------------------------------------
# data
#------------------------------------------------------------
data "aws_s3_bucket" "dlt_bucket" {
  bucket = "dlt-bucket-${var.ENV}"
}

#------------------------------------------------------------
# resources
#------------------------------------------------------------
resource "aws_s3_bucket_notification" "bucket_notification" {
  bucket = data.aws_s3_bucket.dlt_bucket.id

  topic {
    topic_arn     = var.SNS_DLT_TOPIC_ARN
    events        = ["s3:ObjectCreated:*"]
    filter_prefix = "raw-content-topic-${var.ENV}-dlt"
  }
}