CodexBloom - Programming Q&A Platform

Terraform 1.4.3: implementing Configuring Multiple Environment Variables for AWS ECS Task Definitions

👀 Views: 98 💬 Answers: 1 📅 Created: 2025-08-22
terraform aws ecs hcl

I'm dealing with Quick question that's been bugging me - I'm wondering if anyone has experience with This might be a silly question, but I'm trying to configure multiple environment variables for my AWS ECS task definitions using Terraform 1.4.3, but I'm running into issues where only the first set of environment variables is being applied... I have implemented the following code snippet: ```hcl resource "aws_ecs_task_definition" "my_task" { family = "my-task-family" network_mode = "bridge" requires_compatibilities = ["EC2"] container_definitions = jsonencode([ { name = "my-container" image = "my-image:latest" memory = 512 cpu = 256 essential = true environment = [ { name = "ENV_VAR1", value = "value1" }, { name = "ENV_VAR2", value = "value2" }, { name = "ENV_VAR3", value = "value3" } ] } ]) } ``` When I run `terraform apply`, the task definition is created, but it seems that only `ENV_VAR1` is being set in the container. I've also tried defining the environment variables using a different approach: ```hcl environment { name = "ENV_VAR1" value = "value1" } environment { name = "ENV_VAR2" value = "value2" } environment { name = "ENV_VAR3" value = "value3" ``` However, this still results in only the first environment variable being set. I’ve checked if the IAM role for the ECS task has the correct permissions, and it does. Additionally, I made sure that there are no syntax errors in my JSON encoding. I also verified that my Terraform provider configurations are correct and up-to-date. What am I missing here? Is there a specific way to properly set multiple environment variables that I'm overlooking? My team is using Hcl for this web app. Any help would be greatly appreciated! I appreciate any insights! I'm developing on Ubuntu 22.04 with Hcl. I'm using Hcl 3.9 in this project.