Introduction

In this article, we will see a subtle introduction to terraform modules, how to pass data into the module, get something from the module and create a resource (GKE cluster), it’s intended to be as simple as possible just to be aware of what a module is composed of, or how can you do your own modules, sometimes it makes sense to have modules to abstract implementations that you use over several projects, or things that are often repeated along the project. So let’s see what it takes to create and use a module.

The source code for this article can be found here. Note that in this example I’m using GCP since they give you $300 USD for a year to try their services and it looks pretty good so far, after sign-up you will need to go to IAM, then create a service account and after that export the key (this is required for the terraform provider to talk to GCP).

Composition of a Module

A module can be any folder with a main.tf file in it, yes, that is the only required file for a module to be usable, but the recommendation is that you also put a README.md file with a description of the module if it’s intended to be used by people if it’s a sub-module it’s not necessary, also you will need a file called variables.tf and other outputs.tf of course if it’s a big module that cannot be split into sub-modules you can split those files for convenience or readability, variables should have descriptions so the tooling can show you what are they for, you can read more about the basics for a module here.

Before moving on let’s see the folder structure of our project:

Java

1

├── account.json

2

├── LICENSE

3

├── main.tf

4

├── module

5

│   ├── main.tf

6

│   ├── outputs.tf

7

│   └── variables.tf

8

├── README.md

9

└── terraform.tfvars

10

11

1 directory, 8 files

The Project

Let’s start with the main.tf that will call our module, notice that I added a few additional comments but it’s pretty much straight forward, we set the provider, then we define some variables, call our module and print some output (output can also be used to pass data between modules).

Java

1

## Set the provider to be able to talk to GCP

2

provider "google" {

3

  credentials = "${file("account.json")}"

4

  project     = "${var.project_name}"

5

  region      = "${var.region}"

6

}

7

8

## Variable definition

9

variable "project_name" {

10

  default = "testinggcp"

11

  type    = "string"

12

}

13

14

variable "cluster_name" {

15

  default = "demo-terraform-cluster"

16

  type    = "string"

17

}

18

19

variable "region" {

20

  default = "us-east1"

21

  type    = "string"

22

}

23

24

variable "zone" {

25

  default = "us-east1-c"

26

  type    = "string"

27

}

28

29

## Call our module and pass the var zone in, and get cluster_name out

30

module "terraform-gke" {

31

  source = "./module"

32

  zone = "${var.zone}"

33

  cluster_name = "${var.cluster_name}"

34

}

35

36

## Print the value of k8s_master_version

37

output "kubernetes-version" {

38

  value = module.terraform-gke.k8s_master_version

39

}

Then terraform.tfvars has some values to override the defaults that we defined:

Java

1

project_name = "testingcontainerengine"

2

cluster_name = "demo-cluster"

3

region = "us-east1"

4

zone = "us-east1-c"

#tutorial #devops #terraform #gcp cloud #terraform tutorial #kubernetes for beginners #terraform modules

Getting Started With Terraform Modules
1.90 GEEK