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:

Copy

Example: Basic User Data Script For Ubuntu

Copy

Example: Basic User Data Script For Rocky

Copy

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:

Copy

2. Avoid Interactive Commands:

Do not use commands that require user input.

Example:

✔ Correct:

Copy

✘ Incorrect:

Copy

3. Add Logging for Debugging:

Redirect output to a log file:

Copy

4. Handle Failures Gracefully:

Use error handling where needed:

Copy

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

Copy

Install and Configure Docker

Copy

Common Issues

IssueCauseFix
Script didn’t runMissing **#!/bin/bash**Add shebang
Command failedInteractive promptUse non-interactive flags
Service not startingPackage not installedVerify install step
Permission issuesFiles owned by root / wrong permissionsUse **chown** / **chmod**

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.

VariableType to search · ESC to discard
GlossaryType to search · ESC to discard
InsertType to search · ESC to discard
No matches