Search code examples
amazon-web-servicesamazon-ec2systemd

How to run User Data of AWS EC2 instance before starting systemd service?


I use Prometheus on an EC2 instance and want to put the data (e.g. in /var/lib/prometheus/metrics2) on a mounted device. In order to mount the device I use a script in User Data of AWS EC2. I use terraform code to provide the User Data

resource "aws_instance" "prometheus" {
  ...
  user_data =  "${file("userdata.sh")}"
}

The file userdata.sh contains mount $DEVICE $MOUNT_POINT

Prometheus is managed/enabled with systemd.

However, it seems that systemd starts Prometheus before the User Data script is executed, thus the mounted drive is initially not used by Prometheus. Is there any possibility to run the User Data script before (!) starting the systemd service?


Solution

  • Based on the comments.

    The workaround was to restart the prometheus service after setting up the new mount points.