Can you use environment variables in a Fluentd configuration?

Reference environment variables in Fluentd, require missing values explicitly, and supply settings through a shell, systemd, or Kubernetes.

Best practices
Illustration of log collection and monitoring

Yes. Fluentd evaluates Ruby expressions inside double-quoted configuration values, so "#{ENV['ES_HOST']}" reads an environment variable when the configuration loads. The variable must be available to the Fluentd process, not just to an unrelated terminal session.

Reference a variable

With the Elasticsearch output plugin installed, the connection settings can come from the environment:

<match app.**>
  @type elasticsearch
  host "#{ENV.fetch('ES_HOST')}"
  port "#{ENV.fetch('ES_PORT', '9200')}"
  user "#{ENV.fetch('ES_USER')}"
  password "#{ENV.fetch('ES_PASSWORD')}"
</match>

This fragment assumes the input and the destination's TLS settings are configured separately. ENV.fetch without a default raises an error when a required variable is absent. It does not reject an explicitly empty value, so validate those in your deployment configuration too.

For a shell launch, export the non-secret settings and provide credentials through your deployment's secret mechanism:

export ES_HOST=elasticsearch.internal
export ES_PORT=9200
fluentd --dry-run -c fluent.conf
fluentd -c fluent.conf

The dry run checks configuration loading; it does not prove that Elasticsearch authentication or delivery will succeed. Send a sample event after startup and confirm it arrives.

Where this helps

Environment variables let you reuse a configuration across development, staging, and production without putting destination passwords in the file. Containers can receive settings at launch, and Kubernetes can populate a variable from a Secret using valueFrom.secretKeyRef. Environment variables still require access controls: avoid dumping the process environment or logging credential values.

An unset ENV['NAME'] returns nil, which ordinary string interpolation renders as an empty string. Whether that is accepted depends on the plugin and parameter. It is not safe to assume missing values never cause a startup error.

FAQ

How do I pass settings to a systemd service?

Use a service override containing Environment= or EnvironmentFile=, then reload the unit definition and restart the service. A variable exported in your login shell is not automatically inherited by systemd services.

Can I specify a default?

Yes: "#{ENV['ES_PORT'] || '9200'}" or "#{ENV.fetch('ES_PORT', '9200')}". Both retain an empty string if it is explicitly set; neither treats it as missing.

The examples follow Fluentd's documented configuration interpolation and the Elasticsearch plugin settings.

Get a tailored plan

Contact us

Join the community

Scan with WeChat
to join the community

Try Guance

Start online and pay only for what you use.

Get started

Choose a Guance plan

Code hosting