理想的Terraform工作区项目结构 [英] Ideal terraform workspace project structure

查看:180
本文介绍了理想的Terraform工作区项目结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想设置Terraform来管理开发/阶段/产品环境.在所有环境中,基础结构都是相同的,但是在每个环境中,变量都有差异.

既然Terraform 0.10中引入了工作空间,理想的Terraform项目结构是什么样的?在命名/标记基础结构时如何引用工作区?

解决方案

我不建议将工作空间(以前称为环境")用于静态环境,因为它们增加了相当多的复杂性,并且更难跟踪./p>

您可以在所有环境中使用单一文件夹结构,使用工作空间来分隔环境,然后使用基于工作空间的条件值来设置差异.在实践中(尤其是在导致嵌套三元语句的两个以上环境中),您可能会发现很难管理.

相反,我仍然主张为每个静态环境使用单独的文件夹,并使用符号链接保留所有.tf文件在所有环境中都相同,并使用terraform.tfvars文件在每个环境中提供任何差异.

我建议为动态环境(例如短暂的评论/实验室环境)使用工作空间,因为这样可以提供很大的灵活性.我目前正在使用它们在Gitlab CI中创建审阅环境,因此每个分支都可以有一个可选部署的审阅环境,可用于手动集成或探索性测试.

I'd like to setup Terraform to manage dev/stage/prod environments. The infrastructure is the same in all environments, but there are differences in the variables in every environment.

What does an ideal Terraform project structure look like now that workspaces have been introduced in Terraform 0.10? How do I reference the workspace when naming/tagging infrastructure?

解决方案

I wouldn't recommend using workspaces (previously 'environments') for static environments because they add a fair bit of complexity and are harder to keep track of.

You could get away with using a single folder structure for all environments, use workspaces to separate the environments and then use conditional values based on the workspace to set the differences. In practice (and especially with more than 2 environments leading to nested ternary statements) you'll probably find this difficult to manage.

Instead I'd still advocate for separate folders for every static environment and using symlinks to keep all your .tf files the same across all environments and a terraform.tfvars file to provide any differences at each environment.

I would recommend workspaces for dynamic environments such as short lived review/lab environments as this allows for a lot of flexibility. I'm currently using them to create review environments in Gitlab CI so every branch can have an optionally deployed review environment that can be used for manual integration or exploratory testing.

这篇关于理想的Terraform工作区项目结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆