Terraform Module

What is terraform module ?

A Terraform module is a collection of standard configuration files in a dedicated directory. Terraform modules encapsulate groups of resources dedicated to one task, reducing the amount of code you have to develop for similar infrastructure components.

Terraform module definition is a single or many .tf files stacked together in their own directory.

A typical module can look like this:

├── main.tf
├── outputs.tf
├── README.md
└── variables.tf

If we run Terraform in this directory, those configuration files would be considered a root module. It means that this configuration is the base of our operation, that we can expand further.

Installation of Terraform

We are going to install on one of the instance of AWS cloud:

Get the latest release version

TERRAFORM_VER=`curl -s https://api.github.com/repos/hashicorp/terraform/releases/latest |  grep tag_name | cut -d: -f2 | tr -d \"\,\v | awk '{$1=$1};1'`

To download the latest release of Terraform run the below command

wget https://releases.hashicorp.com/terraform/${TERRAFORM_VER}/terraform_${TERRAFORM_VER}_linux_amd64.zip

Extract the file

sudo yum -y install unzip
unzip terraform_${TERRAFORM_VER}_linux_amd64.zip

Move binary file to the /usr/local/bin directory

sudo mv terraform /usr/local/bin/

Creating a Terraform Module

Here we are going to do the following things through terraform module:


1. Create a key pair


3. Launch an EC2 instance using key pair & security group

4. Create an EBS Volume

5. Attach EBS Volume to the EC2 instance

This is the same practical that I have performed through aws cli which you can read from below link:

Let’s head over our practical

Let’s start with the

→ main.tf

This is our main configuration file where we are going to define our resource definition. It will call our module, It contains some required variables which is already defined in source path.

module "aws_architecture" {                
source = "github.com/kmsatyam/instance_ebs_volume_resources?ref=v0.0.1"
instance_type = var.instance_type
availability_zone = var.availability_zone
vpc_security_group_ids = var.vpc_security_group_ids
key_name = var.key_name
disk_name = var.disk_name
ami_id = var.ami_id
disk_size = var.disk_size}

→ variable.tf

Define your variables in separate file which contains all the values defined into source

Input variables let you customize aspects of Terraform modules without altering the module’s own source code. This allows you to share modules across different Terraform configurations, making your module composable and reusable.

When you declare variables in the root module of your configuration, you can set their values using CLI options and environment variables. When you declare them in child modules, the calling module should pass values in the module block.

variable "ami_id" {
type = string
default = "ami-0447a12f28fddb066"
variable "instance_type" {
type = string
default = "t2.micro"
variable "availability_zone" {
type = string
default = "ap-south-1a"
variable "vpc_security_group_ids" {
type = list
default = ["default"]
variable "key_name" {
type = string
default = "task37key"
variable "disk_size" {
type = number
default = 1
variable "disk_name" {
type = string
default = "/dev/sdd"

→ output.tf

Output values make information about your infrastructure available on the command line, and can expose information for other Terraform configurations to use. Output values are similar to return values in programming languages.

output "instance_type" {
value = module.launching_os.instance_type
output "disk_name" {
value = module.launching_os.disk_name
output "ami_id" {
value = module.launching_os.image_id
output "disk_size" {
value = module.launching_os.disk_size

Now our terraform code is ready, the first command we are going to execute is

  • terraform init: It’s going to download code for a provider(aws) that we will use.

Then →

  • terraform plan: This command will tell what terraform does before making any changes.
    1: (+ sign): Resource going to be created
    2: (- sign): Resource going to be deleted
    3: (~ sign): Resource going to be modified

Then →

  • terraform apply: To apply the changes, run terraform apply command.

Then we will be able to see aws ec2 with Ebs attched successfully with the help of Terraform. Terraform requires minimal effort than aws cli which we have used in the above link provided.



For more such articles, Stay Connected 😄



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store