Using a list variable for ECS task in container_definitions with terraform












0















In terraform I am attempting to pass a variable (list) to a module that we built. This variable needs to be used within a aws_ecs_task_definition resource in the container_definitions.

Right now I am just starting with an empty default list defined as a variable:



variable "task_enviornment" {
type = "list"
default =
}


My ECS task definition looks like this:



resource "aws_ecs_task_definition" "ecs_task_definition" {
family = "${var.ecs_family}"
network_mode = "awsvpc"
task_role_arn = "${aws_iam_role.iam_role.arn}"
execution_role_arn = "${data.aws_iam_role.iam_ecs_task_execution_role.arn}"
requires_compatibilities = ["FARGATE"]
cpu = "${var.fargate_cpu}"
memory = "${var.fargate_memory}"

container_definitions =<<DEFINITION
[
{
"cpu": ${var.fargate_cpu},
"image": "${var.app_image}",
"memory": ${var.fargate_memory},
"name": "OURNAME",
"networkMode": "awsvpc",
"environment": "${jsonencode(var.task_enviornment)}",
"logConfiguration": {
"logDriver": "awslogs",
"options": {
"awslogs-group" : "${aws_cloudwatch_log_group.fargate-logs.name}",
"awslogs-region": "us-east-1",
"awslogs-stream-prefix": "demo"
}
},
"portMappings": [
{
"containerPort": ${var.app_port},
"hostPort": ${var.app_port}
}
]
}
]
DEFINITION
}


The part I am having a problem with with the "environment" part:



"environment": "${jsonencode(var.task_enviornment)}",


I have tried a few different ways to get this to work.



If I do "environment": "${jsonencode(var.task_enviornment)}",
I get ECS Task Definition container_definitions is invalid: Error decoding JSON: json: cannot unmarshal string into Go struct field ContainerDefinition.Environment of type *ecs.KeyValuePair



If I do "environment": "${var.task_enviornment}", or "environment": ["${var.task_enviornment}"],

I get At column 1, line 1: output of an HIL expression must be a string, or a single list (argument 8 is TypeList) in:
Then it just outputs the contents of container_definitions



I did also try adding default values and I was getting similar error messages. However I do need to be able to handle no values being sent in, so an empty list.



variable "task_enviornment" {
type = "list"
default = [
{
"name" = "BUCKET",
"value" = "test"
}
]
}









share|improve this question



























    0















    In terraform I am attempting to pass a variable (list) to a module that we built. This variable needs to be used within a aws_ecs_task_definition resource in the container_definitions.

    Right now I am just starting with an empty default list defined as a variable:



    variable "task_enviornment" {
    type = "list"
    default =
    }


    My ECS task definition looks like this:



    resource "aws_ecs_task_definition" "ecs_task_definition" {
    family = "${var.ecs_family}"
    network_mode = "awsvpc"
    task_role_arn = "${aws_iam_role.iam_role.arn}"
    execution_role_arn = "${data.aws_iam_role.iam_ecs_task_execution_role.arn}"
    requires_compatibilities = ["FARGATE"]
    cpu = "${var.fargate_cpu}"
    memory = "${var.fargate_memory}"

    container_definitions =<<DEFINITION
    [
    {
    "cpu": ${var.fargate_cpu},
    "image": "${var.app_image}",
    "memory": ${var.fargate_memory},
    "name": "OURNAME",
    "networkMode": "awsvpc",
    "environment": "${jsonencode(var.task_enviornment)}",
    "logConfiguration": {
    "logDriver": "awslogs",
    "options": {
    "awslogs-group" : "${aws_cloudwatch_log_group.fargate-logs.name}",
    "awslogs-region": "us-east-1",
    "awslogs-stream-prefix": "demo"
    }
    },
    "portMappings": [
    {
    "containerPort": ${var.app_port},
    "hostPort": ${var.app_port}
    }
    ]
    }
    ]
    DEFINITION
    }


    The part I am having a problem with with the "environment" part:



    "environment": "${jsonencode(var.task_enviornment)}",


    I have tried a few different ways to get this to work.



    If I do "environment": "${jsonencode(var.task_enviornment)}",
    I get ECS Task Definition container_definitions is invalid: Error decoding JSON: json: cannot unmarshal string into Go struct field ContainerDefinition.Environment of type *ecs.KeyValuePair



    If I do "environment": "${var.task_enviornment}", or "environment": ["${var.task_enviornment}"],

    I get At column 1, line 1: output of an HIL expression must be a string, or a single list (argument 8 is TypeList) in:
    Then it just outputs the contents of container_definitions



    I did also try adding default values and I was getting similar error messages. However I do need to be able to handle no values being sent in, so an empty list.



    variable "task_enviornment" {
    type = "list"
    default = [
    {
    "name" = "BUCKET",
    "value" = "test"
    }
    ]
    }









    share|improve this question

























      0












      0








      0








      In terraform I am attempting to pass a variable (list) to a module that we built. This variable needs to be used within a aws_ecs_task_definition resource in the container_definitions.

      Right now I am just starting with an empty default list defined as a variable:



      variable "task_enviornment" {
      type = "list"
      default =
      }


      My ECS task definition looks like this:



      resource "aws_ecs_task_definition" "ecs_task_definition" {
      family = "${var.ecs_family}"
      network_mode = "awsvpc"
      task_role_arn = "${aws_iam_role.iam_role.arn}"
      execution_role_arn = "${data.aws_iam_role.iam_ecs_task_execution_role.arn}"
      requires_compatibilities = ["FARGATE"]
      cpu = "${var.fargate_cpu}"
      memory = "${var.fargate_memory}"

      container_definitions =<<DEFINITION
      [
      {
      "cpu": ${var.fargate_cpu},
      "image": "${var.app_image}",
      "memory": ${var.fargate_memory},
      "name": "OURNAME",
      "networkMode": "awsvpc",
      "environment": "${jsonencode(var.task_enviornment)}",
      "logConfiguration": {
      "logDriver": "awslogs",
      "options": {
      "awslogs-group" : "${aws_cloudwatch_log_group.fargate-logs.name}",
      "awslogs-region": "us-east-1",
      "awslogs-stream-prefix": "demo"
      }
      },
      "portMappings": [
      {
      "containerPort": ${var.app_port},
      "hostPort": ${var.app_port}
      }
      ]
      }
      ]
      DEFINITION
      }


      The part I am having a problem with with the "environment" part:



      "environment": "${jsonencode(var.task_enviornment)}",


      I have tried a few different ways to get this to work.



      If I do "environment": "${jsonencode(var.task_enviornment)}",
      I get ECS Task Definition container_definitions is invalid: Error decoding JSON: json: cannot unmarshal string into Go struct field ContainerDefinition.Environment of type *ecs.KeyValuePair



      If I do "environment": "${var.task_enviornment}", or "environment": ["${var.task_enviornment}"],

      I get At column 1, line 1: output of an HIL expression must be a string, or a single list (argument 8 is TypeList) in:
      Then it just outputs the contents of container_definitions



      I did also try adding default values and I was getting similar error messages. However I do need to be able to handle no values being sent in, so an empty list.



      variable "task_enviornment" {
      type = "list"
      default = [
      {
      "name" = "BUCKET",
      "value" = "test"
      }
      ]
      }









      share|improve this question














      In terraform I am attempting to pass a variable (list) to a module that we built. This variable needs to be used within a aws_ecs_task_definition resource in the container_definitions.

      Right now I am just starting with an empty default list defined as a variable:



      variable "task_enviornment" {
      type = "list"
      default =
      }


      My ECS task definition looks like this:



      resource "aws_ecs_task_definition" "ecs_task_definition" {
      family = "${var.ecs_family}"
      network_mode = "awsvpc"
      task_role_arn = "${aws_iam_role.iam_role.arn}"
      execution_role_arn = "${data.aws_iam_role.iam_ecs_task_execution_role.arn}"
      requires_compatibilities = ["FARGATE"]
      cpu = "${var.fargate_cpu}"
      memory = "${var.fargate_memory}"

      container_definitions =<<DEFINITION
      [
      {
      "cpu": ${var.fargate_cpu},
      "image": "${var.app_image}",
      "memory": ${var.fargate_memory},
      "name": "OURNAME",
      "networkMode": "awsvpc",
      "environment": "${jsonencode(var.task_enviornment)}",
      "logConfiguration": {
      "logDriver": "awslogs",
      "options": {
      "awslogs-group" : "${aws_cloudwatch_log_group.fargate-logs.name}",
      "awslogs-region": "us-east-1",
      "awslogs-stream-prefix": "demo"
      }
      },
      "portMappings": [
      {
      "containerPort": ${var.app_port},
      "hostPort": ${var.app_port}
      }
      ]
      }
      ]
      DEFINITION
      }


      The part I am having a problem with with the "environment" part:



      "environment": "${jsonencode(var.task_enviornment)}",


      I have tried a few different ways to get this to work.



      If I do "environment": "${jsonencode(var.task_enviornment)}",
      I get ECS Task Definition container_definitions is invalid: Error decoding JSON: json: cannot unmarshal string into Go struct field ContainerDefinition.Environment of type *ecs.KeyValuePair



      If I do "environment": "${var.task_enviornment}", or "environment": ["${var.task_enviornment}"],

      I get At column 1, line 1: output of an HIL expression must be a string, or a single list (argument 8 is TypeList) in:
      Then it just outputs the contents of container_definitions



      I did also try adding default values and I was getting similar error messages. However I do need to be able to handle no values being sent in, so an empty list.



      variable "task_enviornment" {
      type = "list"
      default = [
      {
      "name" = "BUCKET",
      "value" = "test"
      }
      ]
      }






      terraform terraform-provider-aws






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 27 '18 at 13:22









      Jon HeckmanJon Heckman

      1301312




      1301312
























          1 Answer
          1






          active

          oldest

          votes


















          0














          After a lot of investigation and a fresh set of eyes looking at this figured out the solution. I am unsure why this fixes it, and I feel like this is likely a bug.

          Needed to do 2 things to fix this.





          1. Remove type = "list" from the variable definition.



            variable "task_environment" {
            default =
            }



          2. Remove the quotes when using the variable:
            "environment": ${jsonencode(var.task_environment)},







          share|improve this answer























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53500709%2fusing-a-list-variable-for-ecs-task-in-container-definitions-with-terraform%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            After a lot of investigation and a fresh set of eyes looking at this figured out the solution. I am unsure why this fixes it, and I feel like this is likely a bug.

            Needed to do 2 things to fix this.





            1. Remove type = "list" from the variable definition.



              variable "task_environment" {
              default =
              }



            2. Remove the quotes when using the variable:
              "environment": ${jsonencode(var.task_environment)},







            share|improve this answer




























              0














              After a lot of investigation and a fresh set of eyes looking at this figured out the solution. I am unsure why this fixes it, and I feel like this is likely a bug.

              Needed to do 2 things to fix this.





              1. Remove type = "list" from the variable definition.



                variable "task_environment" {
                default =
                }



              2. Remove the quotes when using the variable:
                "environment": ${jsonencode(var.task_environment)},







              share|improve this answer


























                0












                0








                0







                After a lot of investigation and a fresh set of eyes looking at this figured out the solution. I am unsure why this fixes it, and I feel like this is likely a bug.

                Needed to do 2 things to fix this.





                1. Remove type = "list" from the variable definition.



                  variable "task_environment" {
                  default =
                  }



                2. Remove the quotes when using the variable:
                  "environment": ${jsonencode(var.task_environment)},







                share|improve this answer













                After a lot of investigation and a fresh set of eyes looking at this figured out the solution. I am unsure why this fixes it, and I feel like this is likely a bug.

                Needed to do 2 things to fix this.





                1. Remove type = "list" from the variable definition.



                  variable "task_environment" {
                  default =
                  }



                2. Remove the quotes when using the variable:
                  "environment": ${jsonencode(var.task_environment)},








                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 27 '18 at 16:25









                Jon HeckmanJon Heckman

                1301312




                1301312
































                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53500709%2fusing-a-list-variable-for-ecs-task-in-container-definitions-with-terraform%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Lallio

                    Unable to find Lightning Node

                    Futebolista