Systemd for auto deploy from AWS

Over the last week I’ve completely rebuilt the the only on-premise system we have at work. One of the bits I’m rather happy with is the replacement of M/Monit with just plain systemd. We didn’t use any of the advanced features of M/Monit, we only wanted ordinary process monitoring and restart on a single system. It really was a bit of overkill.

So, instead of

  • using M/Monit to monitor processes
  • a monit configuration for the app (both start and stop instructions)
  • a script to start the app and capture its PID in a file
  • a script to resync the app against the S3 bucket where Travis puts the build artifacts, and if a new version has arrived, remove the PID file thereby triggering M/Monit to restart the app
  • a crontab entry to run the sync/restart script every few minutes

we now have

  • a (simplified) script to start the app1
  • a service unit (app.service) for the app
  • a timer unit (app-sync.timer) to trigger the resync of the app against the S3 bucket
  • a oneshot service unit (app-sync.service), triggered by the timer, to perform the sync of the app with the latest build, i.e. call aws s3 sync
  • a path unit (app-restart.path) to monitor one of the build artifacts, i.e. to pick up that a new version has arrived
  • a oneshot service unit (app-restart.service), triggered by the path unit, calling systemctl restart app.service

There’s one more piece to the setup, but

  • the start script is simplified since it no longer needs to push things to the background and capture the PID
  • the sync/restart script is completely gone (arguably the more complicated of the two scripts in the M/Monit setup)
  • responsibility is cleanly separated leading to a solution that’s easier to understand (as long as you know a bit about systemd of course)

so I think it’s a net improvement.

  1. All it does is set a bunch of environment variables and then start the app, so I’m planning on moving the environment variables into a separate file and put the start command the service unit file instead.

Leave a comment