Creating Modules
转自官方文档,主要说明如何创建模块 https://www.terraform.io/docs/modules/index.html
A module is a container for multiple resources that are used together. Modules can be used to create lightweight abstractions, so that you can describe your infrastructure in terms of its architecture, rather than directly in terms of physical objects.
The .tf files in your working directory when you run terraform plan or terraform apply together form the rootmodule. That module may call other modules and connect them together by passing output values from one to input values of another.
To learn how to use modules, see the Modules configuration section. This section is about creating re-usable modules that other configurations can include using module blocks.
»Module structure
Re-usable modules are defined using all of the same configuration language concepts we use in root modules. Most commonly, modules use:
- Input variables to accept values from the calling module.
- Output values to return results to the calling module, which it can then use to populate arguments elsewhere.
- Resources to define one or more infrastructure objects that the module will manage.
To define a module, create a new directory for it and place one or more .tf files inside just as you would do for a root module. Terraform can load modules either from local relative paths or from remote repositories; if a module will be re-used by lots of configurations you may wish to place it in its own version control repository.
Modules can also call other modules using a module block, but we recommend keeping the module tree relatively flat and using module composition as an alternative to a deeply-nested tree of modules, because this makes the individual modules easier to re-use in different combinations.
»When to write a module
In principle any combination of resources and other constructs can be factored out into a module, but over-using modules can make your overall Terraform configuration harder to understand and maintain, so we recommend moderation.
A good module should raise the level of abstraction by describing a new concept in your architecture that is constructed from resource types offered by providers.
For example, aws_instance and aws_elb are both resource types belonging to the AWS provider. You might use a module to represent the higher-level concept "HashiCorp Consul cluster running in AWS" which happens to be constructed from these and other AWS provider resources.
We do not recommend writing modules that are just thin wrappers around single other resource types. If you have trouble finding a name for your module that isn't the same as the main resource type inside it, that may be a sign that your module is not creating any new abstraction and so the module is adding unnecessary complexity. Just use the resource type directly in the calling module instead.
»Standard Module Structure
The standard module structure is a file and directory layout we recommend for reusable modules distributed in separate repositories. Terraform tooling is built to understand the standard module structure and use that structure to generate documentation, index modules for the module registry, and more.
The standard module structure expects the layout documented below. The list may appear long, but everything is optional except for the root module. Most modules don't need to do any extra work to follow the standard structure.
Root module. This is the only required element for the standard module structure. Terraform files must exist in the root directory of the repository. This should be the primary entrypoint for the module and is expected to be opinionated. For the Consul module the root module sets up a complete Consul cluster. It makes a lot of assumptions however, and we expect that advanced users will use specific nested modules to more carefully control what they want.
README. The root module and any nested modules should have README files. This file should be named
READMEorREADME.md. The latter will be treated as markdown. There should be a description of the module and what it should be used for. If you want to include an example for how this module can be used in combination with other resources, put it in an examples directory like this. Consider including a visual diagram depicting the infrastructure resources the module may create and their relationship.
The README doesn't need to document inputs or outputs of the module because tooling will automatically generate this. If you are linking to a file or embedding an image contained in the repository itself, use a commit-specific absolute URL so the link won't point to the wrong version of a resource in the future.
LICENSE. The license under which this module is available. If you are publishing a module publicly, many organizations will not adopt a module unless a clear license is present. We recommend always having a license file, even if it is not an open source license.
main.tf,variables.tf,outputs.tf. These are the recommended filenames for a minimal module, even if they're empty.main.tfshould be the primary entrypoint. For a simple module, this may be where all the resources are created. For a complex module, resource creation may be split into multiple files but any nested module calls should be in the main file.variables.tfandoutputs.tfshould contain the declarations for variables and outputs, respectively.Variables and outputs should have descriptions. All variables and outputs should have one or two sentence descriptions that explain their purpose. This is used for documentation. See the documentation for variable configuration and output configuration for more details.
Nested modules. Nested modules should exist under the
modules/subdirectory. Any nested module with aREADME.mdis considered usable by an external user. If a README doesn't exist, it is considered for internal use only. These are purely advisory; Terraform will not actively deny usage of internal modules. Nested modules should be used to split complex behavior into multiple small modules that advanced users can carefully pick and choose. For example, the Consul module has a nested module for creating the Cluster that is separate from the module to setup necessary IAM policies. This allows a user to bring in their own IAM policy choices.
If the root module includes calls to nested modules, they should use relative paths like ./modules/consul-cluster so that Terraform will consider them to be part of the same repository or package, rather than downloading them again separately.
If a repository or package contains multiple nested modules, they should ideally be composable by the caller, rather than calling directly to each other and creating a deeply-nested tree of modules.
- Examples. Examples of using the module should exist under the
examples/subdirectory at the root of the repository. Each example may have a README to explain the goal and usage of the example. Examples for submodules should also be placed in the rootexamples/directory.
Because examples will often be copied into other repositories for customization, any module blocks should have their source set to the address an external caller would use, not to a relative path.
A minimal recommended module following the standard structure is shown below. While the root module is the only required element, we recommend the structure below as the minimum:
$ tree minimal-module/
.
├── README.md
├── main.tf
├── variables.tf
├── outputs.tf
A complete example of a module following the standard structure is shown below. This example includes all optional elements and is therefore the most complex a module can become:
$ tree complete-module/
.
├── README.md
├── main.tf
├── variables.tf
├── outputs.tf
├── ...
├── modules/
│ ├── nestedA/
│ │ ├── README.md
│ │ ├── variables.tf
│ │ ├── main.tf
│ │ ├── outputs.tf
│ ├── nestedB/
│ ├── .../
├── examples/
│ ├── exampleA/
│ │ ├── main.tf
│ ├── exampleB/
│ ├── .../
Creating Modules的更多相关文章
- Strom的配置安装
1.准备环境 1.1配置列表 配置项 版本信息 OS Red Hat Enterprise Linux Server release 5.5 (Tikanga) IP 192.168.1.191/ ...
- Joomla
joomla Joomla实际有两个开源的东西: 1.Joomla内容管理系统即JoomlaCMS(Content Management System, CMS).它是网站的一个基础管理平台.几乎适合 ...
- python环境搭建-Linux系统下python2.6.6升级python3.5.2步骤
[root@template ~]# python -v # /usr/lib64/python2.6/encodings/utf_8.pyc matches /usr/lib64/python2.6 ...
- Zabbix4.0报警配置-企业微信报警
一:前期准备 1.1:企业号注册 https://qy.weixin.qq.com/ 微信调用接口说明 http://qydev.weixin.qq.com/wiki/index.php?title ...
- centos7升级Python2.x到3.x
CentOS 7 中默认安装了 Python,版本比较低(2.7.5),为了使用新版 3.x,需要对旧版本进行升级.由于很多基本的命令.软件包都依赖旧版本,比如:yum.所以,在更新 Python 时 ...
- crosss compile VLC with OpenMAX on ARM board(RockChip RK3399),in order to use Hard Acceleration when decode video
reference:http://www.x90x90x90.com/en/raspberry-pi-3-howto-compile-vlc-with-hardware-acceleration/ 1 ...
- Python学习之路 (一)开发环境搭建
前言 python3应该是Python的趋势所在,当然目前争议也比较大,这篇随笔的主要目的是记录在centos6.7下搭建python3环境的过程 以及碰到的问题和解决过程. 另外,如果本机安装了py ...
- CentOS 6编译安装yum和配置常用的yum源
安装环境:VPS,CentOS 6 + devel包 一.安装相应的软件 1.安装python 下载Python源码包 [root@akinlau ~]# wget http://www.python ...
- Centos7手动编译安装Python3
Python3的安装方式非常的简单,首先去到Python下载目录下载想要的Python包,这里我选择了3.6.5版本 $ wget https://www.python.org/ftp/python/ ...
随机推荐
- MicroOrm.Dapper.Repositories 的使用
https://github.com/geffzhang/MicroOrm.Dapper.Repositories 1.特性标记都是要引用: System.ComponentModel.DataAnn ...
- mybatis 插入空值时报错 TypeException
报错内容:nested exception is org.apache.ibatis.type.TypeException: Could not set parameters for mapping: ...
- JavaScript作用域及作用域链详解、声明提升
相信大家在入门JavaScript这门语言时对作用域.作用域链.变量声明提升这些概念肯定会稀里糊涂,下面就来说说这几个 Javascript 作用域 在 Javascript 中,只有局部作用域和全局 ...
- Office 365 企业应用以及服务概览 分享记录
博客地址:http://blog.csdn.net/FoxDave 分享时间: 2017年9月14日 分享地点: 部门内部 参与人数: 16人 分享内容: 讲解微软MVP项目计划的相关内容:讲解O ...
- SharePoint Framework 把你的客户端web部件部署到经典的SharePoint页面
博客地址:http://blog.csdn.net/FoxDave 本节介绍如何把客户端web部件部署到SharePoint服务端页面.我们将继续使用之前的工程. 将HelloWorld的web部 ...
- 从SharePoint 2013迁移到SharePoint Online - 评估工具
博客地址:http://blog.csdn.net/FoxDave 今天想跟大家分享一款从SharePoint 2013迁移到SharePoint Online时的评估工具:SharePoint ...
- this 指向问题, 三个例子
'use strict'; var a = 20; function foo () { var a = 1; var obj = { a: 10, c: this.a + 20, fn: functi ...
- 理解mpvue的生命周期
mpvue是美团基于vue开发的一个开发小程序的框架,从而以vue的语法来开发小程序.在生命周期上,mpvue同时支持了vue的生命周期和小程序的生命周期,这可能让新上手的同学费解.这篇文章就来讲讲m ...
- .Net 3.5 安装教程
虽然说.NET Framework 3.5 SP1已经不是最新版的,但是还有大部分用Xp系统的人还得用它.不过我们使用的系统一般都是Ghost版Xp,Ghost大部分都做过尽量的简化,去除了不必要的组 ...
- JAVA测试编程
本周我们上JAVA课的时候进行了一次测试,本次测试以模拟中国工商银行自助机ATM的功能为大致要求,要求我们进行编写调试,以求达到试题要求. 测试要求我们能模拟ATM的功能设计,当用户插卡后显示,输入密 ...