prometheus.yml for EC2 service discovery, what metadata is exposed for discovered targets, and how to validate the configuration in the Prometheus UI.
Use an IAM user with the minimum permission set required: the AWS-managed policy AmazonEC2ReadOnlyAccess. For higher security, prefer using an instance role (EC2 IAM role) for Prometheus or an external secrets manager instead of hard-coding access keys in
prometheus.yml.Prerequisites
- A Prometheus server with network reachability to the EC2 instances you want to scrape.
- An AWS account and permission to create IAM users/policies.
- Knowledge of the AWS region(s) you want to monitor (for example
us-east-1).
1) Create an IAM user for Prometheus
Create a new IAM user that Prometheus will use for EC2 discovery. This user only needs programmatic access and the Amazon EC2 Read Only Access policy attached.
- In the IAM console: Users → Create user.
- Select Programmatic access so you can obtain an Access Key ID and Secret Access Key.
- Attach the AWS managed policy AmazonEC2ReadOnlyAccess.

- Skip tags (optional), review, and create the user.

- After creation, record the Access Key ID and Secret Access Key. You’ll need them for the Prometheus config.

Do NOT commit AWS keys to version control. Prefer instance roles, environment variables, or a secrets manager (e.g., AWS Secrets Manager) for production deployments. Hard-coding keys in
prometheus.yml is only acceptable for short-term testing.2) Configure Prometheus for EC2 service discovery
Edit your Prometheus configuration file (usually/etc/prometheus/prometheus.yml) and add an EC2 scrape job under scrape_configs. The minimal ec2_sd_configs block requires:
region: the AWS region to discover (e.g.,us-east-1).access_key: the IAM user Access Key ID (or omit if using instance role / environment credentials).secret_key: the Secret Access Key (or omit if using instance role / environment credentials).
prometheus.yml (showing only relevant parts):
- Replace
us-east-1,access_key, andsecret_keywith your region and credentials. - If Prometheus runs on an EC2 instance with an appropriate IAM role, omit
access_key/secret_keyentirely — the AWS SDK will use the instance role credentials automatically.
3) What metadata does EC2 SD provide?
Prometheus extracts many EC2 metadata fields as labels for each discovered target. These are available under Service Discovery in the Prometheus UI and can be used for relabeling or filtering. Example discovered labels for an EC2 target:meta_ec2_* labels and their meaning:
By default Prometheus uses the instance’s private IP for the
instance label and for scraping (address), because it assumes Prometheus runs near (in the same network/VPC as) the targets. If Prometheus runs outside the VPC and you need to use the public IP, use relabeling to map meta_ec2_public_ip into the __address__/instance labels — or run Prometheus within the same VPC.4) Validate in the Prometheus UI
- Open your Prometheus server web UI (e.g.,
http://<prometheus-host>:9090). - Navigate to Status → Service Discovery. You should see the EC2 job and the list of discovered instances.
- Go to Status → Targets to check reachability and scrape status:
- Up: reachable and being scraped.
- Down: unreachable (often a network/firewall issue between Prometheus and the EC2 instance).
- Network connectivity (security groups, VPC routing).
- Prometheus can reach the target port (
/metricspath). - Correct credentials/region for discovery.
Additional references
- Prometheus EC2 SD docs: https://prometheus.io/docs/prometheus/latest/configuration/configuration/#ec2_sd_config
- AWS IAM docs: https://docs.aws.amazon.com/iam/
- AWS EC2 docs: https://docs.aws.amazon.com/ec2/