Launching a script when an instance is created
Objective
In some situations, you will need to launch a script when you create an instance. For example, you may want to do this if you need to configure multiple SSH keys for your instance, or configure your SSH service automatically.
This guide explains how to launch a script when an instance is created, via Cloud-init and OpenStack APIs.
Requirements
Instructions
Create a script
There are several different scripts that are useful for you to launch when you create an instance. For example, you can use shell scripts:
- Add a new user:
With this script, you can create a user named "ovh". Then we give this user sudo access, and add their SSH key.
- Modify SSH configuration:
With this script, you can modify the default SSH port (22 -> 2211) and block the connection using the root user privileges.
- Update packets and set up a web server:
This script can increase the time it takes to create the instance.
It is also possible to run cloud-config scripts when you create your instance, e.g.:
- Create a user with 2 SSH keys:
With this script, you create an "ovh" user with sudo permissions, and they can connect via 2 different SSH keys.
The user "admin" will not be created, but will be replaced by your user.
Create the instance
After you have retrieved the list of images and instance templates, you can launch the script with Cloud-init via the --user- data argument:
After we have double-checked the details, our user is correctly added after the instance has been created, and they have all of the permissions required:
Go further
Join our community of users.