mellifluous
mellifluous

Reputation: 2975

How to send S3 bucket region name to a json file using Terraform?

I am creating a S3 bucket using Terraform, I need to send the region name aws_s3_bucket.website_bucket.region in which the bucket was created to a json file (root/region.json) in the below format.

root/region.json

{
   "region": "us-east-2"
}

root/s3.tf

resource "aws_s3_bucket" "website_bucket" {
  bucket   = var.website_bucket_name
  provider = aws.east
  acl      = "public-read"

  cors_rule {
    allowed_headers = ["*"]
    allowed_methods = ["PUT", "POST", "GET", "DELETE"]
    allowed_origins = ["*"]
  }

  website {
    index_document = "index.html"
  }
}

Upvotes: 0

Views: 275

Answers (1)

stefansundin
stefansundin

Reputation: 3044

The following should accomplish the task:

resource "local_file" "region" {
  filename = "root/region.json"
  content  = jsonencode({
    "region": aws_s3_bucket.website_bucket.region,
  })
}

Upvotes: 2

Related Questions