Search code examples
amazon-web-servicesshellenvironment-variablesamazon-elastic-beanstalkebextensions

Run elastic beanstalk .ebextensions config for specific environments


I have the following config

0_logdna.config
commands:
  01_install_logdna:
    command: "/home/ec2-user/logdna.sh"
  02_restart_logdna:
    command: "service logdna-agent restart"

files:
  "/home/ec2-user/logdna.sh" :
    mode: "000777"
    owner: root
    group: root
    content: |
      #!/bin/sh
      RACK_ENV=$(/opt/elasticbeanstalk/bin/get-config environment -k RACK_ENV)
      echo "$RACK_ENV"
      if [ $RACK_ENV == production ]
      then
        rpm --import https://repo.logdna.com/logdna.gpg
        echo "[logdna]
        name=LogDNA packages
        baseurl=https://repo.logdna.com/el6/
        enabled=1
        gpgcheck=1
        gpgkey=https://repo.logdna.com/logdna.gpg" | sudo tee /etc/yum.repos.d/logdna.repo
        LOGDNA_INGESTION_KEY=$(/opt/elasticbeanstalk/bin/get-config environment -k LOGDNA_INGESTION_KEY)
        yum -y install logdna-agent
        logdna-agent -k $LOGDNA_INGESTION_KEY # this is your unique Ingestion Key
        # /var/log is monitored/added by default (recursively), optionally add more dirs here
        logdna-agent -d /var/app/current/log/logstasher.log
        logdna-agent -d /var/app/containerfiles/logs/sidekiq.log
        # logdna-agent --hostname allows you to pass your AWS env metadata to LogDNA (remove # to uncomment the line below)
        # logdna-agent --hostname `{"Ref": "AWSEBEnvironmentName" }`
        # logdna -t option allows you to tag the host with tags (remove # to uncomment the line below)
        #logdna-agent -t `{"Ref": "AWSEBEnvironmentName" }`
        chkconfig logdna-agent on
        service logdna-agent start
      fi

I want to be able to only run this config for my production environment but each time i run this code, i get an error that says

ERROR   [Instance: i-091794aa00f84ab36,i-05b6d0824e7a0f5da] Command failed on instance. Return code: 1 Output: (TRUNCATED)...not found
/home/ec2-user/logdna.sh: line 17: logdna-agent: command not found
/home/ec2-user/logdna.sh: line 18: logdna-agent: command not found
error reading information on service logdna-agent: No such file or directory
logdna-agent: unrecognized service.

Not sure why this is not working. When i echo RACK_ENV i get production as the value so i know that is correct but why is it failing my if statement and why is it not working properly?


Solution

  • Your use of echo will lead to malformed /etc/yum.repos.d/logdna.repo. To set it up properly, please use the following (indentations for EOL2 are important):

    files:
      "/home/ec2-user/logdna.sh" :
        mode: "000777"
        owner: root
        group: root
        content: |
          #!/bin/sh
          RACK_ENV=$(/opt/elasticbeanstalk/bin/get-config environment -k RACK_ENV)
          echo "$RACK_ENV"
          if [ $RACK_ENV == production ]
          then
            rpm --import https://repo.logdna.com/logdna.gpg
            cat >/etc/yum.repos.d/logdna.repo << 'EOL2'
          [logdna]
          name=LogDNA packages
          baseurl=https://repo.logdna.com/el6/
          enabled=1
          gpgcheck=1
          gpgkey=https://repo.logdna.com/logdna.gpg
          EOL2
            LOGDNA_INGESTION_KEY=$(/opt/elasticbeanstalk/bin/get-config environment -k LOGDNA_INGESTION_KEY)
            yum -y install logdna-agent
            logdna-agent -k $LOGDNA_INGESTION_KEY # this is your unique Ingestion Key
            # /var/log is monitored/added by default (recursively), optionally add more dirs here
            logdna-agent -d /var/app/current/log/logstasher.log
            logdna-agent -d /var/app/containerfiles/logs/sidekiq.log
            # logdna-agent --hostname allows you to pass your AWS env metadata to LogDNA (remove # to uncomment the line below)
            # logdna-agent --hostname `{"Ref": "AWSEBEnvironmentName" }`
            # logdna -t option allows you to tag the host with tags (remove # to uncomment the line below)
            #logdna-agent -t `{"Ref": "AWSEBEnvironmentName" }`
            chkconfig logdna-agent on
            service logdna-agent start
          fi
    

    For further troubleshooting please check /var/log/cfn-init-cmd.log file.