lgallard/terraform-aws-elasticsearch

New Feature: variable create.

icalvete opened this issue · 8 comments

This could be usefull.

A variable to choose if the resources are gonna be created.

// Elasticsearch service
module "aws_es0" {
  source  = "lgallard/elasticsearch/aws"
  version = "~> 0.6.1"

  create = var.environment == "PRO" ? true : false

  domain_name                                    = "elasticsearch0"
  elasticsearch_version                          = "7.8"
  vpc_options_subnet_ids                         = module.my_vpc.private_subnets
  node_to_node_encryption_enabled                = "true"
  snapshot_options_automated_snapshot_start_hour = "23"


  cluster_config = {
    dedicated_master_enabled = "true"
    instance_count           = "3"
    instance_type            = "t3.medium.elasticsearch"
    zone_awareness_enabled   = "true"
    availability_zone_count  = "3"
  }
.
.
.
}

@icalvete If you don't want to create the resource you can just rename the .tf file where it's defined using a different extension, for example tf.disabled. This way Terraform won't include it in the plan / apply.

@icalvete does it make sense to you?

Not really @lgallard. I'm trying to deploy with one repo al my environments. TEST, DEV and PRO.

I have some modules I want create in one, two or event tree environment.

Many modules uses create in order to set the parameter count into the resources to 1.

call the module

module "aws_es0" {
  source  = "lgallard/elasticsearch/aws"
  version = "~> 0.6.1"

  create = var.environment == "PRO" || var.environment == "DEV" ? true : false

variable.tf into the module

variable "create" {                                                                                                            
  description = "Controls if service should be created (it affects almost all resources)"
  type        = bool
  default     = true
}

main.tf into the module

resource "aws_cloudwatch_log_group" "fmy-api" {                                                                             

  count = var.create ? 1 : 0 

  name              = "my-api"
  retention_in_days = 1 
}

Do you like it?

@icalvete with Terraform 14 you can achieve that with a count:

module "aws_es0" {
  source  = "lgallard/elasticsearch/aws"
  version = "~> 0.7.0"
  
  count =  var.environment == "PRO" || var.environment == "DEV" ? 1 : 0
 
  ...
 }

I'm not ready to upgrade to Terraform 14.

@icalvete I will update the module to include this feature

@icalvete PR #33 addresses this issue, but it needs to be tested. Can you tested it and give feedback?

Take into account the variable to use is enabled instead of create, and by default is set to true. You will need to point to the current branch feature/conditional-creation at GitHub instead of the Terraform public registry, therefore you above code should be modified to:

// Elasticsearch service
module "aws_es0" {

  source  = "lgallard/elasticsearch/aws"
  enabled = var.environment == "PRO" ? true : false

  domain_name                                    = "elasticsearch0"
  elasticsearch_version                          = "7.8"
  vpc_options_subnet_ids                         = module.my_vpc.private_subnets
  node_to_node_encryption_enabled                = "true"
  snapshot_options_automated_snapshot_start_hour = "23"


  cluster_config = {
    dedicated_master_enabled = "true"
    instance_count           = "3"
    instance_type            = "t3.medium.elasticsearch"
    zone_awareness_enabled   = "true"
    availability_zone_count  = "3"
  }

...

}

Thanks in advance!

@icalvete I already tested and merge it to release 0.8.0. You can tested as follows:

// Elasticsearch service
module "aws_es0" {
  source  = "lgallard/elasticsearch/aws"
  version = "~> 0.8.0"

  create = var.environment == "PRO" ? true : false

  domain_name                                    = "elasticsearch0"
  elasticsearch_version                          = "7.8"
  vpc_options_subnet_ids                         = module.my_vpc.private_subnets
  node_to_node_encryption_enabled                = "true"
  snapshot_options_automated_snapshot_start_hour = "23"


  cluster_config = {
    dedicated_master_enabled = "true"
    instance_count           = "3"
    instance_type            = "t3.medium.elasticsearch"
    zone_awareness_enabled   = "true"
    availability_zone_count  = "3"
  }

...

}