title: AWS Elasticsearch is not deleting even if count variable's values is zero
body: <p>I am creating Elasticsearch and I am using count variable with value of 0 or 1.</p>
  <p>My code:</p>
  <pre><code>resource &quot;aws_elasticsearch_domain&quot; &quot;es&quot; {
    count                 = &quot;${var.enable_pipeline_1 ? 1 : 0}&quot;
    domain_name           = &quot;${var.name_prefix}&quot;
    elasticsearch_version = &quot;6.8&quot;
  
    cluster_config {
      instance_type = &quot;t2.small.elasticsearch&quot;
    }
  
    vpc_options {
      subnet_ids = [
        &quot;${aws_subnet.selected.id}&quot;,
      ]
  
      security_group_ids = [&quot;${aws_security_group.security_group.id}&quot;]
    }
  
    advanced_options = {
      &quot;rest.action.multi.allow_explicit_index&quot; = &quot;true&quot;
    }
  
    access_policies = &lt;&lt;CONFIG
  {
      &quot;Version&quot;: &quot;2012-10-17&quot;,
      &quot;Statement&quot;: [
          {
              &quot;Action&quot;: &quot;es:*&quot;,
              &quot;Principal&quot;: &quot;*&quot;,
              &quot;Effect&quot;: &quot;Allow&quot;,
              &quot;Resource&quot;: &quot;arn:aws:es:${var.aws_region}:${var.aws_account_id}:domain/${var.name_prefix}/*&quot;
          }
      ]
  }
  CONFIG
  
    snapshot_options {
      automated_snapshot_start_hour = 23
    }
  
    tags = {
      Domain = &quot;ES-${var.name_prefix}&quot;
    }
  
    ebs_options {
      ebs_enabled = true
      volume_type = &quot;standard&quot;
      volume_size = &quot;${var.elasticserch_disk_size}&quot;
    }
  
    log_publishing_options {
      cloudwatch_log_group_arn = &quot;${aws_cloudwatch_log_group.es_log_resource_policy.arn}&quot;
      log_type                 = &quot;INDEX_SLOW_LOGS&quot;
    }
  
    depends_on = [
      &quot;aws_iam_service_linked_role.es&quot;,
    ]
  }
  
  resource &quot;aws_cloudwatch_log_group&quot; &quot;es_log_resource_policy&quot; {
    count = &quot;${var.enable_pipeline_1 ? 1 : 0}&quot;
    name  = &quot;${var.name_prefix}_es_log_group&quot;
  }
  
  resource &quot;aws_cloudwatch_log_resource_policy&quot; &quot;es_log_resource_policy&quot; {
    count       = &quot;${var.enable_pipeline_1 ? 1 : 0}&quot;
    policy_name = &quot;${var.name_prefix}_es_log_resource_policy&quot;
  
    policy_document = &lt;&lt;CONFIG
  {
    &quot;Version&quot;: &quot;2012-10-17&quot;,
    &quot;Statement&quot;: [
      {
        &quot;Effect&quot;: &quot;Allow&quot;,
        &quot;Principal&quot;: {
          &quot;Service&quot;: &quot;es.amazonaws.com&quot;
        },
        &quot;Action&quot;: [
          &quot;logs:PutLogEvents&quot;,
          &quot;logs:PutLogEventsBatch&quot;,
          &quot;logs:CreateLogStream&quot;
        ],
        &quot;Resource&quot;: &quot;arn:aws:logs:*&quot;
      }
    ]
  }
  CONFIG
  }
  
  resource &quot;aws_iam_service_linked_role&quot; &quot;es&quot; {
    count            = &quot;${var.enable_pipeline_1 ? 1 : 0}&quot;
    aws_service_name = &quot;es.amazonaws.com&quot;
  }
  </code></pre>
  <p>So I have 2 problems now:</p>
  <p>1st problem: If I set count value to zero then the elasticsearch domain should delete if exist but it is not deleting.</p>
  <p>2nd problem: So I deleted domain manually on aws console and now I want to create domain but error said:</p>
  <pre><code>aws_iam_service_linked_role.es: Error creating service-linked role with name es.amazonaws.com: InvalidInput: Service role name AWSServiceRoleForAmazonElasticsearchService has been taken in this account, please try a different suffix.
  </code></pre>
  <p>I don't understand what is going wrong, any help please?</p>
  <p>Thank you.</p>