What are Terraform Modules?

Terraform modules are a way to organize and encapsulate your Terraform configuration into reusable components. They allow you to group related resources together and manage them as a single unit. This modular approach helps in maintaining, reusing, and sharing your infrastructure code.

Key Concepts:

  • Module: A container for multiple resources that are used together.
  • Root Module: The main module that Terraform uses to run your configuration.
  • Child Module: Any module that is called by another module.

Why Use Modules?

Modules provide several benefits:

  1. Reusability: Write your configuration once and reuse it across different projects.
  2. Maintainability: Easier to manage and update your infrastructure code.
  3. Organization: Helps in organizing your code into logical components.
  4. Collaboration: Share modules with your team or the community.

Basic Structure of a Module

A module typically consists of the following files:

  • main.tf: Contains the primary configuration for the module.
  • variables.tf: Defines the input variables for the module.
  • outputs.tf: Defines the outputs of the module.
  • README.md: Provides documentation for the module.

Example Directory Structure:

my-module/
├── main.tf
├── variables.tf
├── outputs.tf
└── README.md

Creating a Simple Module

Let's create a simple module that provisions an AWS S3 bucket.

Step 1: Define the Module

Create a directory named s3-bucket and add the following files:

main.tf

resource "aws_s3_bucket" "example" {
  bucket = var.bucket_name
  acl    = var.acl

  tags = {
    Name        = var.bucket_name
    Environment = var.environment
  }
}

variables.tf

variable "bucket_name" {
  description = "The name of the S3 bucket"
  type        = string
}

variable "acl" {
  description = "The ACL to apply to the bucket"
  type        = string
  default     = "private"
}

variable "environment" {
  description = "The environment for the bucket"
  type        = string
  default     = "dev"
}

outputs.tf

output "bucket_arn" {
  description = "The ARN of the S3 bucket"
  value       = aws_s3_bucket.example.arn
}

Step 2: Use the Module

Now, let's use this module in a root module.

main.tf

provider "aws" {
  region = "us-west-2"
}

module "s3_bucket" {
  source      = "./s3-bucket"
  bucket_name = "my-unique-bucket-name"
  environment = "production"
}

Step 3: Initialize and Apply

  1. Initialize the configuration:

    terraform init
    
  2. Apply the configuration:

    terraform apply
    

Practical Exercise

Task:

Create a module that provisions an AWS EC2 instance with the following specifications:

  • Instance type: t2.micro
  • AMI ID: ami-0c55b159cbfafe1f0 (Amazon Linux 2)
  • Key pair name: my-key-pair
  • Tags: Name = "MyInstance"

Solution:

Directory Structure:

ec2-instance/
├── main.tf
├── variables.tf
├── outputs.tf
└── README.md

main.tf

resource "aws_instance" "example" {
  ami           = var.ami_id
  instance_type = var.instance_type
  key_name      = var.key_name

  tags = {
    Name = var.instance_name
  }
}

variables.tf

variable "ami_id" {
  description = "The AMI ID to use for the instance"
  type        = string
}

variable "instance_type" {
  description = "The type of instance to use"
  type        = string
  default     = "t2.micro"
}

variable "key_name" {
  description = "The key pair name to use for the instance"
  type        = string
}

variable "instance_name" {
  description = "The name to tag the instance with"
  type        = string
  default     = "MyInstance"
}

outputs.tf

output "instance_id" {
  description = "The ID of the EC2 instance"
  value       = aws_instance.example.id
}

Using the Module:

provider "aws" {
  region = "us-west-2"
}

module "ec2_instance" {
  source        = "./ec2-instance"
  ami_id        = "ami-0c55b159cbfafe1f0"
  key_name      = "my-key-pair"
  instance_name = "MyInstance"
}

Initialize and Apply:

  1. Initialize the configuration:

    terraform init
    
  2. Apply the configuration:

    terraform apply
    

Conclusion

In this section, we introduced Terraform modules, explained their benefits, and demonstrated how to create and use a simple module. Modules are a powerful feature in Terraform that help you write clean, reusable, and maintainable infrastructure code. In the next section, we will dive deeper into creating more complex modules and using the Terraform Module Registry.

© Copyright 2024. All rights reserved