Interactive debugging of Cloud Functions functions
In this tutorial, you will set up a system to interactively debug Yandex Cloud Functions functions by redirecting requests to a local server. For more information about this solution, see the yc-serverless-live-debug
To set up the interactive function debugging system:
- Prepare your cloud.
- Install the required utilities.
- Create a service account with the admin privileges for the cloud.
- Deploy your resources.
- Run the debugging service.
If you no longer need the resources you created, delete them.
Prepare your cloud
Sign up for Yandex Cloud and create a billing account:
- Go to the management console
and log in to Yandex Cloud or create an account if you do not have one yet. - On the Yandex Cloud Billing
page, make sure you have a billing account linked and it has theACTIVE
orTRIAL_ACTIVE
status. If you do not have a billing account, create one.
If you have an active billing account, you can go to the cloud page
Learn more about clouds and folders.
Required paid resources
The infrastructure support costs include:
- Fee for function calls and computing resources allocated to execute the functions (see Yandex Cloud Functions pricing).
- Fee for the number of requests to the API gateway (see Yandex API Gateway pricing).
- Fee for YDB operations and data storage (see Yandex Managed Service for YDB pricing).
- Fee for logging operations and log storage (see Yandex Cloud Logging pricing).
Install the required utilities
-
Create a folder named
live-debug-test
and open it:mkdir live-debug-test cd live-debug-test
-
Install the
yc-serverless-live-debug
package:npm i -D @yandex-cloud/serverless-live-debug
Create a service account with the admin privileges for the cloud
-
Create a service account:
Management consoleCLITerraformAPI-
In the management console
, select the folder where you want to create a service account. -
In the list of services, select Identity and Access Management.
-
Click Create service account.
-
Enter a name for the service account, e.g.,
sa-live-debug
.The name format requirements are as follows:
- The name must be from 3 to 63 characters long.
- It may contain lowercase Latin letters, numbers, and hyphens.
- The first character must be a letter and the last character cannot be a hyphen.
-
Click Create.
If you do not have the Yandex Cloud command line interface yet, install and initialize it.
The folder specified in the CLI profile is used by default. You can specify a different folder using the
--folder-name
or--folder-id
parameter.To create a service account, run the following command:
yc iam service-account create --name sa-live-debug
Where
name
is the service account name in the following format:- The name must be from 3 to 63 characters long.
- It may contain lowercase Latin letters, numbers, and hyphens.
- The first character must be a letter and the last character cannot be a hyphen.
Result:
id: ajehr0to1g8b******** folder_id: b1gv87ssvu49******** created_at: "2023-03-04T09:03:11.665153755Z" name: sa-live-debug
If you don't have Terraform, install it and configure the Yandex Cloud provider.
-
In the configuration file, describe the parameters of the resources you want to create:
resource "yandex_iam_service_account" "sa" { name = "sa-live-debug" description = "<service_account_description>" folder_id = "<folder_ID>" }
Where:
name
: Service account name. This is a required parameter.description
: Service account description. This is an optional parameter.folder_id
: Folder ID. This is an optional parameter. By default, the value specified in the provider settings is used.
For more information about the
yandex_iam_service_account
resource parameters in Terraform, see the relevant provider documentation . -
Make sure the configuration files are correct.
-
In the command line, go to the folder where you created the configuration file.
-
Run a check using this command:
terraform plan
If the configuration is specified correctly, the terminal will display information about the service account. If there are errors in the configuration, Terraform will point them out.
-
-
Deploy cloud resources.
-
If the configuration does not contain any errors, run this command:
terraform apply
-
To create a service account, use the create REST API method for the ServiceAccount resource or the ServiceAccountService/Create gRPC API call.
-
-
Assign the service account the
admin
role for the cloud:Management consoleCLITerraformAPI- On the management console home page
, select the cloud. - Go to the Access bindings tab.
- Find the
sa-live-debug
account in the list and click . - Click Edit roles.
- Click Add role in the window that opens and select the
admin
role. - Click Save.
Run this command:
yc resource-manager cloud add-access-binding <cloud_ID> \ --role admin \ --subject serviceAccount:<service_account_ID>
Result:
done (1s)
-
In the configuration file, describe the parameters of the resources you want to create:
resource "yandex_resourcemanager_cloud_iam_member" "admin" { cloud_id = "<cloud_ID>" role = "admin" member = "serviceAccount:<service_account_ID>" }
Where:
cloud_id
: Cloud ID. This is a required parameter.role
: Role you want to assign. This is a required parameter.member
: User or service account getting the role. Specify it asuserAccount:<user_ID>
orserviceAccount:<service_account_ID>
. This is a required parameter.
For more information about the
yandex_resourcemanager_folder_iam_member
resource parameters, see the relevant provider documentation . -
Make sure the configuration files are correct.
-
In the command line, go to the folder where you created the configuration file.
-
Run a check using this command:
terraform plan
If the configuration is described correctly, the terminal will display a list of created resources and their parameters. If the configuration contains any errors, Terraform will point them out.
-
-
Deploy cloud resources.
-
If the configuration does not contain any errors, run this command:
terraform apply
-
To assign cloud roles to the service account, use the setAccessBindings REST API method for the ServiceAccount resource or the ServiceAccountService/SetAccessBindings gRPC API call.
- On the management console home page
Deploy your resources
-
Set up the CLI profile to run operations on behalf of the service account:
CLI-
Create an authorized key for the service account and save it to the file:
yc iam key create \ --service-account-id <service_account_ID> \ --folder-id <folder_ID> \ --output key.json
Where:
--service-account-id
:sa-live-debug
service account ID.--folder-id
: ID of the folder in which the service account was created.--output
: Name of the file with the authorized key.
Result:
id: aje8nn871qo4******** service_account_id: ajehr0to1g8******** created_at: "2023-03-04T09:16:43.479156798Z" key_algorithm: RSA_2048
-
Create a CLI profile to run operations on behalf of the service account:
yc config profile create sa-live-debug
Result:
Profile 'sa-live-debug' created and activated
-
Set the profile configuration:
yc config set service-account-key key.json yc config set cloud-id <cloud_ID>
Where:
service-account-key
: File with the service account authorized key.cloud-id
: Cloud ID.
-
Add the credentials to the environment variables:
export YC_TOKEN=$(yc iam create-token) export YC_CLOUD_ID=$(yc config get cloud-id)
-
-
Deploy the resources in the cloud by running this command:
npx serverless-live-debug deploy
As a result, the command will create a folder named
live-debug
in the cloud and deploy all the required resources there.
Run the debugging service
-
In the
live-debug-test
folder, create a file namedlive-debug.config.ts
:nano live-debug.config.ts
-
Copy the code with the following configuration to the
live-debug.config.ts
file:import { defineConfig } from '@yandex-cloud/serverless-live-debug'; import { Handler } from '@yandex-cloud/function-types'; export default defineConfig({ handler: <Handler.Http>(event => { console.log('got request', event); return { statusCode: 200, body: `Hello from local code!`, }; }) });
-
Start the debugging service by running the following command:
npx serverless-live-debug run
Result:
Using config: live-debug.config.ts Running local client... Starting child... Child started Watching changes in: live-debug.config.ts WS connection opened Local client ready. Check url: https://d5ddt4ltdvh7********.apigw.yandexcloud.net Waiting requests...
Where
Check url
is the public address of the API gateway in API Gateway. -
Make sure the debug code is working properly. To do this, open another terminal and run this command:
curl https://d5ddt4ltdvh7********.apigw.yandexcloud.net
Result:
Hello from local code!
For more information about usage examples, see the yc-serverless-live-debug
How to delete the resources you created
Delete the folder with the resources required for interactive debugging of Cloud Functions functions:
- In the management console
, selectlive-debug
. - Click
next to the folder and select Delete. - In the Folder deletion period field, select
Delete now
. - Click Delete.
To delete a folder, use the delete REST API method for the Folder resource or the FolderService/Delete gRPC API call.