Conditions on Terraform

Depend on conditions, we may need to use different parameters. Writing whole tf file with all possible conditions is not feasible and practical.

TEST-1

I want to create ebs volume only in production but not on other environments

Existing code

 ebs_block_device {
    device_name = "/dev/sdb"
    volume_size = 40
    encrypted   = true
 }

Solution-1

We have static code, it is needed to be converted to dynamic block and we should put if condition to put exetute on prod env, discard on other environments.

 dynamic "ebs_block_device" {
    for_each = var.env == "prod" ? [true] : [] //This is the line
    content {
      device_name = "/dev/sdb"
      volume_size = 40
      encrypted   = true
    }
  }

TEST-2

We want to encrypt root block device on just production environment

Solution-2

  root_block_device {
    volume_size           = "20"
    volume_type           = "gp2"
    encrypted             = var.env == "prod" ? true : false
    delete_on_termination = true
  }

Bir cevap yazın

E-posta hesabınız yayımlanmayacak. Gerekli alanlar * ile işaretlenmişlerdir