What are Shutdown Scripts?
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.
How It Works
To enable the Shutdown script, an agent must be installed on the instance during startup. This is done via the user-data entry and will have the agent up and running on the instance within a few seconds. 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. Once we determine we are going to remove or replace the instance we will notify the agent so the script can execute. This means that the script will have around 10 minutes to execute prior to the instance termination.
Things to Note:
- The script will install pip (Python 2.7 Package Manager) automatically, in order for Spotinst agent to be installed and 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 doesn’t run interactively, you cannot include commands that require user feedback (such as rm without the -f flag).
- 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.
It is recommended to adjust the draining time out for instances to 120 seconds in the General Tab of the Creation Wizard under Advanced settings:
Adding a Shutdown Script
Navigate to the Compute Tab and open the Advanced settings:
Install the Spotinst agent. For ease of use, we recommend adding this at the end of 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="" \ SPOTINST_TOKEN="" \ bash
#!/usr/bin/env bash wget -q -O- https://s3.amazonaws.com/spotinst-public/services/agent/elastigroup-agent-init.sh | \ SPOTINST_ACCOUNT_ID="" \ SPOTINST_TOKEN="" \ bash
<powershell> New-Item -ItemType directory -Path "C:\Program Files\Spotinst" Invoke-WebRequest https://s3.amazonaws.com/spotinst-public/services/spotinst-windows-agent/SpotinstWindowsAgent-Latest.zip -OutFile "C:\Program Files\Spotinst\SpotinstAgent.zip" Add-Type -AssemblyName System.IO.Compression.FileSystem [System.IO.Compression.ZipFile]::ExtractToDirectory("C:\Program Files\Spotinst\SpotinstAgent.zip","c:\program files\spotinst\") New-Service SpotinstAgent """C:\Program Files\Spotinst\Agent.exe"" <ACCOUNT_ID> <YOUR_API_TOKEN>" -DisplayName "Spotinst Agent Service" -StartupType auto Start-Service SpotinstAgent</powershell>
- Please get a Token from https://console.spotinst.com/#/settings/tokens/permanent
- Please get your Account Id from the Account settings in your console https://console.spotinst.com/#/settings/account/general
Add your shell script to the shutdown script box, windows scripts should start with the
#enter shutdown script here #!/usr/bin/env bash "Goodbye old instance"
<powershell> Write-Host "Goodbye old instance"
Spotinst also supports a Signal to let the Elastigroup know that the instance is ready for shutdown. This Signal can be used at the end of the Shutdown script.
For more information on the
INSTANCE_READY_TO_SHUTDOWN signal, please see: Using Signals in Elastigroups.