| layout | cloudstack |
|---|---|
| page_title | CloudStack: cloudstack_user_data |
| sidebar_current | docs-cloudstack-resource-user-data |
| description | Registers and manages user data in CloudStack for VM initialization. |
Registers user data in CloudStack that can be used to initialize virtual machines during deployment. User data typically contains scripts, configuration files, or other initialization data that should be executed when a VM starts.
resource "cloudstack_user_data" "web_init" {
name = "web-server-init"
userdata = base64encode(<<-EOF
#!/bin/bash
apt-get update
apt-get install -y nginx
systemctl enable nginx
systemctl start nginx
EOF
)
}resource "cloudstack_user_data" "app_init" {
name = "app-server-init"
userdata = base64encode(<<-EOF
#!/bin/bash
apt-get update
apt-get install -y nginx
# Use parameters passed from instance deployment
echo "<h1>Welcome to $${app_name}!</h1>" > /var/www/html/index.html
echo "<p>Environment: $${environment}</p>" >> /var/www/html/index.html
echo "<p>Debug Mode: $${debug_mode}</p>" >> /var/www/html/index.html
systemctl enable nginx
systemctl start nginx
EOF
)
# Define parameters that can be passed during instance deployment
params = ["app_name", "environment", "debug_mode"]
}resource "cloudstack_user_data" "project_init" {
name = "project-specific-init"
project_id = "12345678-1234-1234-1234-123456789012"
userdata = base64encode(<<-EOF
#!/bin/bash
# Project-specific initialization
echo "Initializing project environment..."
EOF
)
}The following arguments are supported:
name- (Required) The name of the user data. Must be unique within the account/project scope.userdata- (Required) The user data content to be registered. Should be base64 encoded. This is typically a cloud-init script or other initialization data.
account- (Optional) The account name for the user data. Must be used together withdomain_id. If not specified, uses the current account.domain_id- (Optional) The domain ID for the user data. Required whenaccountis specified.project_id- (Optional) The project ID to create this user data for. Cannot be used together withaccount/domain_id.params- (Optional) A list of parameter names that are declared in the user data content. These parameters can be passed values during instance deployment usinguserdata_details.
The following attributes are exported:
id- The user data ID.name- The name of the user data.userdata- The registered user data content.account- The account name owning the user data.domain_id- The domain ID where the user data belongs.project_id- The project ID if the user data is project-scoped.params- The list of parameter names defined in the user data.
User data can be used in multiple ways:
resource "cloudstack_template" "web_template" {
name = "web-server-template"
# ... other template arguments ...
userdata_link {
userdata_id = cloudstack_user_data.app_init.id
userdata_policy = "ALLOWOVERRIDE" # Allow instance to override
}
}resource "cloudstack_instance" "web_server" {
name = "web-server-01"
# ... other instance arguments ...
userdata_id = cloudstack_user_data.app_init.id # Pass parameter values to the userdata script
userdata_details = {
"app_name" = "My Web Application"
"environment" = "production"
"debug_mode" = "false"
}
}User data can be imported using the user data ID:
terraform import cloudstack_user_data.example 12345678-1234-1234-1234-123456789012- User data content should be base64 encoded before registration
- Parameter substitution in user data uses the format
${parameter_name} - Parameters must be declared in the
paramslist to be usable - User data is immutable after creation - changes require resource recreation
- Maximum user data size depends on CloudStack configuration (typically 32KB)