Create multiple S3 buckets at once 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/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
  }
}


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}"
        }
    ]
}
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 = &lt;&lt;EOF
EOF
  }
}