Creating a VM with metadata from environment variables
With the Yandex Cloud CLI, you can create a VM whose metadata will contain values set in environment variablesuser-data
key.
This use case demonstrates creating a VM on Ubuntu 22.04 LTS with a preinstalled NginxUSER_NAME
and SSH_KEY
variables of the environment the command is executed in.
To create a VM with metadata from environment variables:
If you do not have the Yandex Cloud command line interface yet, install and initialize it.
The folder specified in the CLI profile is used by default. You can specify a different folder using the --folder-name
or --folder-id
parameter.
-
Specify the environment variables containing the VM local user's name and SSH key; these will be substituted into the VM metadata when the Yandex Cloud CLI command is executed later on:
export USER_NAME="<username>" export SSH_KEY="<SSH_key>"
-
Create a file named
metadata.yaml
and paste into it the following metadata configuration for the new VM:metadata.yaml
#cloud-config datasource: Ec2: strict_id: false ssh_pwauth: no users: - name: $USER_NAME sudo: 'ALL=(ALL) NOPASSWD:ALL' shell: /bin/bash ssh_authorized_keys: - $SSH_KEY write_files: - path: "/usr/local/etc/startup.sh" permissions: "755" content: | #!/bin/bash apt-get update apt-get install -y nginx service nginx start sed -i -- "s/ nginx/ Yandex Cloud - $$HOSTNAME/" /var/www/html/index.nginx-debian.html defer: true runcmd: - ["/usr/local/etc/startup.sh"]
-
Run this command:
yc compute instance create \ --name <VM_name> \ --hostname <host_name> \ --zone <availability_zone> \ --network-interface subnet-name=<subnet_name>,nat-ip-version=ipv4,security-group-ids=<security_group_ID> \ --create-boot-disk image-folder-id=standard-images,image-family=ubuntu-2204-lts \ --metadata-from-file user-data="<path_to_configuration_file>"
Where:
-
--name
: Name of the new VM. -
--hostname
: Host name for the new VM. This is an optional parameter. If omitted, the VM ID will be used as the host name. -
--zone
: Availability zone the new VM will reside in. -
--network-interface
: Network interface settings for the new VM:subnet-name
: Name of the subnet in the availability zone specified in the--zone
parameter.security-group-ids
: Security group ID.
-
--metadata-from-file
: Theuser-data
key with the path to thecloud-config
YAML configuration file for value. e.g.,--metadata-from-file user-data="/home/user/metadata.yaml"
.Note
Note that the CLI command for the
HOSTNAME
variable will not substitute its value into the metadata. Instead, the$HOSTNAME
variable name will be provided to thecloud-init
configuration when executing the CLI command; the hostname value of the new VM will be substituted in place of that variable later what creating the VM.This is why the
HOSTNAME
variable is specified using the two-dollar syntax in theuser-data
key:$$HOSTNAME
. For more information, see Processing environment variables in metadata via the CLI.
Result:
done (36s) id: epd8m0fqvkuu******** folder_id: b1gt6g8ht345******** created_at: "2025-01-01T14:24:37Z" name: my-sample-vm zone_id: ru-central1-b platform_id: standard-v2 resources: memory: "2147483648" cores: "2" core_fraction: "100" status: RUNNING metadata_options: gce_http_endpoint: ENABLED aws_v1_http_endpoint: ENABLED gce_http_token: ENABLED aws_v1_http_token: DISABLED boot_disk: mode: READ_WRITE device_name: epd60hoo48qj******** auto_delete: true disk_id: epd60hoo48qj******** network_interfaces: - index: "0" mac_address: d0:0d:8b:01:fa:fd subnet_id: e2lqsms4cdl3******** primary_v4_address: address: 192.168.15.14 one_to_one_nat: address: 51.250.**.** ip_version: IPV4 security_group_ids: - enpbtvidu0g0******** serial_port_settings: ssh_authorization: OS_LOGIN gpu_settings: {} fqdn: my-web-server.ru-central1.internal scheduling_policy: {} network_settings: type: STANDARD placement_policy: {} hardware_generation: legacy_features: pci_topology: PCI_TOPOLOGY_V1
For other configuration examples for
user-data
, see Examples.For more information about the
yc compute instance create
command, see the CLI reference. -