1

I have the following aws_ecs_task_definition resource definitions in a child module:

resource "aws_ecs_task_definition" "main" {
  family                   = "${var.name}-task-${var.environment}"
  network_mode             = "awsvpc"
  requires_compatibilities = ["FARGATE"]
  cpu                      = var.container_cpu
  memory                   = var.container_memory
  execution_role_arn       = aws_iam_role.ecs_task_execution_role.arn
  task_role_arn            = aws_iam_role.ecs_task_role.arn
  container_definitions = jsonencode([{
    name        = "${var.name}-container-${var.environment}"
    image       = "${var.container_image}:latest"
    essential   = true
    environment = var.container_environment
    portMappings = [{
      protocol      = "tcp"
      containerPort = var.container_port
      hostPort      = var.container_port
    }]
    logConfiguration = {
      logDriver = "awslogs"
      options = {
        awslogs-group         = aws_cloudwatch_log_group.main.name
        awslogs-stream-prefix = "ecs"
        awslogs-region        = var.region
      }
    }
    secrets = var.container_secrets
  }])

  tags = {
    # Name        = "${var.name}-task-${var.environment}"
    Environment = var.environment
    Name = "genesis-ecs-task-definition"
    Manager = "terraform"
  }
}

And in that child module I have the following in the outputs.tf file:

output "container_image" {
    value = jsondecode(aws_ecs_task_definition.main.container_definitions)[0]["image"]
}

I want to output the value of the container_definitions image value only in the parent module but my solutions don't work as I get a blank output:

container_image = {}

Any pointers on how to do this?

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.