Course outlines for learning terraform.
expected time | requirements |
---|---|
60 minutes | A computer with Terraform installed. |
Goal: learn how to write Terraform modules.
You’ve learned how to write Terraform code. When you write code, you’ll see that you will be repeating yourself. That can be a waste.
Modules to the rescue; with Terraform modules you can write (small) pieces of Terraform code that other (or yourself) can reuse. This prevents double code and makes maintaining your code much easier.
Imaging this example:
resource "azurerm_resource_group" "rg" {
name = "rg-robertdebock-sbx"
location = "west europe"
}
# Create a virtual network
resource "azurerm_virtual_network" "vnet" {
name = "myTFVnet-robert"
address_space = ["10.0.0.0/16"]
location = "west europe"
resource_group_name = azurerm_resource_group.rg.name
}
There are a lot of double values in the example above:
name
has robert
in it.location
is set in both resources.You can make a module where some of these values are set to a default value, so you can prevent naming them.
The benefit of using a module is that you can fix mistakes just once, (in the module) and you do not need to change your Terraform code that uses the module.
As you can see the version = 1.0.0
is used. Versioning is important, you can indicate a few things:
1
) indicates the compatibility. 2.0.0
is not compatible with 1.0.0
.0
) indicates the features. If this number increases, a new feature should be available.0
) indicates the bugfixes. If this number increases, a problem has been fixed.Number can go beyond 9
, so a version a 3.2.13
is possible.
Many vendors do not use SemVer, rather use a new version for marketing purposes.
terraform-azurerm-resource_group
.variables.tf
.outputs.tf
README.md
.main.tf
.versions.tf
examples/default/main.tf
the uses the module.The documentation may also help.
azurerm_resource_group
s. Solutionterraform init
from the examples/default
directory.learn-terraform-azure
) to use the modules you’ve created. Solutionregion
and size
.)r
but region
for example.)README.md
explaining the features.LICENSE
before publishing.terraform.tfstate
, terraform.tfstate.backup
, .terraform
and *.tfvars
in .gitignore
.examples
directory, and in there default
(or others). Write examples/*/main.tf
to try your own module.To use a module stored on a version control system, use this code:
module "resourcegroup" {
source = "git::ssh://[email protected]/NAMESPACE/REPOSITORY.git"
resource_group_name = local.resource_group_name
location = var.location
rg_tags = var.rg_tags
}