铲斗中的Terraform对象

时间:2017-04-26 12:56:48

标签: terraform

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

  • com.dev.example< - bucket

    • 存档
    • 软件
    • 输入
  • com.cert.example< - bucket

    • 存档
    • 软件
    • 输入
  • com.prod.example< - bucket

    • 存档
    • 软件
    • 输入

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

我有以下代码:

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 个答案:

答案 0 :(得分:4)

它需要代码健身房。

这应该有效,其余代码相同。

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