@leanstacks/lambda-utils 0.1.0-alpha.2 → 0.1.0-alpha.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,47 @@
1
+ ---
2
+ name: Bug report
3
+ about: Create a report to help us improve
4
+ title: ''
5
+ labels: bug
6
+ assignees: ''
7
+ ---
8
+
9
+ ## Describe the bug
10
+
11
+ _Provide a clear and concise description of what the bug is._
12
+
13
+ ## Steps to reproduce
14
+
15
+ Steps to reproduce the behavior:
16
+
17
+ 1. Go to '...'
18
+ 2. Click on '....'
19
+ 3. Scroll down to '....'
20
+ 4. See error
21
+
22
+ ## Expected behavior
23
+
24
+ _Provide a clear and concise description of what you expected to happen._
25
+
26
+ ## Screenshots
27
+
28
+ _If applicable, add screenshots to help explain your problem._
29
+
30
+ ## Environment
31
+
32
+ **Desktop (please complete the following information):**
33
+
34
+ - OS: [e.g. iOS]
35
+ - Browser [e.g. chrome, safari]
36
+ - Version [e.g. 22]
37
+
38
+ **Smartphone (please complete the following information):**
39
+
40
+ - Device: [e.g. iPhone6]
41
+ - OS: [e.g. iOS8.1]
42
+ - Browser [e.g. stock browser, safari]
43
+ - Version [e.g. 22]
44
+
45
+ ## Additional context
46
+
47
+ _Add any other context about the problem here._
@@ -0,0 +1,25 @@
1
+ ---
2
+ name: Story
3
+ about: New feature or improvement request
4
+ title: ''
5
+ labels: enhancement
6
+ assignees: ''
7
+ ---
8
+
9
+ ## Describe the story
10
+
11
+ _Provide a clear description of the new feature or improvement to existing functionality._
12
+
13
+ ## Acceptance criteria
14
+
15
+ _Provide clear acceptance criteria to validate the story is complete._
16
+
17
+ [Gherkin syntax](https://cucumber.io/docs/gherkin/reference) example:
18
+
19
+ > Given the 'PERSONA' has 'DONE SOMETHING'
20
+ > When the 'PERSONA' does 'ONE THING'
21
+ > Then the 'PERSONA' must do 'ANOTHER THING'
22
+
23
+ ## Additional context
24
+
25
+ _Add any other context about the story here._
@@ -0,0 +1,15 @@
1
+ ---
2
+ name: Task
3
+ about: A chore unrelated to features or problems
4
+ title: ''
5
+ labels: task
6
+ assignees: ''
7
+ ---
8
+
9
+ ## Describe the task
10
+
11
+ _Provide a clear description of the task._
12
+
13
+ ## Additional context
14
+
15
+ _Add any other context about the task here._
@@ -0,0 +1,39 @@
1
+ :loudspeaker: **Instructions**
2
+
3
+ - Begin with a **DRAFT** pull request.
4
+ - Follow _italicized instructions_ to add detail to assist the reviewers.
5
+ - After completing all checklist items, change the pull request to **READY**.
6
+
7
+ ---
8
+
9
+ ### :wrench: Change Summary
10
+
11
+ _Describe the changes included in this pull request. Link to the associated [GitHub](https://docs.github.com/en/issues/tracking-your-work-with-issues/using-issues/linking-a-pull-request-to-an-issue#linking-a-pull-request-to-an-issue-using-a-keyword) or Jira issue(s)._
12
+
13
+ - see #1234
14
+ - Added the [...]
15
+ - Updated the [...]
16
+ - Fixed the [...]
17
+
18
+ ### :memo: Checklist
19
+
20
+ _Pull request authors must complete the following tasks before marking the PR as ready to review._
21
+
22
+ - [ ] Complete a self-review of changes
23
+ - [ ] Unit tests have been created or updated
24
+ - [ ] The code is free of [new] lint errors and warnings
25
+ - [ ] Update project documentation as needed: README, /docs, JSDoc, etc.
26
+
27
+ ### :test_tube: Steps to Test
28
+
29
+ _Describe the process to test the changes in this pull request._
30
+
31
+ 1. Go to [...]
32
+ 2. Click on [...]
33
+ 3. Verify that [...]
34
+
35
+ ### :link: Additional Information
36
+
37
+ _Optionally, provide additional details, screenshots, or URLs that may assist the reviewer._
38
+
39
+ - [...]
package/README.md CHANGED
@@ -1,3 +1,180 @@
1
1
  # Lambda Utilities
2
2
 
3
- Lambda utilities
3
+ [![npm version](https://badge.fury.io/js/@leanstacks%2Flambda-utils.svg)](https://badge.fury.io/js/@leanstacks%2Flambda-utils)
4
+ [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
5
+
6
+ A comprehensive TypeScript utility library for AWS Lambda functions. Provides pre-configured logging, API response formatting, configuration validation, and AWS SDK clients—reducing boilerplate and promoting best practices.
7
+
8
+ ## Table of Contents
9
+
10
+ - [Installation](#installation)
11
+ - [Quick Start](#quick-start)
12
+ - [Features](#features)
13
+ - [Documentation](#documentation)
14
+ - [Contributing](#contributing)
15
+ - [License](#license)
16
+ - [Support](#support)
17
+
18
+ ## Installation
19
+
20
+ ```bash
21
+ npm install @leanstacks/lambda-utils
22
+ ```
23
+
24
+ ### Requirements
25
+
26
+ - Node.js 24.x or higher
27
+ - TypeScript 5.0 or higher
28
+
29
+ ## Quick Start
30
+
31
+ ### Logging Example
32
+
33
+ ```typescript
34
+ import { Logger, withRequestTracking } from '@leanstacks/lambda-utils';
35
+
36
+ const logger = new Logger().instance;
37
+
38
+ export const handler = async (event: any, context: any) => {
39
+ withRequestTracking(event, context);
40
+
41
+ logger.info('Processing request');
42
+
43
+ // Your Lambda handler logic here
44
+
45
+ return { statusCode: 200, body: 'Success' };
46
+ };
47
+ ```
48
+
49
+ ### API Response Example
50
+
51
+ ```typescript
52
+ import { success, badRequest } from '@leanstacks/lambda-utils';
53
+
54
+ export const handler = async (event: any) => {
55
+ if (!event.body) {
56
+ return badRequest({ message: 'Body is required' });
57
+ }
58
+
59
+ // Process request
60
+
61
+ return success({ message: 'Request processed successfully' });
62
+ };
63
+ ```
64
+
65
+ ## Features
66
+
67
+ - **📝 Structured Logging** – Pino logger pre-configured for Lambda with automatic AWS request context enrichment
68
+ - **📤 API Response Helpers** – Standard response formatting for API Gateway with proper HTTP status codes
69
+ - **⚙️ Configuration Validation** – Environment variable validation with Zod schema support
70
+ - **🔌 AWS SDK Clients** – Pre-configured AWS SDK v3 clients for DynamoDB, Lambda, and more
71
+ - **🔒 Full TypeScript Support** – Complete type definitions and IDE autocomplete
72
+ - **⚡ Lambda Optimized** – Designed for performance in serverless environments
73
+
74
+ ## Documentation
75
+
76
+ Comprehensive guides and examples are available in the `docs` directory:
77
+
78
+ | Guide | Description |
79
+ | ------------------------------------------------------------ | ---------------------------------------------------------------------- |
80
+ | **[Logging Guide](./docs/LOGGING.md)** | Configure and use structured logging with automatic AWS Lambda context |
81
+ | **[API Gateway Responses](./docs/API_GATEWAY_RESPONSES.md)** | Format responses for API Gateway with standard HTTP patterns |
82
+ | **[Configuration](./docs/CONFIGURATION.md)** | Validate and manage environment variables with type safety |
83
+ | **[AWS Clients](./docs/CLIENTS.md)** | Use pre-configured AWS SDK v3 clients in your handlers |
84
+ | **[Getting Started](./docs/GETTING_STARTED.md)** | Setup and first steps guide |
85
+
86
+ ## Usage
87
+
88
+ ### Logging
89
+
90
+ The Logger utility provides structured logging configured specifically for AWS Lambda:
91
+
92
+ ```typescript
93
+ import { Logger } from '@leanstacks/lambda-utils';
94
+
95
+ const logger = new Logger({
96
+ level: 'info', // debug, info, warn, error
97
+ format: 'json', // json or text
98
+ }).instance;
99
+
100
+ logger.info({ message: 'User authenticated', userId: '12345' });
101
+ logger.error({ message: 'Operation failed', error: err.message });
102
+ ```
103
+
104
+ **→ See [Logging Guide](./docs/LOGGING.md) for detailed configuration and best practices**
105
+
106
+ ### API Responses
107
+
108
+ Generate properly formatted responses for API Gateway:
109
+
110
+ ```typescript
111
+ import { success, error, created, badRequest } from '@leanstacks/lambda-utils';
112
+
113
+ export const handler = async (event: any) => {
114
+ return success({
115
+ data: { id: '123', name: 'Example' },
116
+ });
117
+ };
118
+ ```
119
+
120
+ **→ See [API Gateway Responses](./docs/API_GATEWAY_RESPONSES.md) for all response types**
121
+
122
+ ### Configuration Validation
123
+
124
+ Validate your Lambda environment configuration:
125
+
126
+ ```typescript
127
+ import { validateConfig } from '@leanstacks/lambda-utils';
128
+ import { z } from 'zod';
129
+
130
+ const configSchema = z.object({
131
+ DATABASE_URL: z.string().url(),
132
+ LOG_LEVEL: z.enum(['debug', 'info', 'warn', 'error']),
133
+ API_KEY: z.string(),
134
+ });
135
+
136
+ const config = validateConfig(configSchema);
137
+ ```
138
+
139
+ **→ See [Configuration](./docs/CONFIGURATION.md) for validation patterns**
140
+
141
+ ### AWS Clients
142
+
143
+ Use pre-configured AWS SDK v3 clients:
144
+
145
+ ```typescript
146
+ import { getDynamoDBClient, getLambdaClient } from '@leanstacks/lambda-utils';
147
+
148
+ const dynamoDB = getDynamoDBClient();
149
+ const lambda = getLambdaClient();
150
+
151
+ // Use clients for API calls
152
+ ```
153
+
154
+ **→ See [AWS Clients](./docs/CLIENTS.md) for available clients and examples**
155
+
156
+ ## Examples
157
+
158
+ Example Lambda functions using Lambda Utilities are available in the repository:
159
+
160
+ - API Gateway with logging and response formatting
161
+ - Configuration validation and DynamoDB integration
162
+ - Error handling and structured logging
163
+
164
+ ## Reporting Issues
165
+
166
+ If you encounter a bug or have a feature request, please report it on [GitHub Issues](https://github.com/leanstacks/lambda-utils/issues). Include as much detail as possible to help us investigate and resolve the issue quickly.
167
+
168
+ ## License
169
+
170
+ This project is licensed under the MIT License - see [LICENSE](./LICENSE) file for details.
171
+
172
+ ## Support
173
+
174
+ - **Issues & Questions:** [GitHub Issues](https://github.com/leanstacks/lambda-utils/issues)
175
+ - **Documentation:** [docs](./docs/README.md)
176
+ - **NPM Package:** [@leanstacks/lambda-utils](https://www.npmjs.com/package/@leanstacks/lambda-utils)
177
+
178
+ ## Changelog
179
+
180
+ See the project [releases](https://github.com/leanstacks/lambda-utils/releases) for version history and updates.
@@ -0,0 +1,333 @@
1
+ # Logging Guide
2
+
3
+ This guide explains how to use the Logger utility to implement structured logging in your AWS Lambda functions using TypeScript.
4
+
5
+ ## Overview
6
+
7
+ The Logger utility provides a thin wrapper around [Pino](https://getpino.io/) configured specifically for AWS Lambda. It automatically includes Lambda request context information in your logs and supports multiple output formats suitable for CloudWatch.
8
+
9
+ ## Installation
10
+
11
+ The Logger utility is included in the `@leanstacks/lambda-utils` package:
12
+
13
+ ```bash
14
+ npm install @leanstacks/lambda-utils
15
+ ```
16
+
17
+ ## Quick Start
18
+
19
+ ### Basic Usage
20
+
21
+ ```typescript
22
+ import { Logger } from '@leanstacks/lambda-utils';
23
+
24
+ const logger = new Logger().instance;
25
+
26
+ export const handler = async (event: any, context: any) => {
27
+ logger.info('[Handler] > Processing request');
28
+
29
+ // Your handler logic here
30
+
31
+ logger.info({ key: 'value' }, '[Handler] < Completed request');
32
+
33
+ return { statusCode: 200, body: 'Success' };
34
+ };
35
+ ```
36
+
37
+ ## Configuration
38
+
39
+ The Logger accepts a configuration object to customize its behavior:
40
+
41
+ ```typescript
42
+ import { Logger } from '@leanstacks/lambda-utils';
43
+
44
+ const logger = new Logger({
45
+ enabled: true, // Enable/disable logging (default: true)
46
+ level: 'info', // Minimum log level (default: 'info')
47
+ format: 'json', // Output format: 'json' or 'text' (default: 'json')
48
+ }).instance;
49
+ ```
50
+
51
+ ### Configuration Options
52
+
53
+ | Option | Type | Default | Description |
54
+ | --------- | ---------------------------------------- | -------- | -------------------------------------------------------------------------------------------------------- |
55
+ | `enabled` | `boolean` | `true` | Whether logging is enabled. Set to `false` to disable all logging output. |
56
+ | `level` | `'debug' \| 'info' \| 'warn' \| 'error'` | `'info'` | Minimum log level to output. Messages below this level are filtered out. |
57
+ | `format` | `'json' \| 'text'` | `'json'` | Output format for log messages. Use `'json'` for structured logging or `'text'` for human-readable logs. |
58
+
59
+ ### Log Levels
60
+
61
+ Log levels are ordered by severity:
62
+
63
+ - **`debug`**: Detailed information for diagnosing problems (lowest severity)
64
+ - **`info`**: General informational messages about application flow
65
+ - **`warn`**: Warning messages for potentially harmful situations
66
+ - **`error`**: Error messages for serious problems (highest severity)
67
+
68
+ ## Logging Examples
69
+
70
+ ### Basic Logging
71
+
72
+ ```typescript
73
+ const logger = new Logger().instance;
74
+
75
+ logger.debug('Detailed diagnostic information');
76
+ logger.info('Application event or milestone');
77
+ logger.warn('Warning: something unexpected occurred');
78
+ logger.error('Error: operation failed');
79
+ ```
80
+
81
+ When the log message contains a simple string, pass the string as the only aregument to the logger function.
82
+
83
+ ### Structured Logging with Objects
84
+
85
+ ```typescript
86
+ const logger = new Logger().instance;
87
+
88
+ const userId = '12345';
89
+ const permissions = ['user:read', 'user:write'];
90
+
91
+ logger.info(
92
+ {
93
+ userId,
94
+ permissions,
95
+ },
96
+ 'User authenticated',
97
+ );
98
+ ```
99
+
100
+ When using structured logging, pass the context attributes object as the first parameter and the string log message as the second parameter. This allows the logger to properly format messages as either JSON or text.
101
+
102
+ ### Error Logging
103
+
104
+ ```typescript
105
+ const logger = new Logger().instance;
106
+
107
+ try {
108
+ // Your code here
109
+ } catch (error) {
110
+ logger.error(
111
+ {
112
+ error: error instanceof Error ? error.message : String(error),
113
+ stack: error instanceof Error ? error.stack : undefined,
114
+ },
115
+ 'Operation failed',
116
+ );
117
+ }
118
+ ```
119
+
120
+ ## Advanced Usage
121
+
122
+ ### Request Tracking Middleware
123
+
124
+ The `withRequestTracking` middleware automatically adds AWS Lambda context information to all log messages. This enriches your logs with request IDs, function names, and other Lambda metadata.
125
+
126
+ ```typescript
127
+ import { Logger, withRequestTracking } from '@leanstacks/lambda-utils';
128
+
129
+ const logger = new Logger().instance;
130
+
131
+ export const handler = async (event: any, context: any) => {
132
+ // Add Lambda context to all subsequent log messages
133
+ withRequestTracking(event, context);
134
+
135
+ logger.info('Request started');
136
+
137
+ // Your handler logic here
138
+
139
+ return { statusCode: 200 };
140
+ };
141
+ ```
142
+
143
+ ### Environment-Based Configuration
144
+
145
+ Configure logging based on your environment:
146
+
147
+ ```typescript
148
+ import { Logger } from '@leanstacks/lambda-utils';
149
+
150
+ const isProduction = process.env.NODE_ENV === 'production';
151
+ const isDevelopment = process.env.NODE_ENV === 'development';
152
+
153
+ const logger = new Logger({
154
+ level: isDevelopment ? 'debug' : 'info',
155
+ format: isProduction ? 'json' : 'text',
156
+ }).instance;
157
+ ```
158
+
159
+ ### Singleton Pattern for Reusable Logger
160
+
161
+ For best performance, create a single logger instance and reuse it throughout your application:
162
+
163
+ ```typescript
164
+ // logger.ts
165
+ import { Logger } from '@leanstacks/lambda-utils';
166
+
167
+ export const logger = new Logger({
168
+ level: (process.env.LOG_LEVEL as 'debug' | 'info' | 'warn' | 'error') || 'info',
169
+ format: (process.env.LOG_FORMAT as 'json' | 'text') || 'json',
170
+ }).instance;
171
+ ```
172
+
173
+ Then import it in your handlers:
174
+
175
+ ```typescript
176
+ // handler.ts
177
+ import { logger } from './logger';
178
+
179
+ export const handler = async (event: any) => {
180
+ logger.info({ message: 'Processing event', event });
181
+
182
+ // Your handler logic here
183
+
184
+ return { statusCode: 200 };
185
+ };
186
+ ```
187
+
188
+ ## Best Practices
189
+
190
+ ### 1. Use Structured Logging
191
+
192
+ Prefer objects over string concatenation:
193
+
194
+ ```typescript
195
+ // ✅ Good: Structured logging
196
+ logger.info(
197
+ {
198
+ userId: user.id,
199
+ },
200
+ 'User login',
201
+ );
202
+
203
+ // ❌ Avoid: String concatenation
204
+ logger.info(`User ${user.id} logged in at ${new Date().toISOString()}`);
205
+ ```
206
+
207
+ ### 2. Include Relevant Context
208
+
209
+ Include all relevant information that will help with debugging and monitoring:
210
+
211
+ ```typescript
212
+ logger.info(
213
+ {
214
+ orderId: order.id,
215
+ amount: order.total,
216
+ paymentMethod: order.paymentMethod,
217
+ duration: endTime - startTime,
218
+ },
219
+ 'Payment processed',
220
+ );
221
+ ```
222
+
223
+ ### 3. Use Appropriate Log Levels
224
+
225
+ Choose log levels that match the severity and importance of the event:
226
+
227
+ ```typescript
228
+ logger.debug('Cache hit for user profile'); // Development diagnostics
229
+ logger.info('User registered successfully'); // Normal operations
230
+ logger.warn('API rate limit approaching'); // Potential issues
231
+ logger.error('Database connection failed'); // Critical failures
232
+ ```
233
+
234
+ ### 4. Avoid Logging Sensitive Information
235
+
236
+ Never log passwords, API keys, tokens, or personally identifiable information (PII):
237
+
238
+ ```typescript
239
+ // ❌ Never do this
240
+ logger.info({ password: user.password });
241
+
242
+ // ✅ Log safe information
243
+ logger.info({ userId: user.id, email: user.email });
244
+ ```
245
+
246
+ ### 5. Performance Considerations
247
+
248
+ The logger is optimized for Lambda and uses lazy evaluation. Only use `debug` level logs in development:
249
+
250
+ ```typescript
251
+ // Disable debug logs in production for better performance
252
+ const logger = new Logger({
253
+ level: process.env.NODE_ENV === 'production' ? 'info' : 'debug',
254
+ }).instance;
255
+ ```
256
+
257
+ ## Output Formats
258
+
259
+ ### JSON Format (Default)
260
+
261
+ Best for production environments and log aggregation services like CloudWatch, Datadog, or Splunk:
262
+
263
+ ```json
264
+ {
265
+ "timestamp": "2025-12-18T13:42:40.502Z",
266
+ "level": "INFO",
267
+ "requestId": "req-abc-123",
268
+ "message": {
269
+ "awsRequestId": "req-def-456",
270
+ "x-correlation-trace-id": "Root=1-2a-28ab;Parent=1e6;Sampled=0;Lineage=1:bf3:0",
271
+ "x-correlation-id": "crl-abc-123",
272
+ "time": 1702900123456,
273
+ "pid": 1,
274
+ "hostname": "lambda-container",
275
+ "key": "value",
276
+ "msg": "User authenticated"
277
+ }
278
+ }
279
+ ```
280
+
281
+ ### Text Format
282
+
283
+ Best for local development and human-readable output:
284
+
285
+ ```
286
+ [2024-12-18T12:34:56.789Z] INFO: User authenticated userId=12345 requestId=req-abc-123
287
+ ```
288
+
289
+ ## Testing
290
+
291
+ When testing Lambda functions that use the logger, you can mock or configure the logger:
292
+
293
+ ```typescript
294
+ import { Logger } from '@leanstacks/lambda-utils';
295
+
296
+ describe('MyHandler', () => {
297
+ it('should log info message', () => {
298
+ const logger = new Logger({
299
+ enabled: true,
300
+ level: 'info',
301
+ }).instance;
302
+
303
+ const spyLog = jest.spyOn(logger, 'info');
304
+
305
+ // Your test code here
306
+
307
+ expect(spyLog).toHaveBeenCalledWith({
308
+ message: 'Expected message',
309
+ });
310
+ });
311
+ });
312
+ ```
313
+
314
+ ## Troubleshooting
315
+
316
+ ### Logs Not Appearing
317
+
318
+ 1. **Check if logging is enabled**: Verify `enabled: true` in configuration
319
+ 2. **Check log level**: Ensure the message log level meets the configured minimum level. Check the Lambda function [Logging configuration application log level](https://docs.aws.amazon.com/lambda/latest/dg/monitoring-cloudwatchlogs-log-level.html).
320
+ 3. **Check CloudWatch**: Logs appear in CloudWatch Logs under `/aws/lambda/[function-name]`
321
+
322
+ ### Performance Issues
323
+
324
+ 1. **Use appropriate log level**: Reduce logs in production by using `level: 'info'`
325
+ 2. **Limit object size**: Avoid logging very large objects that could impact performance
326
+ 3. **Use singleton pattern**: Create one logger instance and reuse it
327
+
328
+ ## Further reading
329
+
330
+ - [Pino Documentation](https://getpino.io/)
331
+ - [AWS Lambda Environment and Context](https://docs.aws.amazon.com/lambda/latest/dg/nodejs-handler.html)
332
+ - [CloudWatch Logs Insights](https://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/CWL_QuerySyntax.html)
333
+ - [Back to the project documentation](README.md)