<p>I have figured out the way and using the below code snippet, we should be able to filter the ec2 instances based on the tags</p>
<pre><code>    additionalScrapeConfigs: |
  - job_name: &quot;prometheus_nginx&quot;
    ec2_sd_configs:
      - region: us-east-1
        access_key: XXXXXX
        secret_key: XXXXXX
        port: 9113
        filters:
          - name: tag:test
            values:
              - nginx
</code></pre>
<p>Thanks to chatgpt</p>

CC BY-SA 4.0
2023-09-26T13:54:22.217Z
2023-09-26T13:54:22.217Z
Siva Malla
<p>I have a requirement, where I have enabled NGINX metrics and I am trying to plot metrics with Prometheus. I am trying to plot metrics for NGINX EC2 server which has a tag named <code>test=nginx</code> or by running on specific port 9113. I am trying to use the below config, but after adding this config I am unable to see the targets in Prometheus</p>
<pre class="lang-yaml prettyprint-override"><code>- job_name: &quot;prometheus_nginx&quot;
  ec2_sd_configs:
    - region: us-east-1
      access_key: XXXXXXXXXX
      secret_key: XXXXXXXXXX/
      port: 9113
  relabel_configs:
    - source_labels: [__meta_ec2_tag_test]
      regex: nginx
      action: keep
    - source_labels: []
      regex: .*
      action: drop
</code></pre>
<p>So can someone please help in what modifications are needed. NGINX is running on EC2 and Prometheus is running as a pod. Already enabled the traffic between NGINX and Prometheus pod and I am able to plot metrics.</p>
<p>But I am looking at more feasible option which should automatically get the dynamic targets based on the tag of the EC2 server</p>

<amazon-ec2><nginx><prometheus>
How to scrape only some targets of EC2 based on tag
Siva Malla
# How to scrape only some targets of EC2 based on tag

(Asked by: username_0 on 2023-09-25)

I have a requirement, where I have enabled NGINX metrics and I am trying to plot metrics with Prometheus. I am trying to plot metrics for NGINX EC2 server which has a tag named test=nginx or by running on specific port 9113. I am trying to use the below config, but after adding this config I am unable to see the targets in Prometheus
- job_name: "prometheus_nginx"
  ec2_sd_configs:
    - region: us-east-1
      access_key: XXXXXXXXXX
      secret_key: XXXXXXXXXX/
      port: 9113
  relabel_configs:
    - source_labels: [__meta_ec2_tag_test]
      regex: nginx
      action: keep
    - source_labels: []
      regex: .*
      action: drop

So can someone please help in what modifications are needed. NGINX is running on EC2 and Prometheus is running as a pod. Already enabled the traffic between NGINX and Prometheus pod and I am able to plot metrics.
But I am looking at more feasible option which should automatically get the dynamic targets based on the tag of the EC2 server

**Tags:** amazon-ec2, nginx, prometheus


---

## 1 answers

### Answer by username_0 on 2023-09-26. Score: 1

I have figured out the way and using the below code snippet, we should be able to filter the ec2 instances based on the tags
    additionalScrapeConfigs: |
  - job_name: "prometheus_nginx"
    ec2_sd_configs:
      - region: us-east-1
        access_key: XXXXXX
        secret_key: XXXXXX
        port: 9113
        filters:
          - name: tag:test
            values:
              - nginx

Thanks to chatgpt


---

