Terraform

Terraform Modules

Modules

This blog will help you to work with Terraform modules which helps in grouping multiple resources and how to use the module outputs across other modules

Modules in Terraform are used to group multiple resource

create a directory structure as shown below. We are creating separate folder structure for all the modules.

Package structure as shown below

Inside a modules folder, for now we will use only vpc and ec2-instance

Now in main.tf, this show how to access the modules and also the variable like vpc_id for ec2-instance

provider "aws" {
alias="sandbox2"
region = "${var.region}"
access_key = "${var.aws_access_key_sandbox2}"
secret_key = "${var.aws_secret_key_sandbox2}"
}
module "vpc" {
providers = {
    "aws" = "aws.sandbox2"
  }
source = "./modules/vpc"
}





1111111112



module "ec2_instance" {
 providers = {
    "aws" = "aws.sandbox2"
  }
  source = "./modules/ec2-instance"
  vpc_id = "${module.vpc.vpc_id}"

}
module "ec2_instance" {
 providers = {
 "aws" = "aws.sandbox2" }
source = "./modules/ec2-instance"
vpc_id = "${module.vpc.vpc_id}"
}
}
Now in VPC folder, you need to create a vpc_id as output so that it can be used with other modules like this
resource “aws_vpc” “terraform_vpc” { 
 cidr_block = “${var.vpc-fullcidr}” 
 #### this 2 true values are for use the internal vpc dns resolution
enable_dns_support = true
 enable_dns_hostnames = true
 tags {
 Name = “terraform_vpc”
 }
}
output “vpc_id” { 
 value = “${aws_vpc.terraform_vpc.id}” 
}

Now how to use that vpc_id, you need to create a variable and then use it as shown below

variable vpc_id {}
resource "aws_key_pair" "auth" {
  key_name   = "${var.key_name}"
  public_key = "${file(var.public_key_path)}"
}
resource "aws_instance" "webserver" {
  ami           = "${lookup(var.ami, var.region)}"
 connection {
    # The default username for our AMI
    user = "ec2-user"
host = "${aws_instance.webserver.public_ip}"
# The connection will use the local SSH agent for authentication.
  }
instance_type = "t2.micro"
  associate_public_ip_address = "true"
  subnet_id = "${var.public_subnet_id}"
  vpc_security_group_ids = ["${var.FrontEnd_SG_id}"]
 key_name = "${aws_key_pair.auth.id}" 
 tags {
        Name = "webserver"
  }
  
}

Retrieving module data with outputs — one more example

In an output, you define which data you want to be returned by the module

You have 2 folders (VPC,Network), now VPC is created inside VPC folder so the vpc_id is not directly accessible on network folder. You will add below line on the vpc.tf file

output “vpc_id” {
 value = “${aws_vpc.demo_vpc.id}”
}
module

Now on main.tf

module "network" {
  source = "./modules/network"
  vpc_id = "${module.vpc.vpc_id}"
}

on route-network.tf where you want to use vpc_id. You have to declare variable , name of the variable should be same as we have declared in output and then access it using var.

variable “vpc_id” {}
resource “aws_internet_gateway” “gw” {
 vpc_id = “${var.vpc_id}”
 tags {
 Name = “internet gw terraform generated”
 }
}

Now for modules first you need to run

terraform get

this command will first load all the modules

terraform init

terraform plan ( to check what will get installed before running it)

terraform apply

Please follow and like us: