Терраформирование объектов в ведрах


Я пытаюсь создать 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>
1 2

1 ответ:

Для этого нужен спортзал кода.

Это должно сработать, остальные коды такие же.

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>"