惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Stack Overflow Blog
Stack Overflow Blog
云风的 BLOG
云风的 BLOG
G
Google Developers Blog
J
Java Code Geeks
C
Check Point Blog
Last Week in AI
Last Week in AI
Microsoft Azure Blog
Microsoft Azure Blog
Blog — PlanetScale
Blog — PlanetScale
月光博客
月光博客
Vercel News
Vercel News
The GitHub Blog
The GitHub Blog
L
LangChain Blog
有赞技术团队
有赞技术团队
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
博客园 - 司徒正美
IT之家
IT之家
Martin Fowler
Martin Fowler
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
T
Tailwind CSS Blog
U
Unit 42
Jina AI
Jina AI
Microsoft Security Blog
Microsoft Security Blog
I
InfoQ

Luca Cavallin

AI Engineering for Developers | Blog AI Engineering for Developers Platform Engineering End-to-End | Blog Google Cloud Networking 101: The Comprehensive TLDR | Blog Google Cloud Networking 101: The Comprehensive TLDR Containers Are Not Automatically Secure | Blog Containers Are Not Automatically Secure Watery Stone Beacon | Photography Blue Iceman Suture | Photography Hidden Emerald Pool | Photography Autumn Chapel Pinnacles | Photography A Tour of eBPF in the Linux Kernel: Observability, Security and Networking | Blog A Tour of eBPF in the Linux Kernel: Observability, Security and Networking Shared Violet Pulse | Photography Kubernetes Networking from Packets to Pods | Blog An Overview of Network Protocols | Blog An Overview of Network Protocols A Quick Journey Into the Linux Kernel | Blog A Quick Journey Into the Linux Kernel OpenTelemetry: A Guide to Observability with Go | Blog I'm on the Cillers Podcast Talking About Tech and Hackathons | Blog Yet Another List of Random Opinions on Writing Readable Code and Other Rants | Blog My post about Istio is now on the Istio blog too! | Blog Tropical Jungle Escape | Photography The Istio Service Mesh for People Who Have Stuff to Do | Blog Dreamy Cartoonscape Windmill | Photography Twilight Windmill Reflections | Photography Notes I took while reading "Applied Machine Learning and AI for Engineers" and "Introducing MLOps" | Blog Things I've Learned About Terraform That I Keep Telling People About | Blog Analyzing Unsplash Photo Performance with Python | Blog
How to Use Terraform Workspaces to Manage Environment-bas...
Luca Cavallin · 2021-06-11 · via Luca Cavallin

Workspaces are separate instances of state data that can be used from the same working directory. You can use workspaces to manage multiple non-overlapping groups of resources with the same configuration. To create and switch to a new workspace, after running terraform init, run:

shell

terraform workspace create <name>

To switch to other workspaces, run instead:

shell

terraform workspace select <name>

Use the selected workspace in the Terraform files

The selected workspace is made available in your .tf files via the terraform.workspace variable (it's a string). I like to assign the value to a local variable called environment (since the name of the workspaces and that of the environments match).

locals {
	environment = terraform.workspace
}

Add the environment-based configuration to a new module

Now that you have a variable containing the environment (it's just a string) that you are operating on, you can create a module containing the environment-based configuration. I created a vars module inside the modules directory of my repository, which contains at least the following files:

  • main.tf This file will never changes, as it's only needed to aggregate the variables that will be exported.
locals {
	  environments = {
		"development" : local.development,
		"acceptance" : local.acceptance,
		"production" : local.production
	  }
}
  • outputs.tf This file too, will never change. Here I am defining the output of the vars module so that it can be used from anywhere else in the Terraform repository. The exported values are based on the selected workspace.
output "env" {
	value = local.environments[var.environment]
}
  • variables.tf This file defines the variables required to initialize the module. The outputs of the module are based on the selected workspace (environment), which it needs to be aware of.
variable "environment" {
	description = "The environment which to fetch the configuration for."
	type = string
}
  • development.tf & acceptance.tf & production.tf These files contain the actual values that differ by environment. For example, when setting up a GKE cluster, you might want to use cheap machines for your development node pool, and more performant ones in production. This can be done by defining a node_pool_machine_type value in each environment, like so:
// in development.tf
locals {
	development = {
		node_pool_machine_type = "n2-standard-2"
	}
}
// in acceptance.tf
locals {
	acceptance = {
		node_pool_machine_type = "n2-standard-4"
	}
}
// in production.tf
locals {
	production = {
		node_pool_machine_type = "n2-standard-8"
	}
}

The vars module is now ready to be used from anywhere in the repository, for example in main.tf file. To access the configuration values, initialize the module like so:

#
# Fetch variables based on Environment
#
module "vars" {
	source      = "./modules/vars"
	environment = local.environment
}

The correct configuration will be returned based on the Terraform Workspace ( environment name) being passed to it, and values can be accessed via module.vars.env.<variable-name>. For example:

node_pools = [
	{
		...
		machine_type = module.vars.env.node_pool_machine_type
		...
	}
]

Summary

In this blog post, I have shown you how you can use Terraform Workspaces to switch between different configurations based on the environment you are working on while keeping the setup as clean and simple as possible. Are you interested in more articles about Terraform? Check out How to Deploy ElasticSearch on GKE using Terraform and Helm!