DEV Community

Cover image for Day 21 of My 90-Day DevOps Journey: Mastering Terraform Modules for Reusable Infrastructure
Arbythecoder
Arbythecoder

Posted on

Day 21 of My 90-Day DevOps Journey: Mastering Terraform Modules for Reusable Infrastructure

Hey everyone! Can you believe it's already day 21 of my 90-day DevOps journey? Time flies when you're learning and building awesome things! Today, we're diving into a crucial concept in terraform: modules.

If you've been following along, you know that Terraform is all about automating your infrastructure, making it consistent, and reducing errors. But as your infrastructure grows more complex, you need a way to organize and reuse your code. That's where modules come in.

Modules are like building blocks for your infrastructure. They let you define reusable components that you can use repeatedly in different projects. Think of it like this: instead of writing the same code to create an EC2 instance every time, you can create a module that handles all the details for you. Then, whenever you need a new instance, you just call the module and pass in the necessary parameters.

Why are modules so awesome?

Organization:* Modules help you break down complex infrastructure into smaller, manageable parts.

Reusability:* You can use the same module multiple times in different projects, saving you time and effort.

Maintainability:* Changes to a module affect all instances of that module, making updates and bug fixes easier.

Scalability:* Modules make it easier to manage large, complex infrastructure.

In this article, we're going to learn how to create our first Terraform module. We'll start with a simple example – a module for creating an EC2 instance. By the end of this article, you'll be able to build your own reusable modules and take your Terraform skills to the next level!

Creating Your First Terraform Module: An EC2 Instance

Let's create a module that will handle the creation of an EC2 instance.

1. Module Structure:

Our module will consist of three main files:

variables.tf:* Defines the input variables for the module.

outputs.tf:* Defines the outputs that the module will provide.

main.tf:* Contains the Terraform resources that the module will create.

2. variables.tf:


# Define the input variables for our EC2 instance module

variable "instance_type" {

  type = string

  default = "t2.micro"  # Default instance type

}

variable "ami" {

  type = string

  default = "ami-08c40ec9202800652"  # Default AMI ID

}

variable "key_name" {

  type = string

  default = "my-key-pair"  # Default key pair name

}

variable "security_groups" {

  type = list(string)

  default = ["sg-000000000000000000"]  # Default security group ID

}

variable "user_data" {

  type = string

  default = ""  # Optional user data script

}

Enter fullscreen mode Exit fullscreen mode

Explanation:

  • We define variables for key properties of our EC2 instance:

    • instance_type: The type of EC2 instance (e.g., t2.micro, m5.large).
    • ami: The Amazon Machine Image (AMI) to use for the instance.
    • key_name: The name of the key pair to use for SSH access.
    • security_groups: A list of security group IDs to associate with the instance.
    • user_data: An optional user data script that will be executed when the instance starts.

3. outputs.tf:


# Define outputs to expose information about the created instance

output "public_ip" {

  value = aws_instance.example.public_ip

}

output "instance_id" {

  value = aws_instance.example.id

}

Enter fullscreen mode Exit fullscreen mode

Explanation:

  • We define outputs to retrieve important information about the created EC2 instance:

    • public_ip: The public IP address of the instance.
    • instance_id: The unique ID of the instance.

4. main.tf:


# Define the Terraform resources to create the EC2 instance

resource "aws_instance" "example" {

  ami           = var.ami

  instance_type = var.instance_type

  key_name      = var.key_name

  security_groups = var.security_groups

  # Optional user data

  user_data = var.user_data

}

Enter fullscreen mode Exit fullscreen mode

Explanation:

  • We define an aws_instance resource, using the variables we defined in variables.tf to configure the instance.

Using Your EC2 Instance Module

Now that we've created our module, let's use it to deploy an EC2 instance. We'll do this in a separate Terraform configuration file (e.g., main.tf):


# main.tf

module "ec2_instance" {

  source = "./modules/ec2_instance"

  # Pass values to the module variables

  instance_type = "t2.medium"

  ami = "ami-08c40ec9202800652"

  key_name = "my-key-pair"

  security_groups = ["sg-000000000000000000"]

}

output "instance_public_ip" {

  value = module.ec2_instance.public_ip

}

Enter fullscreen mode Exit fullscreen mode

Explanation:

  • We use the module block to call our ec2_instance module.

  • We pass values to the module's variables to configure the instance:

    • instance_type: t2.medium
    • ami: ami-08c40ec9202800652
    • key_name: my-key-pair
    • security_groups: ["sg-000000000000000000"]
  • We use the output block to display the public IP of the instance.

Running Your Terraform Code

  1. Initialize Terraform: terraform init

  2. Plan the changes: terraform plan

  3. Apply the changes: terraform apply

Terraform will now create the EC2 instance based on your module's configuration.

Conclusion

Congratulations! You've successfully created and used your first Terraform module. This is a powerful technique that will help you manage complex infrastructure more effectively. As you continue your DevOps journey, you'll find yourself using modules more and more often. Remember to keep your modules focused, use descriptive names, document your code, and test your modules thoroughly.

Now, go forth and build amazing things with Terraform!

Further Reading

For a deeper dive into Terraform modules and best practices, check out these resources:

"Terraform Modules: A Deep Dive" by HashiCorp:* https://www.hashicorp.com/resources/terraform-modules-deep-dive

"Terraform: Up & Running" by Yevgeniy Brikman:* https://www.oreilly.com/library/view/terraform-up-running/9781491953969/

"Terraform: Infrastructure as Code" by HashiCorp:* https://learn.hashicorp.com/terraform/tutorials/infrastructure-as-code

HashiCorp Learn:* https://learn.hashicorp.com/terraform

These resources will provide you with a comprehensive understanding of Terraform modules and help you build robust and reusable infrastructure.

Top comments (0)