Terraform for Google Cloud Essential Guide by Bernd Nordhausen

Terraform for Google Cloud Essential Guide by Bernd Nordhausen

Author:Bernd Nordhausen
Language: eng
Format: epub
Publisher: Packt
Published: 2023-11-15T00:00:00+00:00


Using a directory structure to manage environments

Note

The code for this section is under the chap05/directory-structure directory in the GitHub repo of this book.

We saw that using workspaces with Google Cloud projects is an easy way to manage nearly identical environments. All we have to do is to create an additional workspace and supply different values in our variable definitions file. However, one of the major limitations of this approach is that the environments must be nearly identical.

Using our example, let’s say we want to have two small servers in the development environment, but in the production environment, we want to have two medium and one large server. Provisioning two servers of different sizes is easy, as we can specify different server sizes in the variable definitions file. However, having a third server of a different configuration only in production is more challenging. We could define a combination of conditional expressions and a count meta-argument, as follows:

count = (terraform.workspace == "prod") ? 1 : 0

However, that can get overly complex very quickly. A better approach is to have different subdirectories for each environment and use modules to keep our code DRY. So, if we want to have two environments, our directory structure looks like the following:

. ├── dev │ ├── backend.tf │ ├── main.tf │ ├── outputs.tf │ ├── provider.tf │ ├── terraform.tfvars │ └── variables.tf ├── modules │ └── server │ ├── main.tf │ ├── outputs.tf │ ├── startup.sh │ └── variables.tf └── prod ├── backend.tf ├── main.tf ├── outputs.tf ├── provider.tf ├── terraform.tfvars └── variables.tf

We have three subdirectories, dev, prod, and modules. Both dev and prod contain the usual files, including their own backend and variable definitions files. The modules subdirectory contains the configuration files for the server module like before.

So, looking at the two main.tf files, we can see how we can define two small servers for dev and three servers of different sizes for prod:

chap05/directory-structure/dev/main.tf

module "server1" { source = "../modules/server" name = "${var.server_name}-1" machine_size = "small" environment = var.environment } module "server2" { source = "../modules/server" name = "${var.server_name}-2" machine_size = "small" environment = var.environment }

In prod/main.tf we change some variable settings and add one additional module declaration for the third server:

chap05/directory-structure/prod/main.tf

module "server1" { source = "../modules/server" name = "${var.server_name}-1" machine_size = "medium" environment = var.environment } module "server2" { source = "../modules/server" name = "${var.server_name}-2" machine_size = "medium" environment = var.environment } module "server3" { source = "../modules/server" name = "${var.server_name}-3" machine_size = "large" environment = var.environment }

With these files in place, we can now run Terraform in each subdirectory specifying the appropriate project ID as an argument:

$ cd dev $ terraform init $ terraform apply -var project_id=[DEV-PROJECT-ID] $ cd ../prod $ terraform init $ terraform apply -var project_id=[PROD-PROJECT-ID]

This structure allows us to have different configurations between the environments yet keep our code DRY. This approach requires the effective use of modules, whether they are stored locally or remotely in Cloud Storage or a repository. When using separate directories, we often need to share state data between the configurations in the different subdirectories.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Popular ebooks
Kubernetes in Production Best Practices by Aly Saleh & Murat Karslioglu(6357)
Optimizing Microsoft Azure Workloads by Rithin Skaria(5909)
Kubernetes in Production Best Practices by Aly Saleh and Murat Karslioglu(5634)
Cloud Computing Demystified for Aspiring Professionals by David Santana(4729)
Zed Attack Proxy Cookbook by Ryan Soper & Nestor N Torres & Ahmed Almoailu(4177)
Google Cloud for Developers: Write, migrate, and extend your code by leveraging Google Cloud by Hector Parra Martinez(3355)
Mastering Cyber Intelligence by Jean Nestor M. Dahj;(3255)
AWS Observability Handbook by Phani Kumar Lingamallu & Fabio Braga de Oliveira(2978)
The Road to Azure Cost Governance by Paola E. Annis Giuliano Caglio(2794)
Microsoft 365 Fundamentals Guide by Gustavo Moraes and Douglas Romão(2230)
Agile Security Operations: Engineering for Agility in Cyber Defense, Detection, and Response by Hinne Hettema(1574)
Cloud Identity Patterns and Strategies: Design enterprise cloud identity models with OAuth 2.0 and Azure Active Directory by Giuseppe Di Federico Fabrizio Barcaroli(1545)
Bootstrapping Service Mesh Implementations with Istio by Anand Rai(1429)
The Road to Azure Cost Governance: Techniques to tame your monthly Azure bill with a continuous optimization journey for your apps by Paola E. Annis Giuliano Caglio(1245)
Agile Security Operations: Engineering for agility in cyber defense, detection, and response by Hinne Hettema(1183)
Zed Attack Proxy Cookbook: Hacking tactics, techniques, and procedures for testing web applications and APIs by Ryan Soper Nestor N Torres Ahmed Almoailu(1167)
Linux Administration Best Practices: Practical Solutions to Approaching the Design and Management of Linux Systems by Scott Alan Miller(1119)
DevSecOps in Practice with VMware Tanzu: Build, run, and manage secure multi-cloud apps at scale on Kubernetes with the Tanzu portfolio by Parth Pandit Robert Hardt(1058)
Terraform for Google Cloud Essential Guide by Bernd Nordhausen(865)
Becoming KCNA Certified by Dmitry Galkin(845)