存储桶中的 Terraform 对象

Terraform Objects in Buckets

我正在尝试创建 3 个包含 3 个相同对象的存储桶,如下所示:

关于如何使这项工作有任何想法吗?

我有以下代码:

resource "aws_s3_bucket" "app-bucket" {
  count  = "${length(var.buckets)}"
  bucket = "com.${element(var.buckets, count.index)}.cami"
  acl    = "private"

  tags {
    Name        = "${var.global_product}-${var.global_account_number}"
    contact     = "${var.global_contact}"
    product     = "${var.global_product}"
    environment = "${var.global_environment}-${var.local_environment}"
    role        = "server-bucket"
  }
}

resource "aws_s3_bucket_object" "app-bucket-objects" {
    count  = "${length(var.bucket_objects)}"
    bucket = "${element(aws_s3_bucket.app-bucket.*.bucket, count.index)}"
    acl    = "private"
    key    = "${element(var.bucket_objects, count.index)}"
    source = "/dev/null"
}

具有以下变量

buckets                 = ["dev", "cert", "int"]
bucket_objects          = ["Archive", "Software", "Input"]

产生

+ aws_s3_bucket_object.app-bucket-objects.0
    acl:                    "private"
    bucket:                 "com.dev.cami"
    content_type:           "<computed>"
    etag:                   "<computed>"
    key:                    "Archive"
    server_side_encryption: "<computed>"
    source:                 "/dev/null"
    storage_class:          "<computed>"
    version_id:             "<computed>"

+ aws_s3_bucket_object.app-bucket-objects.1
    acl:                    "private"
    bucket:                 "com.cert.cami"
    content_type:           "<computed>"
    etag:                   "<computed>"
    key:                    "Software"
    server_side_encryption: "<computed>"
    source:                 "/dev/null"
    storage_class:          "<computed>"
    version_id:             "<computed>"

+ aws_s3_bucket_object.app-bucket-objects.2
    acl:                    "private"
    bucket:                 "com.int.cami"
    content_type:           "<computed>"
    etag:                   "<computed>"
    key:                    "Input"
    server_side_encryption: "<computed>"
    source:                 "/dev/null"
    storage_class:          "<computed>"
    version_id:             "<computed>

它需要代码健身房。

这应该可以,其余代码相同。

resource "aws_s3_bucket_object" "app-bucket-objects" {
  # So you need totally 3 * 3 = 9 bucket objects
  count  = "${length(var.buckets) * length(var.bucket_objects)}"

  # / is division, so you get 0,0,0,1,1,1 ...
  bucket = "${element(aws_s3_bucket.app-bucket.*.bucket, count.index / length(var.bucket_objects))}"

  acl    = "private"

  # % returns reminder, so you get 0,1,2,0,1,2 ...
  key    = "${element(var.bucket_objects, count.index % length(var.bucket_objects) )}"

  source = "/dev/null"
}

输出

+ aws_s3_bucket_object.app-bucket-objects.0
    acl:                    "private"
    bucket:                 "com.dev.cami"
    content_type:           "<computed>"
    etag:                   "<computed>"
    key:                    "Archive"
    server_side_encryption: "<computed>"
    source:                 "/dev/null"
    storage_class:          "<computed>"
    version_id:             "<computed>"

+ aws_s3_bucket_object.app-bucket-objects.1
    acl:                    "private"
    bucket:                 "com.dev.cami"
    content_type:           "<computed>"
    etag:                   "<computed>"
    key:                    "Software"
    server_side_encryption: "<computed>"
    source:                 "/dev/null"
    storage_class:          "<computed>"
    version_id:             "<computed>"

+ aws_s3_bucket_object.app-bucket-objects.2
    acl:                    "private"
    bucket:                 "com.dev.cami"
    content_type:           "<computed>"
    etag:                   "<computed>"
    key:                    "Input"
    server_side_encryption: "<computed>"
    source:                 "/dev/null"
    storage_class:          "<computed>"
    version_id:             "<computed>"

+ aws_s3_bucket_object.app-bucket-objects.3
    acl:                    "private"
    bucket:                 "com.cert.cami"
    content_type:           "<computed>"
    etag:                   "<computed>"
    key:                    "Archive"
    server_side_encryption: "<computed>"
    source:                 "/dev/null"
    storage_class:          "<computed>"
    version_id:             "<computed>"

+ aws_s3_bucket_object.app-bucket-objects.4
    acl:                    "private"
    bucket:                 "com.cert.cami"
    content_type:           "<computed>"
    etag:                   "<computed>"
    key:                    "Software"
    server_side_encryption: "<computed>"
    source:                 "/dev/null"
    storage_class:          "<computed>"
    version_id:             "<computed>"

+ aws_s3_bucket_object.app-bucket-objects.5
    acl:                    "private"
    bucket:                 "com.cert.cami"
    content_type:           "<computed>"
    etag:                   "<computed>"
    key:                    "Input"
    server_side_encryption: "<computed>"
    source:                 "/dev/null"
    storage_class:          "<computed>"
    version_id:             "<computed>"