Answers: <p>Variables are static, literal inputs to your configuration that come from either the command line or from <code>tfvars</code> files. It is not possible to use interpolations here, since the variable values are processed before interpolation begins in order to make them available to other expressions.</p>
  
  <hr>
  
  <p>If the goal is to define the S3 bucket name in one place and re-use it several times in the same configuration, the <em><a href="https://www.terraform.io/docs/configuration/locals.html" rel="nofollow noreferrer">Local Values</a></em> feature can do this if you are using Terraform v0.10.4 or newer. You can declare the named value in one of your <code>.tf</code> files:</p>
  
  <pre><code>locals {
    s3_bucket_name = "tf-${terraform.workspace}"
  }
  </code></pre>
  
  <p>You can then interpolate this value in various places in your configuration using the name <code>local.s3_bucket_name</code>, like this:</p>
  
  <pre><code>resource "aws_s3_bucket" "s3" {
    bucket = "${local.s3_bucket_name}"
    ...
  }
  </code></pre>
  
  <p>Local values are conceptually similar to <a href="https://www.terraform.io/docs/configuration/outputs.html" rel="nofollow noreferrer">module outputs</a> but they are exposed within the same module they are defined in, rather than passing a value up to the parent module.</p>
  
  <hr>
  
  <p>If the goal is to allow this generated default name to be overriden optionally by a variable, then that can be achieved with some conditional logic:</p>
  
  <pre><code>variable "override_s3_bucket_name" {
    default = ""
  }
  
  locals {
    s3_bucket_name = "${var.override_s3_bucket_name != "" ? var.override_s3_bucket_name : "tf-${terraform.workspace}"}"
  }
  
  resource "aws_s3_bucket" "s3" {
    bucket = "${local.s3_bucket_name}"
    ...
  }
  </code></pre>
  
  <p>The above will generate a default name based on the selected workspace if <code>override_s3_bucket_name</code> is not set, but if that variable is set then its value will be used instead.</p>
  
Answers: CC BY-SA 3.0
Answers: 2018-01-12T21:55:03.980Z
Answers: Thanks @Martin. I will use your local variable solutions!
Answers: user389955
Answers: CC BY-SA 3.0
Answers: 2018-01-12T17:41:24.810Z
Answers: 2018-01-12T17:41:24.810Z
Answers: Martin Atkins
Body: <p>I want to have different s3 names based on different environments using terraform.</p>
  
  <p>If I define the name in terraform.tfvar, it won't work:</p>
  
  <pre><code>s3_bucket_name          = "tf-${terraform.workspace}"
  </code></pre>
  
  <p>If I hardcode s3 bucket name in s3.tf, it works:</p>
  
  <pre><code>resource "aws_s3_bucket" "s3" {
     bucket        = "tf-${terraform.workspace}"
     ....
  }
  </code></pre>
  
  <p>how to define the workspace related name from terraform.tfvar? I do not want to hard code it in tf file.</p>
  
Tags: <terraform>
Title: create resource name based on terraform.workspace
UserDisplayName: user389955
ThreadText: # create resource name based on terraform.workspace
  
  (Asked by: username_0 on 2018-01-12)
  
  I want to have different s3 names based on different environments using terraform.
  
  If I define the name in terraform.tfvar, it won't work:
  
  ```
  s3_bucket_name          = "tf-${terraform.workspace}"
  
  ```
  
  If I hardcode s3 bucket name in s3.tf, it works:
  
  ```
  resource "aws_s3_bucket" "s3" {
     bucket        = "tf-${terraform.workspace}"
     ....
  }
  
  ```
  
  how to define the workspace related name from terraform.tfvar? I do not want to hard code it in tf file.
  
  **Tags:** terraform
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2018-01-12. Score: 4 | Accepted answer
  
  Variables are static, literal inputs to your configuration that come from either the command line or from `tfvars` files. It is not possible to use interpolations here, since the variable values are processed before interpolation begins in order to make them available to other expressions.
  
  ---
  
  If the goal is to define the S3 bucket name in one place and re-use it several times in the same configuration, the *[Local Values](https://www.terraform.io/docs/configuration/locals.html)* feature can do this if you are using Terraform v0.10.4 or newer. You can declare the named value in one of your `.tf` files:
  
  ```
  locals {
    s3_bucket_name = "tf-${terraform.workspace}"
  }
  
  ```
  
  You can then interpolate this value in various places in your configuration using the name `local.s3_bucket_name`, like this:
  
  ```
  resource "aws_s3_bucket" "s3" {
    bucket = "${local.s3_bucket_name}"
    ...
  }
  
  ```
  
  Local values are conceptually similar to [module outputs](https://www.terraform.io/docs/configuration/outputs.html) but they are exposed within the same module they are defined in, rather than passing a value up to the parent module.
  
  ---
  
  If the goal is to allow this generated default name to be overriden optionally by a variable, then that can be achieved with some conditional logic:
  
  ```
  variable "override_s3_bucket_name" {
    default = ""
  }
  
  locals {
    s3_bucket_name = "${var.override_s3_bucket_name != "" ? var.override_s3_bucket_name : "tf-${terraform.workspace}"}"
  }
  
  resource "aws_s3_bucket" "s3" {
    bucket = "${local.s3_bucket_name}"
    ...
  }
  
  ```
  
  The above will generate a default name based on the selected workspace if `override_s3_bucket_name` is not set, but if that variable is set then its value will be used instead.
  
  > **Comments:**
  > **username_0**: Thanks @username_1. I will use your local variable solutions!  
  
  ---
  
  