Search code examples
amazon-web-servicesdockerterraformamazon-ecsterraform-provider-aws

Pass command as variable to ECS task definition


Is there a way to pass a Docker command as a Terraform variable to the ECS task definition that is defined in Terraform?


Solution

  • You can try the below method to take the command as a variable with template condition if nothing is passed from the root module. service.json

    [
      {
        ...
        ],
        %{ if command != "" }
        "command"  : [${command}],
        %{ endif ~}
        ...
      }
    ]
    

    container.tf

    data "template_file" "container_def" {
      count    = 1
      template = file("${path.module}/service.json")
      vars = {
        command        = var.command != "" ? join(",", formatlist("\"%s\"", var.command)) : ""
      }
    }
    

    main.tf

    module "example" {
    ...
         command                 = ["httpd", "-f", "-p", "8080"]
    ...
    }
    

    variables.tf

    variable "command" {
      default = ""
    }