Yandex Cloud
Search
Contact UsGet started
  • Pricing
  • Customer Stories
  • Documentation
  • Blog
  • All Services
  • System Status
    • Featured
    • Infrastructure & Network
    • Data Platform
    • Containers
    • Developer tools
    • Serverless
    • Security
    • Monitoring & Resources
    • AI Studio
    • Business tools
  • All Solutions
    • By industry
    • By use case
    • Economics and Pricing
    • Security
    • Technical Support
    • Start testing with double trial credits
    • Cloud credits to scale your IT product
    • Gateway to Russia
    • Cloud for Startups
    • Center for Technologies and Society
    • Yandex Cloud Partner program
  • Pricing
  • Customer Stories
  • Documentation
  • Blog
© 2025 Direct Cursus Technology L.L.C.
Service page
Yandex Compute Cloud
Documentation
Yandex Compute Cloud
    • All guides
      • Connecting to a VM over SSH
      • Connecting to a VM over SSH using Cloud Shell
      • Exchanging files with VMs
      • Connecting to a VM via RDP
      • Connecting to a VM via PowerShell
      • Using Yandex Cloud from within a VM
      • Installing NVIDIA drivers
      • Recovering access to a VM
    • Viewing service resource operations
  • Yandex Container Solution
  • Access management
  • Pricing policy
  • Terraform reference
  • Monitoring metrics
  • Audit Trails events
  • Release notes

In this article:

  • Link your service account
  • Linking to an existing VM
  • Linking to a new VM
  • Authentication from within a VM
  1. Step-by-step guides
  2. Using a VM
  3. Using Yandex Cloud from within a VM

Using Yandex Cloud from within a VM

Written by
Yandex Cloud
Improved by
amatol
Updated at July 29, 2025
  • Link your service account
    • Linking to an existing VM
    • Linking to a new VM
  • Authentication from within a VM

This section describes how to use Yandex Cloud from within a VM via the API or CLI.

To automate working with Yandex Cloud from within a VM, we recommend using service accounts. This is more secure since you do not need to keep your OAuth token on the VM and can restrict access permissions for your service account.

Yandex Cloud provides simplified API and CLI authentication from within a VM for service accounts. To authenticate:

  1. If you do not have a service account yet, create one and configure access permissions for it.
  2. Link the service account to your VM.
  3. Get authenticated from within your VM.

Link your service accountLink your service account

Link your service account to an existing or new VM.

Note

You can only link one service account to a virtual machine.

To link a service account to a VM, you need a permission to use this account. This permission comes with the iam.serviceAccounts.user and editor roles or higher.

Linking to an existing VMLinking to an existing VM

Management console
CLI
Terraform
API
  1. In the management console, select the folder the VM belongs to.
  2. In the list of services, select Compute Cloud.
  3. Click the VM name.
  4. In the top-right corner of the page, click Edit VM.
  5. Under Additional, select an existing service account or create a new one.
  6. Click Save changes.

If you do not have the Yandex Cloud CLI installed yet, install and initialize it.

By default, the CLI uses the folder specified when creating the profile. To change the default folder, use the yc config set folder-id <folder_ID> command. You can also set a different folder for any specific command using the --folder-name or --folder-id parameter.

Update the VM parameters by specifying the service account using --service-account-name or --service-account-id:

yc compute instance update my-instance --service-account-name test

With Terraform, you can quickly create a cloud infrastructure in Yandex Cloud and manage it using configuration files. These files store the infrastructure description written in HashiCorp Configuration Language (HCL). If you change the configuration files, Terraform automatically detects which part of your configuration is already deployed, and what should be added or removed.

Terraform is distributed under the Business Source License. The Yandex Cloud provider for Terraform is distributed under the MPL-2.0 license.

For more information about the provider resources, see the relevant documentation on the Terraform website or its mirror.

If you do not have Terraform yet, install it and configure the Yandex Cloud provider.

  1. Open the Terraform configuration file with the description of the VM you want to link the service account to. See an example of the VM configuration file.

  2. In the section with the yandex_compute_instance resource description, add the service_account_id parameter and specify the service account ID:

    resource "yandex_compute_instance" "vm-1" {
      ...
      service_account_id = "<service_account_ID>"
      ...
    }
    
  3. Apply the changes:

    1. In the terminal, go to the directory where you edited the configuration file.

    2. Make sure the configuration file is correct using this command:

      terraform validate
      

      If the configuration is correct, you will get this message:

      Success! The configuration is valid.
      
    3. Run this command:

      terraform plan
      

      You will see a detailed list of resources. No changes will be made at this step. If the configuration contains any errors, Terraform will show them.

    4. Apply the changes:

      terraform apply
      
    5. Type yes and press Enter to confirm the changes.

    Terraform will change all required resources. You can check the new resources using the management console.

For more information about yandex_compute_instance properties, see this Terraform article.

Use the update REST API method for the Instance resource or the InstanceService/Update gRPC API call. Specify the service account ID in your request.

Linking to a new VMLinking to a new VM

Management console
CLI
Terraform
API

In the management console, you can link a service account to a virtual machine. This service account must be in the same folder as the VM. If the service account is in a different folder, use the CLI or API.

To link a service account to a VM, select it under Additional in the Service account field when creating the VM. You can select an existing service account or create a new one.

If you do not have the Yandex Cloud CLI installed yet, install and initialize it.

By default, the CLI uses the folder specified when creating the profile. To change the default folder, use the yc config set folder-id <folder_ID> command. You can also set a different folder for any specific command using the --folder-name or --folder-id parameter.

Create a VM by specifying the service account using --service-account-name or --service-account-id:

yc compute instance create \
  --name my-instance \
  --network-interface subnet-name=default,nat-ip-version=ipv4 \
  --ssh-key ~/.ssh/id_ed25519.pub \
  --service-account-name my-robot
  1. Open the Terraform configuration file with the description of the VM you want to link the service account to. See an example of the VM configuration file.

  2. In the section with the yandex_compute_instance resource description, add the service_account_id parameter and specify the service account ID:

    resource "yandex_compute_instance" "vm-1" {
      ...
      service_account_id = "<service_account_ID>"
      ...
    }
    
  3. Create the resources:

    1. In the terminal, go to the directory where you edited the configuration file.

    2. Make sure the configuration file is correct using this command:

      terraform validate
      

      If the configuration is correct, you will get this message:

      Success! The configuration is valid.
      
    3. Run this command:

      terraform plan
      

      You will see a detailed list of resources. No changes will be made at this step. If the configuration contains any errors, Terraform will show them.

    4. Apply the changes:

      terraform apply
      
    5. Type yes and press Enter to confirm the changes.

    Terraform will create all the required resources. You can check the new resources using the management console.

For more information about yandex_compute_instance properties, see this Terraform article.

Use the create REST API method for the Instance resource or the InstanceService/Create gRPC API call. Specify the service account ID in your request.

Authentication from within a VMAuthentication from within a VM

To get authenticated from within your VM using the linked service account:

CLI
API
  1. Connect to the VM over SSH.

  2. If you do not have the Yandex Cloud CLI installed yet, install and initialize it.

  3. Create a new profile:

    yc config profile create my-robot-profile
    
  4. Configure your profile to run commands.

    Some commands require that you specify unique IDs for your cloud and folder. You can specify their details in the profile or use a specific flag for these commands.

    1. Specify the cloud in your profile:

      yc config set cloud-id <cloud_ID>
      

      You can also use the --cloud-id parameter to run commands.

    2. Specify a folder in the profile:

      yc config set folder-id <folder_ID>
      

      You can also use the --folder-id parameter to run commands.

    All operations in this profile will be performed on behalf of the linked service account. You can change the profile parameters or switch to another profile.

    You can also get a IAM token, e.g., to get authenticated with the API:

    yc iam create-token
    

    The lifetime of an IAM token in this case will be less than 12 hours. Request an IAM token more often, e.g., every hour. To learn the remaining token lifetime, follow the steps for the API.

  1. Connect to the VM over SSH.

  2. Get an IAM token from metadata in Google Compute Engine format:

    curl \
      --header Metadata-Flavor:Google http://169.254.169.254/computeMetadata/v1/instance/service-accounts/default/token
    

    Result:

    {"access_token":"CggVAgAAA...","expires_in":39944,"token_type":"Bearer"}
    

    The response will return an IAM token in the access_token field. The remaining lifetime of the IAM token is specified in the expires_in field.

  3. Specify the received IAM token when accessing Yandex Cloud resources via the API. Provide the IAM token in the Authorization header in the following format:

    Authorization: Bearer <IAM_token>
    

Keep track of the IAM token lifetime or request a new token more often, e.g., every hour.

Was the article helpful?

Previous
Connecting to a VM via OS Login
Next
Installing NVIDIA drivers
© 2025 Direct Cursus Technology L.L.C.