【发布时间】:2025-12-10 18:25:01
【问题描述】:
我对 terraform 模块有点困惑。我得到了使用模块的代码可重用性的想法。您在目录中创建的任何代码本质上都是一个模块。我正在努力解决的问题是您的模块化程度如何?每个模块是否应该包含单独的资源信息,如下所示:
/modules/security_groups/ssh/main.tf <-- only has one aws_security_group resource
/modules/security_groups/http-80/main.tf <-- only has one aws_security_group resource
/modules/security_groups/http-443/main.tf <-- only has one aws_security_group resource
/modules/launch_configuration/main.tf <-- only has one aws_launch_configuration resource
/modules/auto_scaling_group/main.tf <-- only has one auto_scaling_group resource
/modules/instances/main.tf <-- only has one instance resource
....
/stage/vpc/main.tf
/stage/services/frontend-app/salon.tf <-- source multiple /modules/... and vpc
/prod/vpc/main.tf
/prod/services/frontend-app/salon.tf <-- source multiple /modules/... and vpc
或者每个模块是否应该包含资源信息的组合。以这个目录结构为例。
/modules/salon-app/main.tf <-- has more than one resource aws_security_group, auto_scaling_group, launch_configuration_group ...etc
....
/stage/vpc/main.tf
/stage/services/frontend-app/salon.tf <-- source /modules/salon-app/.. only once
/prod/vpc/main.tf
/prod/services/frontend-app/salon.tf <-- source /modules/salon-app/.. only once
基本上,您是将 aws 资源捆绑到一个大文件中,还是将它们拆分为每个资源的单独文件,然后单独获取它们?
【问题讨论】:
-
我有同样的问题,我最终将资源组合成一个模块(你的最后一个例子)。看我的问题。 *.com/questions/46611713/…
-
谢谢!我在网上阅读的所有内容、博客和各种 terraform 代码示例都指向将资源合并到一个文件中。我仍然很困惑如何为不同的环境真正模块化它,这样你只需要调整一些参数而不必复制代码,但我想看看它是如何工作的。也许我试图做太多我不知道的模块化......
标签: module terraform infrastructure