How does EC2 service discovery work?

IntermediateConceptPrometheus Architecture
Answer

Prometheus calls the EC2 API (needs ec2:DescribeInstances, usually via an instance role) and creates targets on the private IP with a set port, exposing __meta_ec2_tag_<Name>, __meta_ec2_instance_id, __meta_ec2_availability_zone and so on.

- job_name: ec2-node
  ec2_sd_configs:
    - region: ap-south-1
      port: 9100
      filters:
        - name: tag:monitoring
          values: ["enabled"]
  relabel_configs:
    - source_labels: [__meta_ec2_tag_Name]
      target_label: name
All Monitoring & Observability questions Roadmap: Monitoring & alerting

More on Prometheus Architecture