logstash ec2 instanceid

October 2, 2013

How to get Logstash to read your ec2 instance id

logstash.sh

!/bin/bash

EC2_INSTANCE_ID=$(curl -s http://169.254.169.254/latest/meta-data/instance-id)
export EC2_INSTANCE_ID
conf=/opt/logstash/simple.conf
lsjar=/opt/logstash/logstash.jar
myjava=$(which java)
if [ -z $myjava ]; then
  echo "java is required; please install openjdk or jre"
  exit 1
fi
# spawn logstash
$myjava -jar $lsjar agent -f $conf

config file /opt/logstash/simple.conf

input {
  exec {
    type => "instance-id"
    command => "echo -e -n ${EC2_INSTANCE_ID}"
    interval => 5
  }
}
filter {
  grok {
    type => "instance-id"
    pattern => "[A-Za-z0-9_-]+"
    named_captures_only => true
  }
}
output {
  stdout { debug => true debug_format => "json" }
}