Title
Create new category
Edit page index title
Edit category
Edit link
User Data (Startup Script) Documentation For VM on Spot
Overview
User Data allows you to pass a startup script when creating a virtual machine (VM). This script runs automatically during the first boot of the instance and is typically used to:
Install packages and dependencies
Configure applications and services
Set up environments (e.g., web servers, agents)
Perform initial system provisioning
This helps automate instance setup and reduces manual configuration.
How It Works
User Data is provided at the time of VM creation.
The script runs once during the initial boot.
It executes with root privileges.
The script is non-interactive, so it must not require user input.
Supported Format
Only shell scripts are supported.
Your script must start with a shebang:
Example: Basic User Data Script For Ubuntu
Example: Basic User Data Script For Rocky
Execution Behaviour
Runs automatically when the VM boots for the first time
Runs as root user at boot time
Does not run again on reboot
Best Practices:
1. Handle non-critical failures safely:
Ensure your script can run safely without breaking if partially executed. Use || true only when failure is acceptable.
Example:
2. Avoid Interactive Commands:
Do not use commands that require user input.
Example:
✔ Correct:
✘ Incorrect:
3. Add Logging for Debugging:
Redirect output to a log file:
4. Handle Failures Gracefully:
Use error handling where needed:
5. Keep Script Lightweight:
Large scripts increase boot time. For complex setups:
Download scripts from a remote source
Or use configuration management tools
Limitations
Script size may be limited (platform-dependent)
Runs only once during initial boot
No interactive input supported
Execution time impacts instance startup time
Advanced Usage Download and Execute External Script
Install and Configure Docker
Common Issues
Issue | Cause | Fix |
|---|---|---|
Script didn’t run | Missing | Add shebang |
Command failed | Interactive prompt | Use non-interactive flags |
Service not starting | Package not installed | Verify install step |
Permission issues | Files owned by root / wrong permissions | Use |
Security Considerations
Do not hardcode secrets (API keys, passwords) in user data
Scripts are visible via instance metadata depending on platform configuration
Summary
User Data is a simple and powerful way to automate VM initialization. By using shell scripts, you can ensure consistent, repeatable, and fast provisioning of infrastructure.
© 2023 RACKSPACE TECHNOLOGY