logstash ec2 instanceid

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

1 comment to logstash ec2 instanceid

Leave a Reply

To create code blocks or other preformatted text, indent by four spaces:

    This will be displayed in a monospaced font. The first four 
    spaces will be stripped off, but all other whitespace
    will be preserved.
    
    Markdown is turned off in code blocks:
     [This is not a link](http://example.com)

To create not a block, but an inline code span, use backticks:

Here is some inline `code`.

For more help see http://daringfireball.net/projects/markdown/syntax

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>