When you launch an instance in Amazon EC2, you have the option of passing user data to the instance that can be used to perform common automated configuration tasks and even run scripts after the instance starts. You can pass two types of user data to Amazon EC2: shell scripts and cloud-init directives.
Today we are announcing a new ability. Shutdown scripts to your EC2 instances!
Your instances will need access to the curl command in order for your shutdown scripts to function properly.
Shutdown scripts must start with the #! characters and the path to the interpreter you want to read the script (commonly /bin/bash)
Scripts entered as Shutdown scripts are executed as the root user, it is not necessary to use
sudo in the script. Remember that any files you create will be owned by root; if you need non-root users to have file access, you should modify the permissions accordingly in the script. Also, because the script is not run interactively, you cannot include commands that require user feedback (such as
rm without the
Adding the agent installation at boot time adds to the amount of time it takes to boot the instance. Please make sure the shutdown script installation code is the last step in your user-data. You should allow a few minutes of extra time for the tasks to complete before you test that the user script has finished successfully.
Install the Spotinst agent. For ease of use, we recommend adding this to your user data script.
#!/usr/bin/env bash curl -fsSL https://s3.amazonaws.com/spotinst-public/services/agent/elastigroup-agent-init.sh | \ SPOTINST_ACCOUNT_ID='<ACCOUNT_ID>' \ SPOTINST_TOKEN='<YOUR_API_TOKEN>' \ bash
Support Center article: https://help.spotinst.com/hc/en-us/articles/115003069869-Shutdown-Script
Done! Your shutdown script will be deployed to every new instance on your elastigroup. Our agent will sample the API for updates every 10 seconds to figure out if the script should run.