Terraform Error: ClientException: When the volume parameter is specified, only one volume configuration type should be used

Viewed 711

I use terraform to create multiple tasks definitions and one of these tasks definitions has multiple volumes but when I try to create from this error, is it possible to create multiple volumes in a task by terraform? because by aws it is possible. But the terraform shows this error.

Error: ClientException: When the volume parameter is specified, only one volume configuration type should be used

I searched the entire internet to find this problem and didn't find an answer, as I'm already using the dynamic volume equal to this in the terraform documentation.

terraform {
  required_version = ">= 0.13.0"

  required_providers {
    aws = "~> 3.0"
  }
}

variable "task_definition_names" {
  type = any
  default = [
    {
      task_definition_name = "jenkins"
      task_definition_file = "jenkins-task-definition.json"
      shared_memory_size   = 64
      cpu                  = 0
      memory_reservation   = 3900
      entrypoint           = ""
      ecr_repository_name  = "jenkins:lts"
      dns                  = "jenkins.com.br"
      hosted_zone          = ""
      allowed_volumes      = true
      volume = [
        {
          name      = "efs-jenkins"
          host_path = "/var/jenkins_home"
          efs_volume_configuration = [{
            file_system_id     = "fs-"
            root_directory     = "/"
            transit_encryption = "ENABLED"
            authorization_config = [{
              access_point_id = "fsap-"
              iam             = "ENABLED"
            }]
          }]
        },
        {
          name                     = "docker-sock"
          host_path                = "/var/run/docker.sock"
        },
        {
          name                     = "docker-bin"
          host_path                = "/usr/bin/docker"
        }
      ]
    },
    {
      task_definition_name = "staging-api"
      task_definition_file = "staging-api-task-definition.json"
      shared_memory_size   = 64
      cpu                  = 0
      memory_reservation   = 900
      entrypoint           = ""
      ecr_repository_name  = "staging-api"
      allowed_volumes      = false
      volume               = []
    }
 ]
}

resource "aws_ecs_task_definition" "main" {
  count  = length(var.task_definition_names)
  family = var.task_definition_names[count.index].task_definition_name

  container_definitions = templatefile("${path.module}/templates/${var.task_definition_names[count.index].task_definition_file}", var.task_definition_names[count.index])

  dynamic "volume" {
    
    for_each = var.task_definition_names[count.index].volume
    content {
      host_path = lookup(volume.value, "host_path", null)
      name      = volume.value.name

      dynamic "efs_volume_configuration" {
        for_each = lookup(volume.value, "efs_volume_configuration", [])
        content {
          file_system_id          = lookup(efs_volume_configuration.value, "file_system_id", null)
          root_directory          = lookup(efs_volume_configuration.value, "root_directory", null)
          transit_encryption      = lookup(efs_volume_configuration.value, "transit_encryption", null)
          transit_encryption_port = lookup(efs_volume_configuration.value, "transit_encryption_port", null)
          dynamic "authorization_config" {
            for_each = lookup(efs_volume_configuration.value, "authorization_config", [])
            content {
              access_point_id = lookup(authorization_config.value, "access_point_id", null)
              iam             = lookup(authorization_config.value, "iam", null)
            }
          }
        }
      }
    }
  }
}

This script is for create multiple task-definitions.

3 Answers

I had the same issue using awscli, but removing any references to host and host_path in the configuration resolved it for me.

Not sure what do you want to do, but you can have only one efs_volume_configuration. However you are trying to create multiple instances of efs_volume_configuration as a dynamic block:

dynamic "efs_volume_configuration"

Also whatever you do in the console, must be related to something else, or AWS console makes it seem like you can do it. But under hood, you can't do this.

Related