Terraform多个状态文件最佳实践示例 [英] Terraform Multiple State Files Best Practice Examples

查看:470
本文介绍了Terraform多个状态文件最佳实践示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Terraform构建我们的AWS环境,但是遇到了扩展问题.我有一个仅包含我想在构建环境时重复使用的模块的存储库,还有一个仅用于处理这些模块的实际实现的存储库.

我知道HashiCorp的Github页面上有一个示例,但是在那里,每个环境都是一个状态文件.我想拆分环境,但是每个环境中都有多个状态文件.当状态文件变大时,应用小的更新将花费太长时间.

在每个示例中,我都看到了使用多个状态文件的情况,Terraform文件极其干燥且不理想.

我希望能够在环境之间具有不同的变量值,但具有相同的配置.

有人做过这样的事情吗?我想念什么吗?我有点沮丧,因为每个Terraform示例都无法实现规模化,这使像我这样的n00b很难正确地走上正确的道路.任何帮助或建议,我们将不胜感激!

解决方案

环境的想法对不同的人和组织往往意味着不同的含义.

对于某些人来说,它只是创建一些基础结构的多个副本-可能只是临时的,或者可能是长期存在的-以便在一个环境中进行测试和试验而不会影响另一个(可能是 production )环境

对于其他人来说,它是部署架构中的一流构造,环境充当了用于部署其他应用程序和基础结构的容器.在这种情况下,通常会有多个单独的Terraform配置,每个配置在每个环境中都有一组资源,可以共享数据以从较小的部分创建更大的系统.

Terraform具有称为 State Environments 通过在给定配置中同时存在多个命名状态,并允许用户使用terraform env命令在它们之间切换来将更改操作集中在特定状态上,从而满足了这些用例中的第一个用例.

状态环境功能 不足以用于第二个用例,因为它仅在单个配置中处理多个状态.但是,它可以与其他Terraform功能结合使用,利用${terraform.env}插值值处理差异,以允许单个配置内的多个状态环境与另一配置内的相应状态环境集进行交互./p>


我的系列文章中描述了一种相对规模"的方法(相对而言)单独 Terraform配置创建,每个环境不同的配置方式有所不同,但是它们每个都以标准方式公开数据,以允许多个应用程序(每个都使用 State Environments 功能)使用以下命令针对每个环境部署一次: 相同配置.

这种折衷导致环境配置之间的某些重复-可以通过使用 Terraform模块以在它们之间共享模式-但它们随后成为基础,允许通用化并多次重复部署其他配置,而无需重复执行这些操作.

I am trying to build out our AWS environments using Terraform but am hitting some issues scaling. I have a repository of just modules that I want to use repeatedly when building my environments and a second repository just to handle the actual implementations of those modules.

I am aware of HashiCorp's Github page that has an example but there, each environment is one state file. I want to split environments out but then have multiple state files within each environment. When the state files get big, applying small updates takes way too long.

Every example I've seen where multiple state files are used, the Terraform files are extremely un-DRY and not ideal.

I would prefer to be able to have different variable values between environments but have the same configuration.

Has anyone ever done anything like this? Am I missing something? I'm a bit frustrated because every Terraform example is never at scale and it makes it hard for n00b such as myself to start down the right path. Any help or suggestions is very much appreciated!

解决方案

The idea of environment unfortunately tends to mean different things to different people and organizations.

To some, it's simply creating multiple copies of some infrastructure -- possibly only temporary, or possibly long-lived -- to allow for testing and experimentation in one without affecting another (probably production) environment.

For others, it's a first-class construct in a deployment architecture, with the environment serving as a container into which other applications and infrastructure are deployed. In this case, there are often multiple separate Terraform configurations that each have a set of resources in each environment, sharing data to create a larger system from smaller parts.

Terraform has a feature called State Environments that serves the first of these use-cases by allowing multiple named states to exist concurrently for a given configuration, and allowing the user to switch between them using the terraform env commands to focus change operations on a particular state.

The State Environments feature alone is not sufficient for the second use-case, since it only deals with multiple states in a single configuration. However, it can be used in conjunction with other Terraform features, making use of the ${terraform.env} interpolation value to deal with differences, to allow multiple state environments within a single configuration to interact with a corresponding set of state environments within another configuration.


One "at scale" approach (relatively-speaking) is described in my series of articles Terraform Environment+Application Pattern, which describes a generalization of a successful deployment architecture with many separate applications deployed together to form an environment.

In that pattern, the environments themselves (which serve as the "container" for applications, as described above) are each created with a separate Terraform configuration, allowing each to differ in the details of how it is configured, but they each expose data in a standard way to allow multiple applications -- each using the State Environments feature -- to be deployed once for each environment using the same configuration.

This compromise leads to some duplication between the environment configurations -- which can be mitgated by using Terraform modules to share patterns between them -- but these then serve as a foundation to allow other configurations to be generalized and deployed multiple times without such duplication.

这篇关于Terraform多个状态文件最佳实践示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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