https://github.com/vitalets/yc-serverless-live-debug-original
Live debug of Yandex cloud functions with local code on Node.js
https://github.com/vitalets/yc-serverless-live-debug-original
Last synced: 7 months ago
JSON representation
Live debug of Yandex cloud functions with local code on Node.js
- Host: GitHub
- URL: https://github.com/vitalets/yc-serverless-live-debug-original
- Owner: vitalets
- Created: 2023-01-13T09:55:38.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2023-03-11T17:26:32.000Z (over 2 years ago)
- Last Synced: 2025-03-22T18:51:39.319Z (7 months ago)
- Language: TypeScript
- Size: 1.09 MB
- Stars: 4
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# yc-serverless-live-debug
Live debug of Yandex Cloud Functions in Node.js.- [How it works](#how-it-works)
- [Setup](#setup)
- [Usage](#usage)
+ [Debug single function](#debug-single-function)
+ [Debug several functions](#debug-several-functions)
+ [Debug other triggers](#debug-other-triggers)
- [Watch](#watch)## How it works
Main components:
1. `stub` - cloud function that proxies requests to local code
2. `store` - cloud function that stores WebSocket connections info in YDB. This connection info is later used by `stub` to know where to proxy request
3. `client` - CLI app running on local machine and handling requests coming by WebSocket> The schema was inspired by [SST Live Lambda Dev](https://docs.sst.dev/live-lambda-development)
## Setup
Ensure that you have [Terraform installed](https://cloud.yandex.ru/docs/tutorials/infrastructure-management/terraform-quickstart).Install package:
```
npm i -D @yandex-cloud/sls-live-debug
```Deploy cloud components:
```
npx live-debug deploy
```
Review terraform plan and press **Approve**.> By default this command uses [yc cli](https://cloud.yandex.ru/docs/cli/) to get auth token and cloud id. You can manually set these values by `YC_TOKEN` and `YC_CLOUD_ID` env vars
> To authorize by service account key use `YC_SERVICE_ACCOUNT_KEY_FILE` env var:
```
YC_SERVICE_ACCOUNT_KEY_FILE=path/to/key.json npx sls-live-debug deploy
```> By default all components will be created in separate cloud catalogue `live-debug`. You can change this name using `LIVE_DEBUG_FOLDER_NAME` env var:
```
LIVE_DEBUG_FOLDER_NAME=live-debug-test npx sls-live-debug deploy
```Create `live-debug.config.ts` in project root:
```ts
import { defineConfig } from '@yandex-cloud/sls-live-debug';
import { Handler } from '@yandex-cloud/function-types';export default defineConfig({
handler: (event => {
console.log('got request', event);
return {
statusCode: 200,
body: `Hello from local code!`,
};
})
});
```Or `live-debug.config.js` (cjs):
```js
const { defineConfig } = require('@yandex-cloud/sls-live-debug');module.exports = defineConfig({
handler: event => {
console.log('got request', event);
return {
statusCode: 200,
body: `Hello from local code!`,
};
}
});
```Run live debug:
```
npx sls-live-debug run
```
Expected output:
```
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://**********.apigw.yandexcloud.net
Waiting requests...
GET /?
Response sent
```
Click provided link and check console.See [example](/example) for more details.
> Don't forget to add `.live-debug` dir to `.gitignore`
## Usage
All requests to cloud `stub` function will come to config's handler.
Inside handler you can setup routing for your needs.#### Debug single function
To debug single function you can just assign handler in config:
```ts
import { defineConfig } from '@yandex-cloud/sls-live-debug';
import { handler } from './path/to/your/handler';export default defineConfig({
handler
});
```#### Debug several functions
To debug several functions you can setup routing inside handler (for example by url):
```ts
import { defineConfig } from '@yandex-cloud/sls-live-debug';
import { Handler } from '@yandex-cloud/function-types';
import { handlerA } from './path/to/your/handler-a';
import { handlerB } from './path/to/your/handler-b';export default defineConfig({
handler: ((event, ctx) => {
// @ts-expect-error url is not typed
const url = String(event.url);
if (url.startsWith('/handler-a')) return handlerA(event, ctx);
if (url.startsWith('/handler-b')) return handlerB(event, ctx);
return { statusCode: 200, body: 'No handler' };
})
});
```#### Debug other triggers
You can debug all other triggers: message queue, object storage, etc.
In cloud console configure needed [trigger](https://cloud.yandex.ru/docs/serverless-containers/concepts/trigger/) to point to `stub` function.
```ts
import { defineConfig } from '@yandex-cloud/sls-live-debug';
import { Handler } from '@yandex-cloud/function-types';export default defineConfig({
handler: (event => {
console.log(event.messages);
})
});
```## Watch
By default process watches changes in `live-debug.config.ts` and updates handler.
You can set `watch` key in config to watch additional files and directories:
```ts
import { defineConfig } from '@yandex-cloud/sls-live-debug';export default defineConfig({
watch: 'src',
handler: ...
});
```