Configuring VM access permissions
To grant a user, group, or service account access to a VM, assign a role for it.
Assigning a role
- In the management console
, select the folder this VM belongs to. - Select Compute Cloud.
- Select the VM.
- Navigate to the
Access bindings tab. - Click Assign roles.
- In the window that opens, select the group, user, or service account you want to grant access to the VM.
- Click
Add role and select the required roles. - Click Save.
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.
-
See the description of the CLI command for assigning a role for a VM:
yc compute instance add-access-binding --help -
Get a list of VMs in the default folder:
yc compute instance listResult:
+----------------------+-----------------+---------------+---------+----------------------+ | ID | NAME | ZONE ID | STATUS | DESCRIPTION | +----------------------+-----------------+---------------+---------+----------------------+ | fhm0b28lgfp4******** | first-instance | ru-central1-a | RUNNING | my first vm via CLI | | fhm9gk85nj7g******** | second-instance | ru-central1-a | RUNNING | my second vm via CLI | +----------------------+-----------------+---------------+---------+----------------------+ -
View a list of roles already assigned for the resource in question:
yc compute instance list-access-bindings <VM_ID> -
Assign the role using this command:
-
To a user:
yc compute instance add-access-binding <VM_ID> \ --user-account-id <user_ID> \ --role <role>Where:
-
To a service account:
yc compute instance add-access-binding <VM_ID> \ --service-account-id <service_account_ID> \ --role <role>Where:
--service-account-id: Service account ID.--role: Role to assign.
-
With Terraform
Terraform is distributed under the Business Source License
For more information about the provider resources, see the relevant documentation on the Terraform
If you do not have Terraform yet, install it and configure the Yandex Cloud provider.
To assign a role for a VM using Terraform:
-
In the Terraform configuration file, define the parameters of the resources you want to create:
resource "yandex_compute_instance_iam_binding" "sa-access" { instance_id = "<VM_ID>" role = "<role>" members = ["<subject_type>:<subject_ID>","<subject_type>:<subject_ID>"] }Where:
instance_id: VM ID.role: Role to assign.members: List of types and IDs of subjects getting the role. Specify it asuserAccount:<user_ID>orserviceAccount:<service_account_ID>.
For more information about the
yandex_compute_instance_iam_bindingresource parameters, see the relevant provider documentation. -
Create the resources:
-
In the terminal, go to the directory where you edited the configuration file.
-
Make sure the configuration file is correct using this command:
terraform validateIf the configuration is correct, you will get this message:
Success! The configuration is valid. -
Run this command:
terraform planYou 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.
-
Apply the changes:
terraform apply -
Type
yesand press Enter to confirm the changes.
Terraform will create all the required resources. You can check the new resources using the management console
or this CLI command:yc compute instance list-access-bindings <VM_ID> -
Use the updateAccessBindings REST API method for the Instance resource or the InstanceService/UpdateAccessBindings gRPC API call. In the request body, set the action property to ADD and specify the user type and ID under subject.
Assigning multiple roles
- In the management console
, select the folder this VM belongs to. - Select Compute Cloud.
- Select the VM.
- Navigate to the
Access bindings tab. - Click Assign roles.
- In the window that opens, select the group, user, or service account you want to grant access to the VM.
- Click
Add role and select the required roles. - To add another role, click Add role.
- Click Save.
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.
You can assign multiple roles using the set-access-bindings command.
Alert
The set-access-bindings command completely rewrites access permissions for the resource. All current roles for the resource will be deleted.
-
Make sure the resource has no roles assigned that you would not want to lose:
yc compute instance list-access-bindings <VM_ID> -
See the CLI command description for assigning roles for a VM:
yc compute instance set-access-bindings --help -
Assign roles:
yc compute instance set-access-bindings <VM_ID> \ --access-binding role=<role>,subject=<subject_type>:<subject_ID>Where:
-
--access-binding: Role to assign:role: ID of the role to assign.subject: Type and ID of the subject the role is assigned to.
For example, this command will assign roles to multiple users and a single service account:
yc compute instance set-access-bindings test-vm \ --access-binding role=editor,subject=userAccount:gfei8n54hmfh******** --access-binding role=viewer,subject=userAccount:helj89sfj80a******** --access-binding role=editor,subject=serviceAccount:ajel6l0jcb9s******** -
If you do not have Terraform yet, install it and configure the Yandex Cloud provider.
To assign multiple roles for a VM using Terraform:
-
In the Terraform configuration file, describe the resources you want to create:
resource "yandex_compute_instance_iam_binding" "role-1" { instance_id = "<VM_ID>" role = "<role_1>" members = ["<subject_type>:<subject_ID>"] } resource "yandex_compute_instance_iam_binding" "role-2" { instance_id = "<VM_ID>" role = "<role_2>" members = ["<subject_type>:<subject_ID>"] }Where:
instance_id: VM ID.role: Role to assign.members: List of types and IDs of subjects getting the role. Specify it asuserAccount:<user_ID>orserviceAccount:<service_account_ID>.
For more information about the
yandex_compute_instance_iam_bindingresource parameters, see the relevant provider documentation. -
Create the resources:
-
In the terminal, go to the directory where you edited the configuration file.
-
Make sure the configuration file is correct using this command:
terraform validateIf the configuration is correct, you will get this message:
Success! The configuration is valid. -
Run this command:
terraform planYou 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.
-
Apply the changes:
terraform apply -
Type
yesand press Enter to confirm the changes.
You can check the updates using the management console
or this CLI command:yc compute instance list-access-bindings <VM_ID> -
Use the setAccessBindings REST API method for the Instance resource or the InstanceService/SetAccessBindings gRPC API call.
Revoking a role
- In the management console
, select the folder this VM belongs to. - Select Compute Cloud.
- Select the VM.
- Navigate to the
Access bindings tab. - In the line with the user in question, click
and select Edit roles. - Click
next to a role to delete it. - Click Save.
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.
-
See the description of the CLI command for revoking a role for a VM:
yc compute instance remove-access-binding --help -
View the roles and assignees for the resource:
yc compute instance list-access-bindings <VM_ID> -
To revoke access permissions, run this command:
yc compute instance remove-access-binding <VM_ID> \ --role <role_ID> \ --subject <subject_type>:<subject_ID>Where:
--role: ID of the role you want to revoke.--subject: Subject to revoke the role from.
For example, this command revokes the
viewerrole for the VM from a user with theajel6l0jcb9s********ID:yc compute instance remove-access-binding test-vm \ --role viewer \ --subject userAccount:ajel6l0jcb9s********
If you do not have Terraform yet, install it and configure the Yandex Cloud provider.
To revoke a role assigned for a VM using Terraform:
-
Open the Terraform configuration file and delete the section specifying the role:
resource "yandex_compute_instance_iam_binding" "sa-access" { instance_id = "<VM_ID>" role = "<role>" members = ["<subject_type>:<subject_ID>"] } -
Apply the changes:
-
In the terminal, go to the directory where you edited the configuration file.
-
Make sure the configuration file is correct using this command:
terraform validateIf the configuration is correct, you will get this message:
Success! The configuration is valid. -
Run this command:
terraform planYou 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.
-
Apply the changes:
terraform apply -
Type
yesand press Enter to confirm the changes.
You can check the updates using the management console
or this CLI command:yc compute instance list-access-bindings <VM_ID> -
To revoke roles for a VM, use the updateAccessBindings REST API method for the Instance resource or the InstanceService/UpdateAccessBindings gRPC API call. In the request body, set the action property to REMOVE and specify the user type and ID under subject.