Using Yandex Cloud 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:
- If you do not have a service account, create one.
- Assign to the service account a role consistent with the actions you want to perform from within the VM. For example, for Compute Cloud resource management, this could be the
compute.adminrole for a folder or the primitiveeditorrole. - Link the service account to your VM.
- Get authenticated from within your VM.
Link 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 VM
- In the management console
, select the folder the VM belongs to. - In the list of services, select Compute Cloud.
- Click the VM name.
- In the top-right corner of the page, click
Edit VM. - Under Additional, select an existing service account or create a new one.
- 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
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.
-
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.
-
In the section with the
yandex_compute_instanceresource description, add theservice_account_idparameter and specify the service account ID:resource "yandex_compute_instance" "vm-1" { ... service_account_id = "<service_account_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.
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 VM
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
-
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.
-
In the section with the
yandex_compute_instanceresource description, add theservice_account_idparameter and specify the service account ID:resource "yandex_compute_instance" "vm-1" { ... service_account_id = "<service_account_ID>" ... } -
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
. -
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 VM
Warning
Simplified authentication to the Yandex Cloud API or CLI from within a VM is only possible using a service account associated with the VM.
-
Connect to the VM over SSH.
-
If you do not have the Yandex Cloud CLI installed yet, install and initialize it.
-
Create a new profile:
yc config profile create my-robot-profile -
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.
-
Specify the cloud in your profile:
yc config set cloud-id <cloud_ID>You can also use the
--cloud-idparameter to run commands. -
Specify a folder in the profile:
yc config set folder-id <folder_ID>You can also use the
--folder-idparameter 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-tokenThe 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.
-
-
Connect to the VM over SSH.
-
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/tokenResult:
{"access_token":"CggVAgAAA...","expires_in":39944,"token_type":"Bearer"}The response will return an IAM token in the
access_tokenfield. The remaining lifetime of the IAM token is specified in theexpires_infield. -
Specify the received IAM token when accessing Yandex Cloud resources via the API. Provide the IAM token in the
Authorizationheader 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.