S3 存储桶资源未能通过数据找到。

huangapple go评论91阅读模式
英文:

S3 bucket resource not being found using data

问题

Module s3_notification 使用了对该存储桶的引用。这样做的原因是因为当我在本地运行时,我将会创建这个存储桶。如果部署的是代码,该存储桶将已经存在,我就不需要创建它。

然而,在运行 terraform apply 时,我遇到了以下错误:

Error: Failed getting S3 bucket (my-bucket): NotFound: Not Found
... in data "aws_s3_bucket" "s3_notifications_bucket":

我该如何在处理 data 引用之前创建该存储桶?

英文:

I have a main.tf that looks like this:

resource "aws_s3_bucket" "s3_notifications_bucket" {
  bucket        = local.s3_bucket_name
  force_destroy = true
}

module "s3_notification" {
  source              = "..."
  s3_bucket_name      = local.s3_bucket_name
  function_name       = module.lambda.lambda_function_name
  lambda_function_arn = module.lambda.lambda_arn
}

Module s3_notification uses a reference for that bucket. The reason for that is because when I am running locally, I will be creating this bucket. If it's deployed code, the bucket will already exist and I don't have to create it.

data "aws_s3_bucket" "s3_notifications_bucket" {
  bucket = var.s3_bucket_name
}

resource "aws_s3_bucket_notification" "aws_lambda_trigger" {
  bucket = data.aws_s3_bucket.s3_notifications_bucket.id

  lambda_function {
    lambda_function_arn = var.lambda_function_arn
    events              = ["s3:ObjectCreated:*"]
  }
}
    
resource "aws_lambda_permission" "lambda_permission" {
  statement_id  = "AllowS3Invoke"
  action        = "lambda:InvokeFunction"
  function_name = var.function_name
  principal     = "s3.amazonaws.com"
  source_arn    = "arn:aws:s3:::${data.aws_s3_bucket.s3_notifications_bucket.id}"
}

However, when I run terraform apply, I get the following error:

Error: Failed getting S3 bucket (my-bucket): NotFound: Not Found
... in data "aws_s3_bucket" "s3_notifications_bucket":

How do I make that bucket to be created before data reference gets processed?

答案1

得分: 3

以下是翻译好的部分:

There is actually no need to rely on using a data source in this case, there needs to be one slight change:

这种情况实际上不需要依赖于使用数据源,只需要进行一个小的更改:

module "s3_notification" {
  source              = "..."
  s3_bucket_name      = aws_s3_bucket.s3_notifications_bucket.id
  function_name       = module.lambda.lambda_function_name
  lambda_function_arn = module.lambda.lambda_arn
}

This uses an implicit resource reference, which means that the S3 bucket will be created first, and only then the bucket name attribute will be passed to the s3_bucket_name in the module call.

这使用了一个隐式的资源引用,这意味着S3存储桶将首先被创建,然后才会将存储桶名称属性传递给模块调用中的s3_bucket_name

Additionally, one change needs to happen in the module code as well:

另外,在模块代码中还需要进行一些更改:

resource "aws_s3_bucket_notification" "aws_lambda_trigger" {
  bucket = var.s3_bucket_name
  lambda_function {
    lambda_function_arn = var.lambda_function_arn
    events              = ["s3:ObjectCreated:*"]
  }
}

and the data source can be dropped for local testing.

并且数据源可以在本地测试时被删除。

英文:

There is actually no need to rely on using a data source in this case, there needs to be one slight change:

module "s3_notification" {
  source              = "..."
  s3_bucket_name      = aws_s3_bucket.s3_notifications_bucket.id
  function_name       = module.lambda.lambda_function_name
  lambda_function_arn = module.lambda.lambda_arn
}

This uses an implicit resource reference, which means that the S3 bucket will be created first, and only then the bucket name attribute will be passed to the s3_bucket_name in the module call.

Additionally, one change needs to happen in the module code as well:

resource "aws_s3_bucket_notification" "aws_lambda_trigger" {
  bucket = var.s3_bucket_name
  lambda_function {
    lambda_function_arn = var.lambda_function_arn
    events              = ["s3:ObjectCreated:*"]
  }
}

and the data source can be dropped for local testing.

huangapple
  • 本文由 发表于 2023年3月9日 18:31:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/75683356.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定