Сommand-line interface

Getting started with the command-line interface

The Yandex.Cloud command-line interface (CLI) provides downloadable software for managing your cloud resources from the command line.

Installation

  1. Run at a command prompt:

    $ curl https://storage.yandexcloud.net/yandexcloud-yc/install.sh | bash
    

    The script will install the CLI and add the executable file path to the environment variable PATH.

    Note

    The script will update PATH only if you run it in bash or zsh shell.

    If you run the script in a different shell, add the path to the CLI to the variable PATH yourself.

  2. After installation is complete, restart your terminal.

  1. Run at a command prompt:

    $ curl https://storage.yandexcloud.net/yandexcloud-yc/install.sh | bash
    

    The script will install the CLI and add the executable file path to the environment variable PATH.

  2. Restart your terminal for the changes to take effect.

The CLI supports command completion for the bash shell. For command completion to work:

  1. Install the Homebrew package manager.

  2. Install the bash-completion package:

    $ brew install bash-completion
    
  3. After installation is complete, add the following lines to the ~/.bash_profile file:

    if [ -f $(brew --prefix)/etc/bash_completion ]; then
    . $(brew --prefix)/etc/bash_completion
    fi
    
  4. Restart your terminal.

  1. Construct a link to download the current version of the CLI:

    • for Windows 64-bit:

      https://storage.yandexcloud.net/yandexcloud-yc/release/<current-version>/windows/amd64/yc.exe
      
    • for Windows 32-bit:

      https://storage.yandexcloud.net/yandexcloud-yc/release/<current-version>/windows/386/yc.exe
      

    Replace <current-version> with the CLI version from the link.

  2. Download the CLI executable file and save it on your computer, for example, in the C:\Yandex-Cloud directory.

  3. Add the executable file path to the PATH environment variable:

    1. Press the keyboard shortcut Windows+R and run the command control.exe /name Microsoft.System.

    2. Click the Advanced system settings link.

    3. Click Environment Variables.

    4. Select the PATH environment variable and click Edit.

    5. In the list in the Variable value field, add the character ; and the CLI executable path, for example, C:\Yandex-Cloud.

      Click OK in the Edit System Variable, Environment Variables, and System Properties windows to close them.

Initialization

  1. Get an OAuth token from Yandex.OAuth. To do this, follow the link and click Allow.

  2. To configure your CLI profile, run the command yc init.

  3. Enter your OAuth token when prompted by the command.

    Please go to https://oauth.yandex.ru/authorize?response_type=token&client_id=1a6990aa636648e9b2ef855fa7bec2fb
     in order to obtain OAuth token.
    
    Please enter OAuth token: AaAaBbBbCcCcDdDdEeEeFfFfGgGg
    
  4. At the command prompt, select one of the clouds from the list of those you have access to:

    Please select cloud to use:
     [1] cloud1 (id = aoe2bmdcvatao4frg22b)
     [2] cloud2 (id = dcvatao4faoe2bmrg22b)
    Please enter your numeric choice: 2
    
  5. Select the default folder:

    Please choose a folder to use:
     [1] folder1 (id = cvatao4faoe2bmdrg22b)
     [2] folder2 (id = tao4faoe2cvabmdrg22b)
     [3] Create a new folder
    Please enter your numeric choice: 1
    
  6. Select the default availability zone for the Yandex Compute Cloud service:

    Do you want to configure a default Yandex Compute Cloud availability zone? [Y/n] Y
    Which zone do you want to use as a profile default?
     [1] ru-central1-a
     [2] ru-central1-b
     [3] ru-central1-c
     [4] Don't set default zone
    Please enter your numeric choice: 2
    
  7. View your CLI profile settings:

    $ yc config list
    

Command examples

The following steps describe how to create a cloud network, subnet, and virtual machine that is connected to this subnet.

  1. View the description of the CLI commands for working with cloud networks:

    $ yc vpc network --help
    
  2. Create a cloud network in the folder specified in your CLI profile:

    $ yc vpc network create \
        --name my-yc-network \
        --labels my-label=my-value \
        --description "my first network via yc"
    
  3. Create a subnet in the cloud network my-yc-network:

    $ yc vpc subnet create \
        --name my-yc-subnet-b \
        --zone ru-central1-b \
        --range 10.1.2.0/24 \
        --network-name my-yc-network \
        --description "my first subnet via yc"
    
  4. Get a list of all cloud networks in the directory specified in your CLI profile:

    $ yc vpc network list
    
    +----------------------+------------------+-------------------------+
    |          ID          |       NAME       |       DESCRIPTION       |
    +----------------------+------------------+-------------------------+
    | skesdqhkc6449hbqqar1 | my-ui-network    | my first network via ui |
    | c6449hbqqar1skesdqhk | my-yc-network    | my first network via yc |
    +----------------------+------------------+-------------------------+
    

    Get the same list with more details in YAML format:

    $ yc vpc network list --format yaml
    
    - id: skesdqhkc6449hbqqar1
      folder_id: ijkl9012
      created_at: "2018-09-05T09:51:16Z"
      name: my-ui-network
      description: "my first network via ui"
      labels: {}
    - id: c6449hbqqar1skesdqhk
      folder_id: ijkl9012
      created_at: "2018-09-05T09:55:36Z"
      name: my-yc-network
      description: "my first network via yc"
      labels:
        my-label: my-value
    
  5. Create a virtual machine and connect it to the subnet my-yc-subnet-b:

    1. Prepare the key pair (public and private) for SSH access to the VM.

    2. Create a virtual machine:

      $ yc compute instance create \
          --name my-yc-instance \ 
          --network-interface subnet-name=my-yc-subnet-b,nat-ip-version=ipv4 \
          --ssh-key ~/.ssh/id_rsa.pub
      

      Pass the path to the public key for SSH access in the ssh-key parameter. The yc-user user will be automatically created in the virtual machine OS with the specified public key.

  6. Connect to the virtual machine over SSH:

    1. Find out the public IP address of the virtual machine. To do this, view detailed information about your virtual machine:

      $ yc compute instance get my-yc-instance
      

      In the command output, find the address of the virtual machine in the one_to_one_nat section:

      one_to_one_nat:
          address: 130.193.32.90
          ip_version: IPV4
      
    2. Connect to the virtual machine over SHH on behalf of the yc-user user, using the private key:

      $ ssh yc-user@130.193.32.90
      
  7. Delete the my-yc-instance virtual machine, the my-yc-subnet-b subnet and the my-yc-network network:

    $ yc compute instance delete my-yc-instance
    $ yc vpc subnet delete my-yc-subnet-b
    $ yc vpc network delete my-yc-network