Modules

A Terraform module is very simple, any collections of .tf files containing resources, input variables, and outputs which exist outside the root folder (working directory).

The current working directory witch contains Terraform configuration files in which you run terraform apply is itself a valid module called as root module.

Terraform modules provide an easy way to abstract common blocks of configuration into reusable infrastructure elements.

 

Creating Modules

Like the root module you can specify inputs and outputs for the module using variable and output.

Create a directory for modules and create sub directory (in this case instance) to create configuration files.

$ mkdir -p modules/instance
$ cd modules/instance/
$ touch main.tf variables.tf outputs.tf
$ cd ..
$ tree
.
└── instance
    ├── main.tf
    ├── outputs.tf
    └── variables.tf

1 directory, 3 files

In the file main.tf, we create the resources which define our ec2 instance.

We do not need to add a provider as this is picked up from the root module.

variables.tf

variable "ami" {
 default = "ami-009d6802948d06e52"
}

main.tf

resource "aws_instance" "web" {
  ami = "${var.ami}"
  instance_type = "t2.micro"
  tags = {
   Name = "WebServer"
 }
}

outputs.tf

output "PublicDNS" {
  value = "${aws_instance.web.public_dns}"
}

Once module is created, we need to reference our module using “module”.

Create another directory for our configuration files.

$ mkdir ec2
$ tocuh ec2/instance.tf

instance.tf

provider "aws" {
  region ="us-east-2"
}
module "instance" {
  source = "../modules/instance"
  ami = "ami-02e680c4540db351e"
}
output "PublicDNS" {
  value = "${module.instance.PublicDNS}"
}

Final tree structure

$ tree
.
├── ec2
│   └── instance.tf
└── modules
    └── instance
        ├── main.tf
        ├── outputs.tf
        └── variables.tf

3 directories, 4 files

 

Module Sources

The module installer will download the source code to a directory on local disk so that it can be used by other Terraform commands.

It supports installation from below sources.

  • Local paths
  • Terraform Registry
  • GitHub
  • Bitbucket
  • HTTP URLs
  • S3 buckets

Local paths :

The module already present on local disk and we can use directly.

A local path must begin with either ./ or ../ to indicate that a local path.

module "instance" {
  source = "../modules/instance"
}

Terraform Registry

A module registry is used to distribute Terraform modules for use across multiple configurations. It also supports module versioning.

You can also use a private registry, either via the built-in feature from Terraform Enterprise or by running a custom service that implements the module registry protocol.

Terraform Registry can be referenced using a registry source address of the form <NAMESPACE>/<NAME>/<PROVIDER>.

module "elb" {
  source = "hashicorp/elb/aws"
  version = "0.1.0"
}

GitHub

Terraform will recognize unprefixed github.com URLs and interpret them automatically as Git repository sources.

module "autoscale" {
source = "github.com/aws-modules/autoscaling"
}
module "autoscale" {
source = "git@github.com:aws-modules/autoscaling.git"
}

Bitbucket

Terraform will recognize unprefixed bitbucket.org URLs and interpret them automatically as BitBucket repositories.

module "loadbalancer" {
  source = "bitbucket.org/aws-modules/ec2-loadbalancer"
}

HTTP URLs

When you use an HTTP or HTTPS URL, Terraform will make a GET request to the given URL.

module "vpc" {
  source = "https://example.com/rds-module.zip"
}

The extensions that Terraform recognizes are:

  • Zip
  • tar.bz2 and tbz2
  • tar.gz and tgz
  • tar.xz and txz

S3 Bucket

You can use archives stored in S3 as module sources using the special s3:: prefix, followed by an S3 bucket object URL.

module "consul" {
 source = "s3::https://s3-eu-west-1.amazonaws.com/getstart-terraform-modules/vpc.zip"
}