我应该为整个应用程序堆栈创建简单的模块还是模块? [英] Should I be creating simple modules or modules for an entire app stack?

查看:19
本文介绍了我应该为整个应用程序堆栈创建简单的模块还是模块?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们以下面的模块为例.

Let's take the following module as an example.

https://registry.terraform.io/modules/terraform-aws-modules/ec2-instance/aws/1.0.1

如果我的应用程序堆栈需要一些 Web 服务器、一个 ELB 和一个 mysql 服务器,我计划使用模块,这样我就不会重复我的代码.我的问题是,我是否应该有简单的模块,例如上面列出的一个(然后一个用于 ELB,一个用于 MySQL),然后通过在 env-dev/app/apollo/中制作一个堆栈来组合所有这些main.tf?

If my app stack requires a few web servers, an ELB and a mysql server, I plan on utilizing modules so that I'm not repeating my code. My question is, should I have simple modules such as the one listed above (then one for ELB and one for MySQL), and then combine all that by making a stack out of it within env-dev/app/apollo/main.tf?

示例结构:

├── env-dev
│   └── app
│       └── apollo
│           └── main.tf
├── env-test
├── global
├── mgmt
└── modules
    ├── ec2-elb
    └── ec2-instance
        ├── LICENSE
        ├── main.tf

我的 env-dev/app/apollo/main.tf 应该获取这两个模块来构建应用程序,还是应该创建一个模块来构建 ELB 的实例?

Should my env-dev/app/apollo/main.tf source the two modules to build the app or should I be creating a module to build out the instances along with ELB?

推荐答案

一般来说,一个模块只有在它对使用施加一些限制(例如应用组织特定的约定)或者它代表许多资源(从您的系统的角度来看)表现为可以多次实例化的原子单元.

Generally-speaking, a module should be created only if it either imposes some restrictions on use (to apply organization-specific conventions, for example) or if it represents a number of resources that behave (from the perspective of your system) as an atomic unit that may be instantiated multiple times.

您链接到的注册表模块不是可重用模块的一个很好的例子,因为它只是一个现有资源的包装器,精确地传递输入参数并且没有添加超出 resource<可以表达的值/code> 直接阻塞.该模块更多地用作复制到根模块的入门"示例,而不是您直接调用的东西,因为它不会将抽象级别提高到高于它所包装的资源.

The registry module you linked to is not a good example of a reusable module because it is merely a wrapper around an existing resource, passing through input arguments exactly and adding no value beyond what could be expressed with the resource block directly. That module serves more as a "getting started" example to copy into your root module, rather than something you'd call directly, since it doesn't raise the level of abstraction any higher than the resource it wraps.

有几种不同的方法来处理 Terraform 配置的结构,但是在对简单的事物进行建模时,我建议从单个根模块开始,其中包含直接的 resource 块.随着时间的推移,您可能会注意到某些资源被有效地复制粘贴(稍作调整)在同一配置中或跨多个配置;那时我会考虑将这些资源分解到可以多次实例化的模块中.

There are a few different ways to approach the structure of a Terraform configuration, but when modelling simple things I would suggest starting with a single root module with direct resource blocks inside of it. Over time, you may notice that certain resources are effectively being copy-pasted (with minor adjustments) either within the same configuration or across multiple configurations; at that point I would consider factoring out those resources into modules that can be instantiated multiple times.

对于应该将多少东西打包到一个模块中,没有明确的答案.就像编程语言中的函数一样,通常最好选择可以组合在一起的小构建块,而不是大而不灵活的单元.但是,到底在哪里划线取决于具体情况.要做出决定,请考虑您期望需要支持的不同排列,以及哪些事物似乎自然地"属于一起.

There is no definitive answer to how many things should be packed together into a module. Just like with functions in a programming language, it's generally better to err on the side of small building blocks that you can compose together, rather than large and inflexible units. However, exactly where to draw the line is something that depends on the situation. To decide, think about the different permutations you expect to need to support, and which things seem to "naturally" belong together.

如果您第一次没有做对,那么这并不是世界末日,因为您可以在重构配置时使用 terraform state mv 命令在模块之间移动资源状态.

If you don't get it right the first time then it's not the end of the world because you can use the terraform state mv command to move resource state between modules as you refactor your configuration.

这篇关于我应该为整个应用程序堆栈创建简单的模块还是模块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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