How to reference environment variables in logstash configuration file?

Julio Faerman picture Julio Faerman · Jun 2, 2014 · Viewed 14.7k times · Source

Is it possible to reference environment variables in logstash configuration?

In my case, i want to make my elasticsearch address configurable that i have set in the environment.

Answer

Franck picture Franck · Jun 3, 2015

With logstash 2.3, you can set environment variable references into Logstash plugins configuration using ${var} or $var. https://www.elastic.co/guide/en/logstash/current/environment-variables.html

Before logstash 2.3, you can use the "environment" filter plugin which is community maintained.

Documentation at : https://www.elastic.co/guide/en/logstash/current/plugins-filters-environment.html#plugins-filters-environment-add_field_from_env

How to install this plugin:

$LOGSTASH_HOME/bin/plugin install logstash-filter-environment

Source code at : https://github.com/logstash-plugins/logstash-filter-environment

The main part is:

# encoding: utf-8
require "logstash/filters/base"
require "logstash/namespace"


# Set fields from environment variables
class LogStash::Filters::Environment < LogStash::Filters::Base
  config_name "environment"

  # Specify a hash of fields to the environment variable
  # A hash of matches of `field => environment` variable
  config :add_field_from_env, :validate => :hash, :default => {}

  public
  def register
    # Nothing
  end # def register

  public
  def filter(event)
    return unless filter?(event)
    @add_field_from_env.each do |field, env|
      event[field] = ENV[env]
    end
    filter_matched(event)
  end # def filter
end # class LogStash::Filters::Environment