dzbeda
dzbeda

Reputation: 303

Terraform - How to define which modules to run and create using variables

I have terraform code that creates and runs a few modules - VPC, Network, ansible and Jenkins. I need the option that Ansible and Jenkins module will be created based on a true or false value.

When I tried to comment-out the Jenkins module I needed to remove all related variables and it was a mes,. I needed to comment-out variables from the root folder (variables ,tfvars and output files) and the network module since I'm passing the Jenkins security group id.

Is there a smart way to choose which modules should be kept out including their variables without editing files?

root folder - Main.tf

module "network" {
     source = "./modules/network"
     vpc_id = module.vpc.vpcid
     number_of_public_subnets = 2
     number_of_private_subnets = 2
     public-subnet-block = var.public-subnet-block
     private-subnet-block = var.private-subnet-block
     availability_zone = var.availability_zone
     gateway_id = module.vpc.gwid
     tag_enviroment= var.tag_enviroment
     project_name = var.project_name
     **jenkins_server_target_group_arn = module.jenkins.jenkins-server-target-group-arn**

}
module "ansible-server"{
     source = "./modules/ansible-server"
     ami_id = "ami-04505e74c0741db8d"
     instance_type = var.ansible_server_instance-type
     availability_zone = var.availability_zone[0]
     subnet_id = module.network.public-subnet-id[0]
     tag_enviroment= var.tag_enviroment
     project_name = var.project_name
     vpc_id = module.vpc.vpcid
     key_name  = aws_key_pair.mid_project_key.key_name
     private_key_file_name = var.private_key_file_name
     iam_instance_profile   = aws_iam_instance_profile.ec2-role.name
     depends_on = [local_file.mid_project_key]
}

module "jenkins"{
     source = "./modules/jenkins"
     ami_id = "ami-0e472ba40eb589f49"
     jenkins_nodes_number_of_server = 2
     jenkins-server-instance-type = var.jenkins-server-instance-type
     jenkins-node-instance-type = var.jenkins-node-instance-type
     private_subnet_id = module.network.private-subnet-id
     tag_enviroment= var.tag_enviroment
     project_name = var.project_name
     vpc_id = module.vpc.vpcid
     key_name  = aws_key_pair.mid_project_key.key_name
     private_key_file_name = var.private_key_file_name
     iam_instance_profile   = aws_iam_instance_profile.ec2-role.name
     alb1_security_group_id = module.network.alb1-security-group-id
}

root folder - Main.tf variables.tf

variable "ansible_server_instance-type" {
  type = string
  default = "t2.micro"
}
variable "private-subnet-block" {
  type = list(string)
}
variable "public-subnet-block" {
  type = list(string)
}
variable "availability_zone" {}
variable "private_key_file_name" {}
variable "consul-instance-type" {}
variable "jenkins-node-instance-type" {}
variable "jenkins-server-instance-type" {}

root folder - terraform.tfvars

jenkins-node-instance-type = "t2.micro"
jenkins-server-instance-type = "t2.micro"

Module folder -> main.tf

resource "aws_alb_listener" "jenkins" {
  load_balancer_arn = aws_alb.alb1.arn
  port              = "9000"
  protocol          = "HTTP"

  default_action {
    type             = "forward"
    target_group_arn = var.jenkins_server_target_group_arn
  }
}

Module folder -> varibles.tf

variable "jenkins_server_target_group_arn" {}

Upvotes: 0

Views: 160

Answers (1)

stdtom
stdtom

Reputation: 762

The count meta argument can help you here. Just create 0 or 1 instances of the module depending on the outcome of a conditional expression.

variable "create_ansible_server {
  type    = bool
}

...

module "ansible-server"{
  source = "./modules/ansible-server"
  count  = var.create_ansible_server ? 1 : 0
  ...
}

Upvotes: 1

Related Questions