Answers: <p>You can also enable container insights in the <a href="https://docs.aws.amazon.com/AmazonECS/latest/developerguide/ecs-account-settings.html#container-insights-setting" rel="nofollow noreferrer">account settings of Amazon Elastic Container Service</a>.</p>
  <pre class="lang-none prettyprint-override"><code>resource &quot;aws_ecs_account_setting_default&quot; &quot;container_insights&quot; {
    name  = &quot;containerInsights&quot;
    value = &quot;enabled&quot;
  }
  </code></pre>
  <p>When you opt in to the <code>containerInsights</code> account setting, all <strong>new</strong> clusters have Container Insights enabled by default. As far as I see, you must enable it manually for existing clusters.</p>
  <p>See also <a href="https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/deploy-container-insights-ECS-cluster.html#deploy-container-insights-ECS-new" rel="nofollow noreferrer">this docs</a> page if you want to enable it via the AWS Console.</p>
  
Answers: CC BY-SA 4.0
Answers: 2024-11-25T10:48:59.507Z
Answers: 2024-11-25T10:48:59.507Z
Answers: Rafael-WO
Answers: <p>The only solution I found is to import the created resource in AWS to a new configured resource in your Terraform configuration.</p>
  <p>If the newly created ECS Cluster is named <code>AWSBatch-foo-123456</code> than append to your Terraform configuration the following resource:</p>
  <pre><code>resource &quot;aws_ecs_cluster&quot; &quot;foo-cluster&quot; {
    name = &quot;AWSBatch-foo-123456&quot;
    
    setting {
      name  = &quot;containerInsights&quot;
      value = &quot;enabled&quot;
    }
  }
  </code></pre>
  <p>and then execute <code>terraform import aws_ecs_cluster.foo-cluster AWSBatch-foo-123456</code>.</p>
  <p>Now, the ECS cluster will be managed by Terraform and you can apply any setting on it.</p>
  
Answers: CC BY-SA 4.0
Answers: 2023-01-20T10:23:27.440Z
Answers: 2023-01-20T10:23:27.440Z
Answers: Ohav
Body: <p>I have an AWS Batch compute environment defined in my Terraform files. The creation of the compute environment creates an ECS Cluster, where the containers are being run.</p>
  <p>The issue I have with Terraform, is that while the <code>aws_batch_compute_environment</code> resource is managed by Terraform, then the <code>aws_ecs_cluster</code> which is created from it is not.</p>
  <p>I want to enable ContainerInsights on this cluster, but because it is not managed I am struggling to understand how to do it.</p>
  <p>This is how my Batch compute environment is currently defined:</p>
  <pre><code>resource &quot;aws_batch_compute_environment&quot; &quot;fargate-batch-compute&quot; {
    compute_environment_name = &quot;fargate-batch-compute&quot;
  
    compute_resources {
      max_vcpus          = 100
      security_group_ids = [...]
      subnets            = [...]
      type               = &quot;FARGATE&quot;
    }
  
    type = &quot;MANAGED&quot;
  }
  </code></pre>
  <p>This is how to easily enable ContainerInsights on an ECS cluster using Terraform:</p>
  <pre><code>resource &quot;aws_ecs_cluster&quot; &quot;foo&quot; {
    name = &quot;&quot;
  
    setting {
      name  = &quot;containerInsights&quot;
      value = &quot;enabled&quot;
    }
  }
  </code></pre>
  <p><strong>Notes:</strong></p>
  <ul>
  <li>I checked and it seems that it's not possible to pass an existing ECS cluster to the creation of the compute environment - <a href="https://stackoverflow.com/a/49983869/11808461">Reference</a></li>
  </ul>
  
Tags: <terraform>
Title: How to enable ContainerInsights (setting) on an ECS cluster created from a Batch compute environment using Terraform?
UserDisplayName: Ohav
ThreadText: # How to enable ContainerInsights (setting) on an ECS cluster created from a Batch compute environment using Terraform?
  
  (Asked by: username_0 on 2023-01-20)
  
  I have an AWS Batch compute environment defined in my Terraform files. The creation of the compute environment creates an ECS Cluster, where the containers are being run.
  
  The issue I have with Terraform, is that while the `aws_batch_compute_environment` resource is managed by Terraform, then the `aws_ecs_cluster` which is created from it is not.
  
  I want to enable ContainerInsights on this cluster, but because it is not managed I am struggling to understand how to do it.
  
  This is how my Batch compute environment is currently defined:
  
  ```
  resource "aws_batch_compute_environment" "fargate-batch-compute" {
    compute_environment_name = "fargate-batch-compute"
  
    compute_resources {
      max_vcpus          = 100
      security_group_ids = [...]
      subnets            = [...]
      type               = "FARGATE"
    }
  
    type = "MANAGED"
  }
  
  ```
  
  This is how to easily enable ContainerInsights on an ECS cluster using Terraform:
  
  ```
  resource "aws_ecs_cluster" "foo" {
    name = ""
  
    setting {
      name  = "containerInsights"
      value = "enabled"
    }
  }
  
  ```
  
  **Notes:**
  
  * I checked and it seems that it's not possible to pass an existing ECS cluster to the creation of the compute environment - [Reference](https://stackoverflow.com/a/49983869/11808461)
  
  **Tags:** terraform
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_1 on 2024-11-25. Score: 0
  
  You can also enable container insights in the [account settings of Amazon Elastic Container Service](https://docs.aws.amazon.com/AmazonECS/latest/developerguide/ecs-account-settings.html#container-insights-setting).
  
  ```none
  resource "aws_ecs_account_setting_default" "container_insights" {
    name  = "containerInsights"
    value = "enabled"
  }
  
  ```
  
  When you opt in to the `containerInsights` account setting, all **new** clusters have Container Insights enabled by default. As far as I see, you must enable it manually for existing clusters.
  
  See also [this docs](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/deploy-container-insights-ECS-cluster.html#deploy-container-insights-ECS-new) page if you want to enable it via the AWS Console.
  
  
  ---
  
  ### Answer by username_0 on 2023-01-20. Score: 0
  
  The only solution I found is to import the created resource in AWS to a new configured resource in your Terraform configuration.
  
  If the newly created ECS Cluster is named `AWSBatch-foo-123456` than append to your Terraform configuration the following resource:
  
  ```
  resource "aws_ecs_cluster" "foo-cluster" {
    name = "AWSBatch-foo-123456"
    
    setting {
      name  = "containerInsights"
      value = "enabled"
    }
  }
  
  ```
  
  and then execute `terraform import aws_ecs_cluster.foo-cluster AWSBatch-foo-123456`.
  
  Now, the ECS cluster will be managed by Terraform and you can apply any setting on it.
  
  
  ---
  
  