-
Notifications
You must be signed in to change notification settings - Fork 0
/
07._Modules.txt
127 lines (105 loc) · 3.14 KB
/
07._Modules.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
Content-Type: text/x-zim-wiki
Wiki-Format: zim 0.6
Creation-Date: 2023-10-26T10:55:08+02:00
====== 07. Modules ======
Way to organize source code, allowing reusability.
===== Module sources =====
See: [[https://developer.hashicorp.com/terraform/language/modules/sources|Module Sources]]
'''
# On the Terraform registry repo
module "myVpc1" {
source = "terraform-aws-modules/vpc/aws"
version = "5.1.2"
}
'''
'''
# On a Git repository 'git::https://' or 'git::ssh://'
# 'ref' can reference a tag name, a hash or a branch
module "myVpc2" {
source = "git::https://corporate-gitlab.com/tf-aws-vpc.git?ref=v1.2.0"
}
'''
'''
# Local module ALWAYS starts with './' or '../'
module "instance1" {
source = "./module-ec2"
}
'''
→ A lot of sources can be used, such as S3 (''s3::https://''), GCS buckets (''gcs::https://''), GitHub (''github.com/...''),...
===== Key points =====
**A module can be instantiated several times, with a different module instance name**
'''
module "myVpc1" {
source = "terraform-aws-modules/vpc/aws"
version = "5.1.2"
}
module "myVpc2" {
source = "terraform-aws-modules/vpc/aws"
version = "5.1.2"
}
'''
**Parameters can be provided to a module instance. They are mapped to the module variables**
'''
module "myVpc1" {
source = "terraform-aws-modules/vpc/aws"
version = "5.1.2"
# input variables
var1 = "val1"
var2 = "val2"
}
'''
**A module can send informations back to its caller using ''output''**
'''
# inside the module
output "foo_name" {
description = "Name of ssth"
value = my_object.name
}
'''
'''
# inside the caller
module "module_instance1" {
source = "../module/my_module"
}
# later, use module.module_instance1.foo_name
'''
===== Standard module structure =====
See: [[https://developer.hashicorp.com/terraform/language/modules/develop/structure|Module structure]]
'''
$ tree minimal-module/
.
├── README.md
├── main.tf
├── variables.tf
├── outputs.tf
'''
'''
$ 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/
│ ├── .../
'''
===== Terraform registry =====
See: https://registry.terraform.io/browse/modules
**Requirements for Publishing Module**
* **GitHub:** The module must be on GitHub and must be a public repo. This is only a requirement for the public registry.
* **Named:** Module repositories must use this three-part name format ''terraform-<PROVIDER>-<NAME>''
* **Repository description:** The GitHub repository description is used to populate the short description of the module.
* **Standard module structure:** The module must adhere to the standard module structure.
* **x.y.z tags for releases:** The registry uses tags to identify module versions. Release tag names must be a semantic version, which can optionally be prefixed with a ''v''. For example, ''v1.0.4'' or ''0.9.2''