# Setting en variable for filebeat in ebextension

**URL:** <https://discuss.elastic.co/t/setting-en-variable-for-filebeat-in-ebextension/348465>\
**Category:** Beats\
**Tags:** elastic-stack-monitoring, elastic-stack-alerting, filebeat\
**Created:** [December 2, 2023, 1:02am UTC](https://discuss.elastic.co/t/setting-en-variable-for-filebeat-in-ebextension/348465 "2023-12-02T01:02:31Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![apsh](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/apsh/32/126751_2.png) [@apsh](https://discuss.elastic.co/u/apsh)\
**Post date:** [December 2, 2023, 1:02am UTC](https://discuss.elastic.co/t/setting-en-variable-for-filebeat-in-ebextension/348465/1 "2023-12-02T01:02:31Z")

</div>

I've been encountering an issue with my Filebeat setup on AWS Elastic Beanstalk. I'm trying to dynamically set the environment name in my Filebeat configuration using an environment variable ($ENVIRONMENT). However, it seems that the changes are not being applied when I deploy my configuration.

```auto
###################### Filebeat Configuration #########################

commands:
  01_stop_filebeat:
    test: "[-x /usr/bin/filebeat]"
    command: "systemctl stop filebeat"
  02_remove_filebeat:
    test: "[-x /usr/bin/filebeat]"
    command: "yum remove -y filebeat"
  03_remove_directories:
    command: "sudo rm -rf /etc/filebeat"
  04_download_filebeat:
    command: "curl -L -O https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-8.10.3-x86_64.rpm"
    cwd: /home/ec2-user
  05_install_filebeat:
    command: "sudo rpm -vi filebeat-8.10.3-x86_64.rpm"
    cwd: /home/ec2-user
  06_set_filebeat_config:
    command: |
      if [["$ENVIRONMENT" == "staging"]]; then
        NAME="BlueAPI-Staging"
        ENV="staging"
      elif [["$ENVIRONMENT" == "production"]]; then
        NAME="BlueAPI-Production"
        ENV="production"
      fi
      echo "filebeat.inputs:
      - type: filestream
        id: my-filestream-id
        enabled: true
        paths:
          - /var/log/web.stdout.log
        fields:
          server: "blueapi"
          env: '${ENV}'
        name: '${NAME}'
      output.logstash:
        hosts: ['XX:5044']" | sudo tee /etc/filebeat/filebeat.yml

    command: "systemctl enable filebeat"
  08_start_filebeat:
    command: "systemctl start filebeat"

```

---

<div class="post-metadata">

**Author:** ![apsh](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/apsh/32/126751_2.png) [@apsh](https://discuss.elastic.co/u/apsh)\
**Post date:** [December 4, 2023, 12:13am UTC](https://discuss.elastic.co/t/setting-en-variable-for-filebeat-in-ebextension/348465/2 "2023-12-04T00:13:58Z")

</div>

I am posting the resolution here for anyone who is interested to know :

I solved the problem by changing the format to the below :

```auto

container_commands:
  01_stop_filebeat:
    test: "[-x /usr/bin/filebeat]"
    command: "systemctl stop filebeat"

  02_remove_filebeat:
    test: "[-x /usr/bin/filebeat]"
    command: "yum remove -y filebeat"

  03_remove_directories:
    command: "sudo rm -rf /etc/filebeat"

  04_download_filebeat:
    command: "curl -L -O https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-8.10.3-x86_64.rpm"
    cwd: /home/ec2-user

  05_install_filebeat:
    command: "sudo rpm -vi filebeat-8.10.3-x86_64.rpm"
    cwd: /home/ec2-user

  06_set_filebeat_config:
    command: |
      echo "filebeat.inputs:
      - type: filestream
        id: my-filestream-id
        enabled: true
        paths:
          - /var/log/web.stdout.log
        fields:
          server: 'blueapi'
          env: "${ENV}"
        name: "${NAME}"
      output.logstash:
        hosts: ['XXXX:5044']" | sudo tee /etc/filebeat/filebeat.yml

  07_enable_filebeat:
    command: "systemctl enable filebeat"

  08_start_filebeat:
    command: "systemctl start filebeat"

```

Just a quick note - you need to change the command to container\_command. Also, I didn't test the if statement after making changes. I just defined the env variable in the Beanstalk config and it works.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [January 1, 2024, 2:14am UTC](https://discuss.elastic.co/t/setting-en-variable-for-filebeat-in-ebextension/348465/3 "2024-01-01T02:14:27Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
