How to Use Environmental Variables in Fluentd Config

Better Stack Team
Updated on February 5, 2024

Fluentd, starting from version v1.13.0, allows the integration of environment variables within its configuration using the syntax #{ENV['YOUR_ENV_VARIABLE']}.

For instance, if you've defined an environment variable SECRET_KEY in your system, it can be used in the Fluentd configuration file as follows:

some_field "#{ENV['SECRET_KEY']}"

Remember to enclose the syntax in double quotes, not single quotes.

This setup dynamically assigns the value of the SECRET_KEY environment variable to some_field.

To illustrate with a more specific example, consider a match block in Fluentd's configuration used for forwarding logs to Better Stack, which requires a source token like W8QhqAQ82MVn1nqgPZoe2T9G. The environment variable can be utilized in this context as shown:

<label /etc/fluent/fluentd.conf>
  <match docker.nginx.**>
    @type logtail
    @id output_nginx_logtail
    source_token "#{ENV['SOURCE_TOKEN']}"
    flush_interval 2 # in seconds

Additionally, environment variables can be employed for setting tags. For example, using the FLUENTD_TAG environment variable to specify a tag would look like this:

<match "docker.#{ENV['FLUENTD_TAG']}">

For a comprehensive understanding of Fluentd and its capabilities in log data management, you can refer to this guide on collecting, processing, and shipping log data with Fluentd.

🔭 Want to centralize and monitor your logs?

Go to Logtail and start your log management in 5 minutes.

Better Uptime Dashboard

Make your mark

Join the writer's program

Are you a developer and love writing and sharing your knowledge with the world? Join our guest writing program and get paid for writing amazing technical guides. We'll get them to the right readers that will appreciate them.

Write for us
Writer of the month
Marin Bezhanov
Marin is a software engineer and architect with a broad range of experience working...
Build on top of Better Stack

Write a script, app or project on top of Better Stack and share it with the world. Make a public repository and share it with us at our email.

or submit a pull request and help us build better products for everyone.

See the full list of amazing projects on github