EC2 Discovery Relabelling

Let’s say you are on EC2 and wanted to use the Name tag rather than the private IP and port you’d get by default with EC2 service discovery. You could use a relabel rule like:

relabel_configs:
  - source_labels: [__meta_ec2_tag_Name]
    target_label: instance

You could go a step further and use the public rather than private IP, while also setting the instance label:

relabel_configs:
 - source_labels: [__meta_ec2_public_ip]
   regex:  '(.*)'             # This is the default value.
   target_label: __address__
   replacement: '${1}:9100'   # Have to specify a port too.
 - source_labels: [__meta_ec2_tag_Name]
   target_label: instance

You can also keep only the running instances.

    relabel_configs:
       - source_labels: [__meta_ec2_instance_state]
         regex: running
         action: keep

See http://www.robustperception.io/controlling-the-instance-label/ for more information.

Additional EC2 tips:

use public IP if available, else use private IP:

relabel_configs:
 - source_labels: [__meta_ec2_public_ip]
   regex:  '([1-9]{1,3}\.[1-9]{1,3}\.[1-9]{1,3}\.[1-9]{1,3})'
   target_label: __address__
   replacement: '${1}:9100'

Capture all EC2 tags dynamically:

relabel_configs:
- action: labelmap
  regex: __meta_ec2_tag_(.+)

Strip commas from Subnet ID:

- source_labels: [__meta_ec2_subnet_id]
   regex:         ',(.*),'
   replacement: '${1}'
   target_label: ec2_subnet_id

Rate this tip

If you think this tip by bbrazilbbrazil is useful — rate it up!

rating: +3+x
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License