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

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

问题描述

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

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.

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

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.

我见过的每个使用多个状态文件的示例,Terraform 文件都非常不干燥且不理想.

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.

有人做过这样的事吗?我错过了什么吗?我有点沮丧,因为每个 Terraform 示例都没有规模,这让像我这样的 n00b 很难走上正确的道路.非常感谢任何帮助或建议!

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.

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

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 具有一项称为状态环境的功能它通过允许给定配置同时存在多个命名状态,并允许用户使用 terraform env 命令在它们之间切换以将更改操作集中在特定状态上,从而为这些用例中的第一个提供服务.

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.

State Environments 功能单独 不足以用于第二个用例,因为它仅在单个配置中处理多个状态.但是,它可以与其他 Terraform 功能结合使用,利用 ${terraform.env} 插值来处理差异,以允许单个配置中的多个状态环境与另一个配置中对应的一组状态环境.

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.

在我的系列文章 Terraform Environment+Application Pattern,它描述了成功部署架构的概括,其中许多单独的应用程序部署在一起以形成一个环境.

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.

在该模式中,环境自身(如上所述,作为应用程序的容器")每个都使用单独 Terraform 配置创建,允许每个在如何配置的细节上有所不同,但它们都以标准方式公开数据,以允许多个应用程序(每个应用程序都使用 State Environments 功能)为每个环境部署一次,使用相同配置.

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.

这种妥协会导致环境配置之间出现一些重复——这可以通过使用 来缓解Terraform 模块 在它们之间共享模式 - 但这些模块随后作为基础,允许其他配置被概括和部署多次 这样的重复.

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天全站免登陆