https://github.com/if1live/serverless-standalone
Emulate AWS λ and aws-sdk locally when developing your Serverless node.js project
https://github.com/if1live/serverless-standalone
aws-lambda serverless
Last synced: 2 months ago
JSON representation
Emulate AWS λ and aws-sdk locally when developing your Serverless node.js project
- Host: GitHub
- URL: https://github.com/if1live/serverless-standalone
- Owner: if1live
- Created: 2023-08-13T07:03:54.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2023-12-31T18:05:48.000Z (over 1 year ago)
- Last Synced: 2025-02-02T18:14:40.157Z (4 months ago)
- Topics: aws-lambda, serverless
- Language: TypeScript
- Homepage:
- Size: 228 KB
- Stars: 3
- Watchers: 2
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# serverless-standalone
Emulate AWS λ and aws-sdk locally when developing your Serverless node.js project
## features
- AWS WebSocket API + @aws-sdk/client-apigatewaymanagementapi
- [Schedule](https://www.serverless.com/framework/docs/providers/aws/events/schedule)## demo
- server: `pnpm dev`, see `examples/dev.ts`
- client: `wscat -c "ws://127.0.0.1:9001/path?foo=1&foo=2"`
- aws-sdk example: `node ./examples/index.mjs {connectionId}`## usage
define serverless function. similar with serverless.yml.
```ts
const websocket_connect: APIGatewayProxyHandler = async (event, context) => {
...
}
const websocket_disconnect: APIGatewayProxyHandler = async (event, context) => {
...
}const definitions: FunctionDefinition[] = [
{
handler: websocket_connect,
events: [{ websocket: { route: "$connect" } }],
},
{
handler: websocket_disconnect,
events: [{ websocket: { route: "$disconnect" } }],
},
];
```start serverless-standalone at localhost.
```ts
await StandAlone.start(definitions, {
http: 9000,
websocket: 9001,
api: 9002,
});
```connect websocket. aws lambda handler are invoked.
`wscat -c "ws://127.0.0.1:9001/path?username=me&password=pw"`use aws-sdk locally.
```ts
const client = new ApiGatewayManagementApiClient({
region: "ap-northeast-1",
endpoint: "http://127.0.0.1:9001/",
credentials: {
accessKeyId: "localAccessKeyId",
secretAccessKey: "localAecretAccessKey",
},
});const output = await client.send(
new PostToConnectionCommand({
ConnectionId: connectionId,
Data: new TextEncoder().encode("hello"),
}),
);
```