title: Logstash combine data stream and index templates
body: <p>I'm trying to use <strong>data streams</strong> and <strong>index templates</strong> in logstash v7.17</p>
  <p>What is the right elasticsearch output configuration to achieve this?</p>
  <p><strong>Option 1</strong> Using data_stream in the tempalte -&gt; FAILS</p>
  <pre><code>    output {
        elasticsearch {
          hosts =&gt; [&quot;https://elasticsearch-master:9200&quot;]
          index =&gt; &quot;microservice-%{+YYYY.MM.dd}&quot;
          template =&gt; &quot;/usr/share/logstash/templates/microservices.json&quot;
          # template_overwrite =&gt; false
          template_name =&gt; &quot;microservices&quot;
        }    
      }
  </code></pre>
  <p>Content of <code>/usr/share/logstash/templates/microservices.json</code>:</p>
  <pre><code>    {
        &quot;index_patterns&quot;: &quot;microservice-*&quot;,
        &quot;template&quot;: {
          &quot;settings&quot; : {
              &quot;index&quot; : {
                &quot;number_of_shards&quot; : &quot;1&quot;,
                &quot;number_of_replicas&quot; : &quot;1&quot;
              }
            },
          &quot;mappings&quot; : {
              &quot;properties&quot; : {
                &quot;@timestamp&quot; : {
                  &quot;type&quot; : &quot;date&quot;
                },
                &quot;@version&quot; : {
                  &quot;type&quot; : &quot;keyword&quot;
                },
                &quot;host&quot; : {
                  &quot;type&quot; : &quot;keyword&quot;
                },
                &quot;level&quot; : {
                  &quot;type&quot; : &quot;keyword&quot;
                },
                &quot;service&quot; : {
                  &quot;type&quot; : &quot;keyword&quot;
                },
                &quot;type&quot; : {
                  &quot;type&quot; : &quot;keyword&quot;
                }
              }
            }
        },
        &quot;data_stream&quot; : {
            &quot;hidden&quot; : false
        }
      }
  </code></pre>
  <p>Logstash logs (debug mode):</p>
  <pre><code>18:37:34.459 [[.monitoring-logstash]-pipeline-manager] INFO  logstash.outputs.elasticsearchmonitoring - Config is not compliant with data streams. `data_stream =&gt; auto` resolved to `false`
  18:37:34.470 [Ruby-0-Thread-14: :1] INFO  logstash.outputs.elasticsearchmonitoring - Config is not compliant with data streams. `data_stream =&gt; auto` resolved to `false`
  18:37:34.564 [[.monitoring-logstash]-pipeline-manager] WARN  logstash.javapipeline - 'pipeline.ordered' is enabled and is likely less efficient, consider disabling if preserving event order is not necessary
  18:37:34.666 [[main]-pipeline-manager] WARN  logstash.outputs.elasticsearch - Restored connection to ES instance {:url=&gt;&quot;https://elastic:xxxxxx@elasticsearch-master:9200/&quot;}
  18:37:34.678 [[main]-pipeline-manager] INFO  logstash.outputs.elasticsearch - Elasticsearch version determined (7.16.3) {:es_version=&gt;7}
  18:37:34.737 [[main]-pipeline-manager] WARN  logstash.outputs.elasticsearch - Detected a 6.x and above cluster: the `type` event field won't be used to determine the document _type {:es_version=&gt;7}
  18:37:34.850 [[main]-pipeline-manager] DEBUG logstash.outputs.elasticsearch - Not eligible for data streams because ecs_compatibility is not enabled. Elasticsearch data streams require that events adhere to the Elastic Common Schema. While `ecs_compatibility` can be set for this individual Elasticsearch output plugin, doing so will not fix schema conflicts caused by upstream plugins in your pipeline. To avoid mapping conflicts, you will need to use ECS-compatible field names and datatypes throughout your pipeline. Many plugins support an `ecs_compatibility` mode, and the `pipeline.ecs_compatibility` setting can be used to opt-in for all plugins in a pipeline. 
  18:37:34.850 [[main]-pipeline-manager] INFO  logstash.outputs.elasticsearch - Config is not compliant with data streams. `data_stream =&gt; auto` resolved to `false`
  18:37:35.059 [Ruby-0-Thread-17: :1] INFO  logstash.outputs.elasticsearch - Using mapping template from {:path=&gt;&quot;/usr/share/logstash/templates/microservices.json&quot;}
  18:37:35.067 [Ruby-0-Thread-17: :1] DEBUG logstash.outputs.elasticsearch - Attempting to install template {:template=&gt;{&quot;index_patterns&quot;=&gt;&quot;microservice-*&quot;, &quot;template&quot;=&gt;{&quot;settings&quot;=&gt;{&quot;index&quot;=&gt;{&quot;number_of_shards&quot;=&gt;&quot;1&quot;, &quot;number_of_replicas&quot;=&gt;&quot;1&quot;, &quot;refresh_interval&quot;=&gt;&quot;5s&quot;}}, &quot;mappings&quot;=&gt;{&quot;properties&quot;=&gt;{&quot;@timestamp&quot;=&gt;{&quot;type&quot;=&gt;&quot;date&quot;}, &quot;@version&quot;=&gt;{&quot;type&quot;=&gt;&quot;keyword&quot;}, &quot;host&quot;=&gt;{&quot;type&quot;=&gt;&quot;text&quot;, &quot;fields&quot;=&gt;{&quot;keyword&quot;=&gt;{&quot;type&quot;=&gt;&quot;keyword&quot;, &quot;ignore_above&quot;=&gt;256}}}, &quot;level&quot;=&gt;{&quot;type&quot;=&gt;&quot;keyword&quot;}, &quot;service&quot;=&gt;{&quot;type&quot;=&gt;&quot;keyword&quot;}, &quot;type&quot;=&gt;{&quot;type&quot;=&gt;&quot;keyword&quot;}}}}, &quot;data_stream&quot;=&gt;{&quot;hidden&quot;=&gt;false}}}
  18:37:35.135 [[main]-pipeline-manager] INFO  logstash.javapipeline - Starting pipeline {:pipeline_id=&gt;&quot;main&quot;, &quot;pipeline.workers&quot;=&gt;1, &quot;pipeline.batch.size&quot;=&gt;125, &quot;pipeline.batch.delay&quot;=&gt;50, &quot;pipeline.max_inflight&quot;=&gt;125, &quot;pipeline.sources&quot;=&gt;[&quot;/usr/share/logstash/pipeline/logstash.conf&quot;, &quot;/usr/share/logstash/pipeline/uptime.conf&quot;], :thread=&gt;&quot;#&lt;Thread:0x38ca1ed0@/usr/share/logstash/logstash-core/lib/logstash/pipeline_action/create.rb:54 run&gt;&quot;}
  18:37:35.136 [[.monitoring-logstash]-pipeline-manager] INFO  logstash.javapipeline - Starting pipeline {:pipeline_id=&gt;&quot;.monitoring-logstash&quot;, &quot;pipeline.workers&quot;=&gt;1, &quot;pipeline.batch.size&quot;=&gt;2, &quot;pipeline.batch.delay&quot;=&gt;50, &quot;pipeline.max_inflight&quot;=&gt;2, &quot;pipeline.sources&quot;=&gt;[&quot;monitoring pipeline&quot;], :thread=&gt;&quot;#&lt;Thread:0x3d81d305 run&gt;&quot;}
  18:37:35.346 [Ruby-0-Thread-17: :1] INFO  logstash.outputs.elasticsearch - Installing Elasticsearch template {:name=&gt;&quot;microservices&quot;}
  18:37:35.677 [Ruby-0-Thread-17: :1] ERROR logstash.outputs.elasticsearch - Failed to install template {:message=&gt;&quot;Got response code '400' contacting Elasticsearch at URL 'https://elasticsearch-master:9200/_template/microservices'&quot;, :exception=&gt;LogStash::Outputs::ElasticSearch::HttpClient::Pool::BadResponseCodeError, :backtrace=&gt;[&quot;/usr/share/logstash/vendor/bundle/jruby/2.5.0/gems/logstash-output-elasticsearch-11.4.1-java/lib/logstash/outputs/elasticsearch/http_client/manticore_adapter.rb:84:in `perform_request'&quot;, &quot;/usr/share/logstash/vendor/bundle/jruby/2.5.0/gems/logstash-output-elasticsearch-11.4.1-java/lib/logstash/outputs/elasticsearch/http_client/pool.rb:324:in `perform_request_to_url'&quot;, &quot;/usr/share/logstash/vendor/bundle/jruby/2.5.0/gems/logstash-output-elasticsearch-11.4.1-java/lib/logstash/outputs/elasticsearch/http_client/pool.rb:311:in `block in perform_request'&quot;, &quot;/usr/share/logstash/vendor/bundle/jruby/2.5.0/gems/logstash-output-elasticsearch-11.4.1-java/lib/logstash/outputs/elasticsearch/http_client/pool.rb:398:in `with_connection'&quot;, &quot;/usr/share/logstash/vendor/bundle/jruby/2.5.0/gems/logstash-output-elasticsearch-11.4.1-java/lib/logstash/outputs/elasticsearch/http_client/pool.rb:310:in `perform_request'&quot;, &quot;/usr/share/logstash/vendor/bundle/jruby/2.5.0/gems/logstash-output-elasticsearch-11.4.1-java/lib/logstash/outputs/elasticsearch/http_client/pool.rb:318:in `block in Pool'&quot;, &quot;/usr/share/logstash/vendor/bundle/jruby/2.5.0/gems/logstash-output-elasticsearch-11.4.1-java/lib/logstash/outputs/elasticsearch/http_client.rb:408:in `template_put'&quot;, &quot;/usr/share/logstash/vendor/bundle/jruby/2.5.0/gems/logstash-output-elasticsearch-11.4.1-java/lib/logstash/outputs/elasticsearch/http_client.rb:85:in `template_install'&quot;, &quot;/usr/share/logstash/vendor/bundle/jruby/2.5.0/gems/logstash-output-elasticsearch-11.4.1-java/lib/logstash/outputs/elasticsearch/template_manager.rb:29:in `install'&quot;, &quot;/usr/share/logstash/vendor/bundle/jruby/2.5.0/gems/logstash-output-elasticsearch-11.4.1-java/lib/logstash/outputs/elasticsearch/template_manager.rb:17:in `install_template'&quot;, &quot;/usr/share/logstash/vendor/bundle/jruby/2.5.0/gems/logstash-output-elasticsearch-11.4.1-java/lib/logstash/outputs/elasticsearch.rb:494:in `install_template'&quot;, &quot;/usr/share/logstash/vendor/bundle/jruby/2.5.0/gems/logstash-output-elasticsearch-11.4.1-java/lib/logstash/outputs/elasticsearch.rb:318:in `finish_register'&quot;, &quot;/usr/share/logstash/vendor/bundle/jruby/2.5.0/gems/logstash-output-elasticsearch-11.4.1-java/lib/logstash/outputs/elasticsearch.rb:283:in `block in register'&quot;, &quot;/usr/share/logstash/vendor/bundle/jruby/2.5.0/gems/logstash-output-elasticsearch-11.4.1-java/lib/logstash/plugin_mixins/elasticsearch/common.rb:149:in `block in after_successful_connection'&quot;]}
  </code></pre>
  <hr />
  <p><strong>Option 2</strong> Using data_streams in the output -&gt; FAILS</p>
  <pre><code>    output {
        elasticsearch {
          hosts =&gt; [&quot;https://elasticsearch-master:9200&quot;]
          data_stream =&gt; true
          #data_stream_type =&gt; &quot;logs&quot;
          #data_stream_dataset =&gt; &quot;microservices&quot;
          #data_stream_namespace =&gt; &quot;&quot;    
          index =&gt; &quot;microservice-%{+YYYY.MM.dd}&quot;
          template =&gt; &quot;/usr/share/logstash/templates/microservices.json&quot;
          # template_overwrite =&gt; false
          # ecs_compatibility =&gt; &quot;v1&quot;
          template_name =&gt; &quot;microservices&quot;
        }    
      }
  </code></pre>
  <p>The logs are:</p>
  <pre><code>18:41:47.356 [[main]-pipeline-manager] ERROR logstash.outputs.elasticsearch - Invalid data stream configuration, following parameters are not supported: {&quot;template&quot;=&gt;&quot;/usr/share/logstash/templates/microservices.json&quot;, &quot;template_name&quot;=&gt;&quot;microservices&quot;, &quot;index&quot;=&gt;&quot;microservice-%{+YYYY.MM.dd}&quot;}
  18:41:47.357 [Ruby-0-Thread-16: :1] ERROR logstash.outputs.elasticsearch - Invalid data stream configuration, following parameters are not supported: {&quot;template&quot;=&gt;&quot;/usr/share/logstash/templates/microservices.json&quot;, &quot;template_name&quot;=&gt;&quot;microservices&quot;, &quot;index&quot;=&gt;&quot;microservice-%{+YYYY.MM.dd}&quot;}
  18:41:47.400 [[main]-pipeline-manager] ERROR logstash.javapipeline - Pipeline error {:pipeline_id=&gt;&quot;main&quot;, :exception=&gt;#&lt;LogStash::ConfigurationError: Invalid data stream configuration: [&quot;template&quot;, &quot;template_name&quot;, &quot;index&quot;]&gt;, :backtrace=&gt;[&quot;/usr/share/logstash/vendor/bundle/jruby/2.5.0/gems/logstash-output-elasticsearch-11.4.1-java/lib/logstash/outputs/elasticsearch/data_stream_support.rb:68:in `check_data_stream_config!'&quot;, &quot;/usr/share/logstash/vendor/bundle/jruby/2.5.0/gems/logstash-output-elasticsearch-11.4.1-java/lib/logstash/outputs/elasticsearch/data_stream_support.rb:33:in `data_stream_config?'&quot;, &quot;/usr/share/logstash/vendor/bundle/jruby/2.5.0/gems/logstash-output-elasticsearch-11.4.1-java/lib/logstash/outputs/elasticsearch.rb:296:in `register'&quot;, &quot;org/logstash/config/ir/compiler/OutputStrategyExt.java:131:in `register'&quot;, &quot;org/logstash/config/ir/compiler/AbstractOutputDelegatorExt.java:68:in `register'&quot;, &quot;/usr/share/logstash/logstash-core/lib/logstash/java_pipeline.rb:232:in `block in register_plugins'&quot;, &quot;org/jruby/RubyArray.java:1821:in `each'&quot;, &quot;/usr/share/logstash/logstash-core/lib/logstash/java_pipeline.rb:231:in `register_plugins'&quot;, &quot;/usr/share/logstash/logstash-core/lib/logstash/java_pipeline.rb:589:in `maybe_setup_out_plugins'&quot;, &quot;/usr/share/logstash/logstash-core/lib/logstash/java_pipeline.rb:244:in `start_workers'&quot;, &quot;/usr/share/logstash/logstash-core/lib/logstash/java_pipeline.rb:189:in `run'&quot;, &quot;/usr/share/logstash/logstash-core/lib/logstash/java_pipeline.rb:141:in `block in start'&quot;], &quot;pipeline.sources&quot;=&gt;[&quot;/usr/share/logstash/pipeline/logstash.conf&quot;, &quot;/usr/share/logstash/pipeline/uptime.conf&quot;], :thread=&gt;&quot;#&lt;Thread:0x98bdee3 run&gt;&quot;}
  
  </code></pre>
  <p><strong>Option 3)</strong> Manually -&gt; Works</p>
  <p>First, create a index template <strong>manually</strong> via API call:
  <code>PUT _index_template/microservices</code> using <code>/usr/share/logstash/templates/microservices.json</code></p>
  <p>Then:</p>
  <pre><code>    output {
        elasticsearch {
          hosts =&gt; [&quot;https://elasticsearch-master:9200&quot;]
          index =&gt; &quot;microservice-test&quot;
          action =&gt; &quot;create&quot;
        }    
      }
  </code></pre>
  <p>But I don't want to do this manual step. I want to use the logstash output options to manage data_stream + index names + index templates.</p>