Setting en variable for filebeat in ebextension

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.

###################### 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"


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

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


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.

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