如何通过 Terraform 中的 ARN 添加依赖项?

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

How to add dependency via arn in terraform?

问题

我有Terraform代码,用于创建一些AWS资源。例如,我创建一个S3存储桶,并根据它创建另一个资源,或者依赖于这个S3资源。我知道Terraform会确定这个新对象资源依赖于第一个S3存储桶资源,因为我已经将这个=> aws_s3_bucket.s3.id 传递给新资源。我不确定这是ARN、存储桶名称还是Terraform维护的某个内部ID。但是是否可以通过传递ARN或其他内容来添加这种依赖关系呢?

假设我有一个已经在其他地方创建的存储桶名称,我可以在下面的示例中简单地传递它的ARN,所以不是aws_s3_bucket.s3.id,而是传递存储桶名称,如var.bucket name,这样会起作用吗?

我尝试过传递这个=> aws_s3_bucket.s3.id,这样可以工作。但我想知道是否可以传递ARN。

英文:

I have terraform code , to create few aws resources. for example . i create a s3 bucket and create another resource based on or which depends on this s3 resource. I know terraform will figure out that this new object resource is dependent on the first s3 bucket resource, as i have passed this => aws_s3_bucket.s3.id , to the new resource. I'm not sure if this is a arn or bucket name or some intrinsic id that terraform maintains. but is it possible to add such dependency by passing in arn or something else?

so say , i had a bucket name that is already created somewhere else, and I can simply pass its arn in my example below , so instead of aws_s3_bucket.s3.id , i can pass bucket name , as var.bucket name
will that work?

resource "aws_s3_bucket" "s3" {
  bucket = "s3-bucket-name"
  acl    = "private"
  ...
}

resource "aws_s3_bucket_object" "init" {
  bucket = aws_s3_bucket.s3.id
  key    = "bin.tar.gz"
  ....
}

I've tried passing this => aws_s3_bucket.s3.id, which works. but want to know if i can pass an arn

答案1

得分: 1

  1. aws_s3_bucket.s3.id 实际上是存储桶名称;
  2. aws_s3_bucket_object.init.bucket 预期的是存储桶名称,传递存储桶 ARN 将导致错误;
  3. aws_s3_bucket_object.init.bucket 预期的是一个字符串,您可以以任何方式传递它,只要它是有效的字符串存储桶名称,var.bucket 完全可行。
英文:
  1. aws_s3_bucket.s3.id is actually the bucket name;
  2. aws_s3_bucket_object.init.bucket expects a bucket name, passing a bucket ARN will result in an error;
  3. aws_s3_bucket_object.init.bucket expects a string, it doesn't matter how you pass it there, so var.bucket will totally work as long as it's a valid string bucket name.

huangapple
  • 本文由 发表于 2023年3月4日 02:27:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/75630651.html
匿名

发表评论

匿名网友

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

确定