Run commands during the server provisioning process.
When provisioning a server at Packet you have the option to include User Data, which can be used to perform various automation tasks after the server is ready, such as executing common scripts/packages, or triggering other more advanced configuration processes.
User data - like other forms of instance “meta data” - is tied into Packet’s extensive use of cloud-init. Designed in the early days of AWS, Cloud-init is a simple but powerful technique for carrying out actions on a cloud instance at boot time. It takes care of defining each machine’s hostname, adds SSH keys, and performs the “phone home” process when the server finishes provisioning.
User Data allows you to influence and augment this critical part of the server provisioning process.
Userdata takes two different forms:
User Data Script
- Typically used by those who just want to execute a shell script.
- Begins with: #!
Cloud Config Data
- Using valid yaml syntax, you can specify certain things in a human-friendly format.
- Begins with: #cloud-config
When provisioning a new server from the Packet Portal, you will find the option to add userdata under Manage:
On the other hand, if you want to provision a server via our API, you can add userdata by simply using the "userdata": "string" in the body of the call.
Example Usage: Deploying Nginx
User Data Script
#!/bin/bash export DEBIAN_FRONTEND=noninteractive apt-get update && apt-get upgrade -y apt-get install nginx -y
#cloud-config package_upgrade: true packages: - nginx
Once the server provisions, you should be able to hit the webserver on the Public IP.
The first thing to keep in mind when using any form of user data, is that depending on the length of your script, you need to give it some time to execute after seeing the server as “Active” in the Packet portal.
If you need to check whether the server has downloaded the user-data at all, you can check
Any form of user data will only run on the first boot, and you won’t be able to rerun it again later on the same server.
If you need to troubleshoot any issues, the log files where you can get any needed information are
/var/log/cloud-init.log and /var/log/cloud-init-output.log