---
title: "Demonstrating Unique Instances with Terraform Workspace"
description: "If you need to work on a project simultaneously with your team without messing things up, consider Terraform workspace. Learn how to create and launch instances in different Terraform workspaces in this tutorial!"
canonical: "https://adamtheautomator.com/terraform-workspace/"
---

# Demonstrating Unique Instances with Terraform Workspace

> If you need to work on a project simultaneously with your team without messing things up, consider Terraform workspace. Learn how to create and launch instances in different Terraform workspaces in this tutorial!

Source: https://adamtheautomator.com/terraform-workspace/

---

ATA Learning

Tap to hide

[

ATA Learning

](/)

*   [Home](/)
*   [Tutorials](/tutorials/)
*   [Instructors](/author/)
*   [Advertising](/advertising/)
*   [Recommended Resources](/resources/)
*   [About Adam](/about-adam/)

Search for:  

*   [](https://twitter.com/adbertram)
*   [](https://github.com/Adam-the-Automator)
*   [](https://www.linkedin.com/company/adam-the-automator-llc)
*   [](/feed/)

![Demonstrating Unique Instances with Terraform Workspace](https://adamtheautomator.com/wp-content/uploads/2021/11/Demonstrating-Unique-Instances-with-Terraform-Workspace.jpg)

# Demonstrating Unique Instances with Terraform Workspace

[![](https://secure.gravatar.com/avatar/2beb65fca997135120ed98dc6a2e57dcdf1a7d7d2f5ff687b5d91dc7ccd7a6b5?s=192&d=mm&r=g)Sagar](https://adamtheautomator.com/author/shanky-mendiratta/)25 November 20215 min. read

Categories: [DevOps](/category/devops/), [Uncategorized](/category/uncategorized/)

Tags:[Terraform](/tag/terraform/)

Table of Contents

*   [Prerequisites](#prerequisites)
*   [Creating Multiple Terraform Workspaces](#creating-multiple-terraform-workspaces)
*   [Building Terraform Configurations to Launch EC2 Instances](#building-terraform-configurations-to-launch-ec2-instances)
*   [Creating Two AWS EC2 Instances in Different Workspaces](#creating-two-aws-ec2-instances-in-different-workspaces)
*   [Verifying the Resources Exist in AWS Console](#verifying-the-resources-exist-in-aws-console)
*   [Conclusion](#conclusion)

If you are building a huge infrastructure with Terraform, considering multiple [Terraform Workspace](https://www.terraform.io/docs/language/state/workspaces.html) is crucial. A Terraform workspace provides you with a separate virtual space to store your persistent data.

Throughout this tutorial, you’ll learn how Terraform workspace lets team members or developers work on a code without impacting existing resources.

Sounds interesting? Read on!

## Prerequisites

If you’d like to follow along in this tutorial, ensure you have the following in place:

*   An [Amazon Web Service account (AWS)](https://aws.amazon.com/premiumsupport/knowledge-center/create-and-activate-aws-account/) – You can create a free-tier account if you don’t have one.
*   [Terraform v0.14.9](https://releases.hashicorp.com/terraform/0.14.9/) – This tutorial’s demonstrations are on Ubuntu 18.04.5 LTS, but other operating systems with Terraform will work.

Related:[How to Install Terraform on Windows](https://adamtheautomator.com/terraform-windows/)

*   A code editor – Even though you can use any text editor to work with Terraform configuration files, you should have one that understands the HCL Terraform language. Try out [Visual Studio (VS) Code](https://code.visualstudio.com/).

Related:[What You Need to Know about Visual Studio Code: A Tutorial](https://adamtheautomator.com/visual-studio-code-tutorial/)

## Creating Multiple Terraform Workspaces

In today’s automation era, engineers such as Terraform developers need multiple workspaces to test the same scripts and code before building rock-solid resources.

Currently, the following backends support multiple workspaces: [AzureRM](https://www.terraform.io/docs/language/settings/backends/azurerm.html), [Consul](https://www.terraform.io/docs/language/settings/backends/consul.html), [COS](https://www.terraform.io/docs/language/settings/backends/cos.html), [GCS](https://www.terraform.io/docs/language/settings/backends/gcs.html), [Kubernetes](https://www.terraform.io/docs/language/settings/backends/kubernetes.html), [Local](https://www.terraform.io/docs/language/settings/backends/local.html), [Manta](https://www.terraform.io/docs/language/settings/backends/manta.html), [Postgres](https://www.terraform.io/docs/language/settings/backends/pg.html), [Remote](https://www.terraform.io/docs/language/settings/backends/remote.html), and [S3](https://www.terraform.io/docs/language/settings/backends/s3.html).

To better understand how multiple Terraform workspaces work, create two different workspaces:

1\. Open your terminal and run the commands below to create a folder in your home directory, and change the working directory to that folder.

In this tutorial, the folder is called _terraform-workspace-demo_, stored in your home directory. You can name the folder differently as you prefer. This folder stores your Terraform configuration files.

```bash
mkdir ~/terraform-workspace-demo
cd ~/terraform-workspace-demo
```

2\. Next, open your favorite code editor, and copy/paste the command below.

The [`terraform workspace new`](https://www.terraform.io/docs/cli/commands/workspace/new.html) command creates a new workspace (`workspace1`) and switches your current workspace to the newly created workspace.

For this example, the workspace is named `workspace1`, but you can preferably name it anything.

```bash
# Creating the new workspace workspace1 and switching to it.
terraform workspace new workspace1
```

![Creating the new workspace (workspace1) and switching to it ](https://adamtheautomator.com/wp-content/uploads/2021/11/image-369.png)

Creating the new workspace (workspace1) and switching to it

> _Perhaps you want to switch from one workspace to another. If so, run the command below. Replace `workspace-name` with the actual workspace name. `terraform workspace select workspace-name`_

3\. Similarly, run the below command to create another workspace. This time you’ll create another workspace named workspace2.

```bash
# Creating the new workspace workspace2 and switching to it.
terraform workspace new workspace2
```

As you can see below, you created and switched to **workspace2** successfully.

![Creating the new workspace (workspace2) and switching to it. ](https://adamtheautomator.com/wp-content/uploads/2021/11/image-370.png)

Creating the new workspace (workspace2) and switching to it.

> If you prefer to d_elete a workspace you don’t need anymore, run this `terraform workspace delete workspace-name` command._

## Building Terraform Configurations to Launch EC2 Instances

You now have two different Terraform workspaces to work with the same Terraform configurations, but you still need to create an instance. You’ll create the terraform configuration file to create an AWS EC2 instance in two different workspaces.

1\. Open your favorite code editor, and copy/paste the configuration below. Save the configuration as _main.tf_ inside the _~/terraform-workspace-demo_ directory.

> _Terraform uses several different types of configuration files. Each file is written in either plain text or JSON format with a specific naming convention of either [.tf or .tfjson format](https://www.terraform.io/docs/language/files/index.html)._

The _main.tf_ file allows you to create AWS EC2 instances with the [AMI](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/AMIs.html) and \[[instance type](https://aws.amazon.com/ec2/instance-types/?trkCampaign=acq_paid_search_brand&sc_channel=PS&sc_campaign=acquisition_IN&sc_publisher=Google&sc_category=Cloud%20Computing&sc_country=IN&sc_geo=APAC&sc_outcome=acq&sc_detail=amazon%20ec2%20instance%20types&sc_content=%7Bad%20group%7D&sc_matchtype=e&sc_segment=536392685920&sc_medium=ACQ-P%7CPS-GO%7CBrand%7CDesktop%7CSU%7CCloud%20Computing%7CEC2%7CIN%7CEN%7CSitelink&s_kwcid=AL!4422!3!536392685920!e!!g!!amazon%20ec2%20instance%20types&ef_id=Cj0KCQjwssyJBhDXARIsAK98ITQem3BsxzQ6K0bJYLG7zuCFqalemJ4STyfGcvXcNhTGMJZsJaDn8McaAmmBEALw_wcB:G:s&s_kwcid=AL!4422!3!536392685920!e!!g!!amazon%20ec2%20instance%20types)\] declared as variables. These variables have their values defined in another file named _terraform.tfvars_ that you will create later in this section.

> _You can find [Linux AMIs](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/finding-an-ami.html) via the Amazon EC2 console._

```yaml
# Declaring the resource block aws_instance
resource "aws_instance" "my-machine" {
# Declaring the argument ami
  ami = var.ami 
# Declaring the argument instance_type
  instance_type = var.instance_type 
# Tagging the instances 
  tags = {
    Name = "ec2-instance-${terraform.workspace}"
  }
}
```

2\. Now, create another file named _vars.tf_ inside the _~/terraform-workspace-demo_ directory, and copy/paste the content below to the _vars.tf_ file. The _vars.tf file_ is a [Terraform variables file](https://www.terraform.io/docs/language/values/variables.html), which contains all the variables that the configuration file references.

```bash
# Creating a Variable for ami
variable "ami" {       
  type = string
}

# Creating a Variable for instance_type
variable "instance_type" {    
  type = string
}
```

3\. Create one more file called _provider.tf_ inside _~/terraform-workspace-demo_ directory and put in the code below to the _provider.tf_ file. The _provider.tf_ file is where you define the AWS provider so that Terraform can connect to the correct cloud services.

> _Including all configuration values in a single configuration file is possible. But to keep things clear for developers and admins, breaking the logic and variables into separate files is much more preferable._

Related:[Related: Getting Started with the Terraform AWS Provider](https://adamtheautomator.com/terraform-aws/)

The code below defines the AWS provider named `aws` and creates resources in the `us-east-2` region.

> _You can launch aws resources in [any regions](https://aws.amazon.com/about-aws/global-infrastructure/regions_az/)_

```yaml
provider "aws" {
  region = "us-east-2"
}
```

4\. Create one last file inside the _~/terraform-workspace-demo_ directory. Name the file as _terraform.tfvars,_ and paste in the code below. The _terraform.tfvars_ contains the values Terraform uses to replace the variable references inside a configuration file.

```yaml
ami = "ami-0742a572c2ce45ebf"
instance_type = "t2.micro"
```

5\. Finally, run the `tree` command below to verify all of the required files are contained in the _terraform-workspace-demo_ folder.

```bash
tree terraform-workspace-demo
```

Below, you will notice that another directory (`terraform.tfstate.d`) contains two folders (**workspace1** and **workspace2**). Terraform automatically creates these folders as you create the Terraform workspace.

![Verifying the folder structure required by Terraform workspace](https://adamtheautomator.com/wp-content/uploads/2021/11/image-371.png)

Verifying the folder structure required by Terraform workspace

## Creating Two AWS EC2 Instances in Different Workspaces

You now have the Terraform workspaces and Terraform configuration files ready, so the next step is to launch AWS EC2 instances. You’ll create and launch two AWS instances within two different workspaces.

1\. Run the command below to verify the Terraform workspace before you create resources in the AWS account.

```bash
# Displays the current workspace 
terraform workspace show
```

Below, you can see the current workspace is workspace2.

![Displaying the current workspace](https://adamtheautomator.com/wp-content/uploads/2021/11/image-372.png)

Displaying the current workspace

2\. Next, run each command below to navigate to `~\\terraform-workspace-demo` directory and initialize Terraform. Terraform initializes the plugins and providers which are required to work with resources.

> _Terraform typically uses a three-stage approach, which is [terraform init](https://www.terraform.io/docs/cli/commands/init.html) → [terraform plan](https://www.terraform.io/docs/cli/commands/plan.html) → [terraform apply](https://www.terraform.io/docs/cli/commands/apply.html)._

```bash
cd ~\terraform-workspace-demo
## Initializing Terraform 
terraform init
## Gives you an overview of which resources will be provisioned in your infrastructure
terraform plan
## Tells Terraform to provision the instance
terraform apply
```

Related:[Creating the AWS EKS Cluster with a Terraform Configuration](https://adamtheautomator.com/terraform-eks-module/)

If all goes well after running `terraform apply` command, you’ll get the **Apply complete! Resources: 1 added, 0 changed, 0 destroyed** message shown below.

Note the output instance ID as you’ll need them to verify if they exist in the AWS Management Console later. Below, you can see the command displays the output’s EC2 instance ID.\`

![Creating the AWS instance in Workspace2](https://adamtheautomator.com/wp-content/uploads/2021/11/image-373.png)

**Creating the AWS instance in Workspace2**

3\. Now, run the command below to switch workspace from workspace2 to workspace1. Replace `workspace-name` with the workspace’s name you want to switch with the current workspace.

```bash
terraform workspace select workspace-name
```

![switch workspace from workspace2 to workspace1](https://adamtheautomator.com/wp-content/uploads/2021/11/image-374.png)

switch workspace from workspace2 to workspace1

Finally, rerun the commands below as you previously did (step two). This time, Terraform creates the instance in a different workspace (workspace1).

![Creating the AWS instance in Workspace1](https://adamtheautomator.com/wp-content/uploads/2021/11/image-375.png)

**Creating the AWS instance in Workspace1**

## Verifying the Resources Exist in AWS Console

By now, you should have created all the EC2 instances launched with Terraform. But you can verify if all the EC2 instances exist by manually checking in the AWS Management Console.

1\. Open your favorite web browser and log on to the [AWS Management Console](https://aws.amazon.com/console/).

2\. Now click on the search bar at the top, search for ‘EC2’, and click on the **EC2** menu item below.

![Opening an EC2 console.](https://adamtheautomator.com/wp-content/uploads/2021/11/image-376.png)

Opening an EC2 console.

Finally, on the EC2 page, verify that all IDs are the same ones that Terraform returned earlier under the “Creating two AWS EC2 instances in different Workspaces” section.

![Checking all the instances in the AWS console.](https://adamtheautomator.com/wp-content/uploads/2021/11/image-377.png)

Checking all the instances in the AWS console.

## Conclusion

In this tutorial, you’ve learned how to use Terraform workspace to launch unique resources simultaneously using the same Terraform configuration. Now you can work on different workspaces without messing things up!

With this knowledge, take what you have learned and use Terraform to manage your entire infrastructure!

Share this article

[Share on X](https://twitter.com/intent/tweet?url=https%3A%2F%2Fadamtheautomator.com%2Fterraform-workspace%2F&text=Demonstrating%20Unique%20Instances%20with%20Terraform%20Workspace)[Share on Facebook](https://www.facebook.com/sharer/sharer.php?u=https%3A%2F%2Fadamtheautomator.com%2Fterraform-workspace%2F)[Share on LinkedIn](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fadamtheautomator.com%2Fterraform-workspace%2F)

## Related Posts

![](https://adamtheautomator.com/wp-content/uploads/2024/03/terragrunt.png)

### [Keeping Terraform Maintainable with Terragrunt](/terragrunt/)

Discover Terragrunt and embrace the power to simplify Terraform workflows—Ensure maintainability and organization of your infrastructure today!

![](https://adamtheautomator.com/wp-content/uploads/2023/02/terraform-best-practices.jpg)

### [Ensuring Success by Learning Terraform Best Practices](/terraform-best-practices/)

Discover Terraform best practices and ensure that your DevOps environment is being utilized safely and securely to it’s fullest potential!

![](https://adamtheautomator.com/wp-content/uploads/2022/08/Which-Tool-Terraform-vs-Cloudformation-in-the-Fight-for-AWS.jpg)

### [Which Tool? Terraform vs. Cloudformation in the Fight for AWS](/terraform-vs-cloudformation/)

Learn which tool, Terraform vs Cloudformation is right for infrastructure as configuration with AWS in this ATA Learning tutorial!

## Categories

*   [IT Ops](/category/it-ops/)
*   [Cloud](/category/cloud/)
*   [DevOps](/category/devops/)
*   [Home Ops](/category/home-ops/)
*   [Information Security](/category/infosec/)
*   [Software Development](/category/software-development/)

## Site

*   [Home](/)
*   [Tutorials](/tutorials/)
*   [Instructors](/author/)
*   [Advertising](/advertising/)
*   [Recommended Resources](/resources/)
*   [About Adam](/about-adam/)

Copyright 2026© ATA Learning | [Privacy Policy](/privacy/)
