3

Is there some way to trigger an event (e.g. running a script to push some logs to S3) when an EC2 instance is stopped/terminated?

I have looked into triggering the script using a service in /usr/lib/systemd/system but I haven't had any luck with that yet. I have heard that networking capabilities on the instance can be shutdown before a service is triggered and if true, that could be why the script is not executing correctly.

user1015492
  • 155
  • 2
  • 10

2 Answers2

6

So the answer is not really AWS specific, but it is working for me now (tested on EC2 instance stopping and terminating).

  1. I've created a system.d service file:

/usr/lib/systemd/system/my_shutdown.service

[Unit]
Description=my_shutdown Service
Before=shutdown.target reboot.target halt.target
Requires=network-online.target network.target

[Service]
KillMode=none
ExecStart=/bin/true
ExecStop=/path/to/my_script.sh
RemainAfterExit=yes
Type=oneshot

[Install]
WantedBy=multi-user.target
  1. Added this service to multi-user.target:

systemctl enable my_shutdown.service

Alternatively you can manually create the symlink:

ln -s /usr/lib/systemd/system/my_shutdown.service /etc/systemd/system/multi-user.target.wants/my_shutdown.service

  1. Started the service and tested by stopping/terminating the instance.

systemctl start my_shutdown.service

My understanding:

  • Description: a description of our service.
  • Before: we want our service to stop before these targets are started.
  • Requires: our service requires that network capabilities are available. These targets must not be stopped before our service starts/stops.
  • KillMode: none; do not kill our process.
  • ExecStart: /bin/true; a command that does nothing but returns a success. Run when are service is started.
  • ExecStop: the script to run. Run when are service is being stopped.
  • RemainAfterExit: consider our service active even when all its processes exited.
  • Type: oneshot; it is expected that the process has to exit before systemd starts follow-up units.
  • WantedBy: the target we want to add our service to.

References:

  1. https://www.freedesktop.org/software/systemd/man/systemd.service.html
  2. https://www.freedesktop.org/software/systemd/man/systemd.kill.html#
  3. https://www.freedesktop.org/software/systemd/man/systemd.special.html
  4. https://www.freedesktop.org/software/systemd/man/systemd.target.html
user1015492
  • 155
  • 2
  • 10
1

You can trigger events, such as pushing logs to S3 on specific events, with CloudWatch... Learn more here: https://aws.amazon.com/cloudwatch/

hephalump
  • 5,860
  • 1
  • 22
  • 23
  • 1
    A deeper link to [Cloudwatch EC2 Events](http://docs.aws.amazon.com/AmazonCloudWatch/latest/DeveloperGuide/EventTypes.html#ec2_event_type) would probably be helpful, here. However, once an instance starts shutting down, it's probably a little bit late to think about trying to preserve logs or export anything. The system stops instances by a simulated short-press of the power button. Auto scaling groups have interruptible state changes and spot instances have a 2 minute warning, but for other instances, an autonomous, internal cleanup action on shutdown may not be practical. – Michael - sqlbot Apr 19 '16 at 23:33
  • Point taken, Michael-sqlbot. Thanks for the deeper link. – hephalump Apr 19 '16 at 23:42