Answers: <p>You can make changes in <code>Main.tf</code> as specified below:</p>
  
  <pre><code>resource "aws_s3_bucket" "b" {
    count      = "${length(var.domain_name)}"
    bucket     = "${element(var.domain_name, count.index)}"
    acl        = "public-read"
    policy     = "${file("bucket-policy.json")}"
  
    website {
      index_document = "index.html"
      error_document = "error.html"
  
      routing_rules = &lt;&lt;EOF
  EOF
    }
  }
  </code></pre>
  
Answers: CC BY-SA 4.0
Answers: 2019-08-29T14:39:47.897Z
Answers: Thanks for your answer, very helpful.  Do you also know how to make the variables in the JSON part work?
Answers: Eoin
Answers: CC BY-SA 4.0
Answers: 2019-08-30T22:09:35.683Z
Answers: I tried this and I got this error: Call to function "element" failed: cannot read elements from string.
Answers: Eoin
Answers: CC BY-SA 4.0
Answers: 2019-08-30T22:12:36.127Z
Answers: I tried this but no avail `${tolist(var.domain_name)[count.index]}"`
Answers: Eoin
Answers: CC BY-SA 4.0
Answers: 2019-08-31T16:41:36.020Z
Answers: what is your terraform version?
Answers: Samit
Answers: CC BY-SA 4.0
Answers: 2019-08-31T17:51:42.863Z
Answers: it is 0.12.7 I just downloaded it very recently.
Answers: Eoin
Answers: CC BY-SA 4.0
Answers: 2019-08-28T23:30:52.787Z
Answers: 2019-08-28T23:30:52.787Z
Answers: Samit
Body: <p>I am trying to create a static website using S3 buckets.  I want to create a bucket for www and non-www versions.  I can't work out how to create two buckets at once.  I read that you can chain two entries together using square brackets.  And I read that you can use variables in the json file.  </p>
  
  <p>So far I haven't managed to get variables or multiple entries to work.  Can anyone suggest what I am doing wrong?</p>
  
  <p>I have used this code:</p>
  
  <p><strong>Main.tf</strong></p>
  
  <pre><code>provider "aws" {
    shared_credentials_file = "C:/Users/Eoin.Oliver/.aws/terraform.tfvars"  
    profile                 = "customprofile"  
    access_key = "${var.access_key}"
    secret_key = "${var.secret_key}"
    region     = "${var.region}"
  }
  
  resource "aws_s3_bucket" "b" {
    count      = "${length(var.domain_name)}"
    bucket     = "${var.domain_name[count.index]}"
    acl        = "public-read"
    policy     = "${file("bucket-policy.json")}"
  
    website {
      index_document = "index.html"
      error_document = "error.html"
  
      routing_rules = &lt;&lt;EOF
  EOF
    }
  }
  </code></pre>
  
  <p><strong>Variables.tf</strong></p>
  
  <pre><code>variable "access_key" {}
  variable "secret_key" {}
  variable "region" {
      default = "eu-west-2"
  }
  variable "domain_name" {}
  </code></pre>
  
  <p><strong>terraform.tvars</strong></p>
  
  <pre><code>domain_name             = ["website.com", "www.website.com"]
  region                  = "eu-west-2"
  access_key              = "xxxxxxxxx"
  secret_key              = "xxxxxxxxxxx"
  </code></pre>
  
  <p><strong>bucket-policy.json</strong></p>
  
  <pre><code>{
      "Version": "2012-10-17",
       "Statement": [
          {
               "Effect": "Allow",
              "Principal": {
                  "AWS": ["arn:aws:iam::240479180938:user/${iams_user_name}"]
              },
              "Action": "s3:PutBucketPolicy",
              "Resource": "arn:aws:s3:::${domain_name}"
          }
      ]
  }
  </code></pre>
  
Tags: <terraform>
Title: Create multiple S3 buckets at once
UserDisplayName: Eoin
ThreadText: # Create multiple S3 buckets at once
  
  (Asked by: username_0 on 2019-08-28)
  
  I am trying to create a static website using S3 buckets. I want to create a bucket for www and non-www versions. I can't work out how to create two buckets at once. I read that you can chain two entries together using square brackets. And I read that you can use variables in the json file.
  
  So far I haven't managed to get variables or multiple entries to work. Can anyone suggest what I am doing wrong?
  
  I have used this code:
  
  **Main.tf**
  
  ```
  provider "aws" {
    shared_credentials_file = "C:/Users/username_0.Oliver/.aws/terraform.tfvars"  
    profile                 = "customprofile"  
    access_key = "${var.access_key}"
    secret_key = "${var.secret_key}"
    region     = "${var.region}"
  }
  
  resource "aws_s3_bucket" "b" {
    count      = "${length(var.domain_name)}"
    bucket     = "${var.domain_name[count.index]}"
    acl        = "public-read"
    policy     = "${file("bucket-policy.json")}"
  
    website {
      index_document = "index.html"
      error_document = "error.html"
  
      routing_rules = <<EOF
  EOF
    }
  }
  
  ```
  
  **Variables.tf**
  
  ```
  variable "access_key" {}
  variable "secret_key" {}
  variable "region" {
      default = "eu-west-2"
  }
  variable "domain_name" {}
  
  ```
  
  **terraform.tvars**
  
  ```
  domain_name             = ["website.com", "www.website.com"]
  region                  = "eu-west-2"
  access_key              = "xxxxxxxxx"
  secret_key              = "xxxxxxxxxxx"
  
  ```
  
  **bucket-policy.json**
  
  ```
  {
      "Version": "2012-10-17",
       "Statement": [
          {
               "Effect": "Allow",
              "Principal": {
                  "AWS": ["arn:aws:iam::240479180938:user/${iams_user_name}"]
              },
              "Action": "s3:PutBucketPolicy",
              "Resource": "arn:aws:s3:::${domain_name}"
          }
      ]
  }
  
  ```
  
  **Tags:** terraform
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2019-08-28. Score: 1
  
  You can make changes in `Main.tf` as specified below:
  
  ```
  resource "aws_s3_bucket" "b" {
    count      = "${length(var.domain_name)}"
    bucket     = "${element(var.domain_name, count.index)}"
    acl        = "public-read"
    policy     = "${file("bucket-policy.json")}"
  
    website {
      index_document = "index.html"
      error_document = "error.html"
  
      routing_rules = <<EOF
  EOF
    }
  }
  
  ```
  
  > **Comments:**
  > **username_0**: Thanks for your answer, very helpful.  Do you also know how to make the variables in the JSON part work?  
  > **username_0**: I tried this and I got this error: Call to function "element" failed: cannot read elements from string.  
  > **username_0**: I tried this but no avail `${tolist(var.domain_name)[count.index]}"`  
  > **username_1**: what is your terraform version?  
  > **username_0**: it is 0.12.7 I just downloaded it very recently.  
  
  ---
  
  