Yandex Cloud
Search
Contact UsGet started
  • Blog
  • Pricing
  • Documentation
  • All Services
  • System Status
    • Featured
    • Infrastructure & Network
    • Data Platform
    • Containers
    • Developer tools
    • Serverless
    • Security
    • Monitoring & Resources
    • ML & AI
    • Business tools
  • All Solutions
    • By industry
    • By use case
    • Economics and Pricing
    • Security
    • Technical Support
    • Customer Stories
    • Cloud credits to scale your IT product
    • Gateway to Russia
    • Cloud for Startups
    • Education and Science
    • Yandex Cloud Partner program
  • Blog
  • Pricing
  • Documentation
© 2025 Direct Cursus Technology L.L.C.
Yandex Compute Cloud
    • All guides
      • Stopping and starting a VM
      • Resetting a Windows Server VM user password
      • Attaching a disk to a VM
      • Detaching a disk from a VM
      • Moving a VM to a different availability zone
      • Moving a VM to a different folder
      • Moving a VM to a different cloud
      • Adding another network interface to a VM
      • Deleting a network interface from a VM
      • Assigning a public IP address to a VM
      • Unassigning a public IP address from a VM
      • Making a VM public IP address static
      • Reassigning a public IP address from one VM to another
      • Updating the VM internal IP address
      • Updating a VM
      • Changing VM computing resources
      • Changing VM security groups
      • VM maintenance policy management
      • Configuring VM access permissions
      • Linking a service account to a VM
      • Deleting a VM
    • Viewing operations with resources
  • Yandex Container Solution
  • Access management
  • Terraform reference
  • Monitoring metrics
  • Audit Trails events
  • Release notes
  1. Step-by-step guides
  2. Managing a VM
  3. Linking a service account to a VM

Linking a service account to a VM

Written by
Yandex Cloud
Updated at May 5, 2025

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.

If you do not have a service account yet, create one and configure access permissions for it.

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. From 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 yet, install and initialize it.

The folder specified when creating the CLI profile is used by default. To change the default folder, use the yc config set folder-id <folder_ID> command. You can specify a different folder 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 documentation on the Terraform website or mirror website.

If you do not have Terraform yet, install it and configure its 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, change to the folder where you edited the configuration file.

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

      terraform validate
      

      If the configuration is correct, the following message is returned:

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

      terraform plan
      

      The terminal will display a list of resources with parameters. No changes are made at this step. If the configuration contains errors, Terraform will point them out.

    4. Apply the configuration changes:

      terraform apply
      
    5. Confirm the changes: type yes in the terminal and press Enter.

    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 yet, install and initialize it.

The folder specified when creating the CLI profile is used by default. To change the default folder, use the yc config set folder-id <folder_ID> command. You can specify a different folder 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 resources:

    1. In the terminal, change to the folder where you edited the configuration file.

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

      terraform validate
      

      If the configuration is correct, the following message is returned:

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

      terraform plan
      

      The terminal will display a list of resources with parameters. No changes are made at this step. If the configuration contains errors, Terraform will point them out.

    4. Apply the configuration changes:

      terraform apply
      
    5. Confirm the changes: type yes in the terminal and press Enter.

    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.

Was the article helpful?

Previous
Configuring VM access permissions
Next
Deleting a VM
© 2025 Direct Cursus Technology L.L.C.