Terraform Workspace

 


In this post will see how to create a Terraform Organization and Workspace.

Organizations are shared spaces for one or more teams to collaborate on workspaces.

Workspaces in Terraform are simply independently managed state files. 

A workspace contains everything that Terraform needs to manage a given collection of infrastructure and separate Workspaces function like separate working directories. We can manage multiple environments with Workspaces.

Let's see how to create a workspace.





I have created a workspace called "aws-ec2" and we need to perform the actions on the above screenshot.

1) Do terraform login.
2) Authenticate with the token generated on Terraform portal.
3) Execute the below TF config.










terraform {
  cloud {
    organization = "RSInfominds"

    workspaces {
      name = "aws-ec2"
    }
  }
}

Done with terraform init and apply.

Now, we will create an AWS EC2 instance through Terraform workspace.

terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "4.62.0"
    }
  }
}
# Provider Block
provider "aws" {
  #profile = "default" # AWS Credentials configured via AWS CLI.
  region  = "us-east-1"
}

# Resource Block
resource "aws_instance" "ec2demo" {
  ami           = "ami-0b5eea76982371e91" # Amazon Linux in us-east-1
  instance_type = "t2.micro"
  subnet_id = "subnet-0cfdbb9ad42fcf3c9"
  security_groups = ["sg-0495b116a9de10f0c"]
  tags = {
    "Name" = "webserver1"
  }
}




When you do terraform plan, you will see the below error.



When using AWS with Terraform workspace, export the AWS Access Key and Secret ID as a variable set. Refer to 
https://developer.hashicorp.com/terraform/tutorials/aws-get-started/aws-remote#set-workspace-variables












Once the variable set is configured our TF script should work.


Comments

Popular posts from this blog

SRE/DevOps Syllabus

AWS Code Commit - CI/CD Series Part 1

Docker - Preventing IP overlapping