@ttoss/lambda-postgres-query 0.3.16 → 0.3.18
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.
- package/README.md +88 -38
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -1,28 +1,22 @@
|
|
|
1
1
|
# @ttoss/lambda-postgres-query
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Create an AWS Lambda function to securely query a PostgreSQL database in a private VPC subnet without exposing the database to the internet.
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
## When to Use
|
|
6
6
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
2. Decompose your architecture into multiple Lambdas—some inside the VPC and some outside the VPC. The Lambda inside the VPC can query the database, and the Lambda outside the VPC can query the Lambda inside the VPC. On this approach, Lambdas outside the VPC invoke Lambdas inside the VPC using the AWS SDK to query the database. This approach is complex and requires more effort to maintain.
|
|
10
|
-
|
|
11
|
-
_Check this StackOverflow question for more information: [Why can't an AWS lambda function inside a public subnet in a VPC connect to the internet?](https://stackoverflow.com/questions/52992085/why-cant-an-aws-lambda-function-inside-a-public-subnet-in-a-vpc-connect-to-the)_
|
|
7
|
+
This package solves the challenge of querying a PostgreSQL database from AWS Lambda functions without internet access. Traditional approaches require expensive NAT Gateways or complex multi-Lambda architectures. This package provides a simpler solution by deploying a dedicated Lambda function within your VPC.
|
|
12
8
|
|
|
13
9
|
## Installation
|
|
14
10
|
|
|
15
|
-
To install this package, you need to run the following command:
|
|
16
|
-
|
|
17
11
|
```bash
|
|
18
12
|
pnpm install @ttoss/lambda-postgres-query
|
|
19
13
|
```
|
|
20
14
|
|
|
21
|
-
##
|
|
15
|
+
## Setup
|
|
22
16
|
|
|
23
|
-
### CloudFormation
|
|
17
|
+
### CloudFormation Template
|
|
24
18
|
|
|
25
|
-
Create a
|
|
19
|
+
Create a CloudFormation template to deploy the Lambda function:
|
|
26
20
|
|
|
27
21
|
```typescript
|
|
28
22
|
import { createLambdaPostgresQueryTemplate } from '@ttoss/lambda-postgres-query/cloudformation';
|
|
@@ -32,26 +26,32 @@ const template = createLambdaPostgresQueryTemplate();
|
|
|
32
26
|
export default template;
|
|
33
27
|
```
|
|
34
28
|
|
|
35
|
-
|
|
29
|
+
### Lambda Handler
|
|
30
|
+
|
|
31
|
+
Create a handler file that exports the Lambda function:
|
|
36
32
|
|
|
37
33
|
```typescript
|
|
38
34
|
export { handler } from '@ttoss/lambda-postgres-query';
|
|
39
35
|
```
|
|
40
36
|
|
|
41
|
-
|
|
37
|
+
### Environment Variables
|
|
38
|
+
|
|
39
|
+
Configure the following environment variables:
|
|
42
40
|
|
|
43
41
|
```env
|
|
44
|
-
DATABASE_NAME=
|
|
45
|
-
DATABASE_USERNAME=
|
|
46
|
-
DATABASE_PASSWORD=
|
|
47
|
-
DATABASE_HOST=
|
|
48
|
-
DATABASE_HOST_READ_ONLY=
|
|
49
|
-
DATABASE_PORT=
|
|
50
|
-
SECURITY_GROUP_IDS=
|
|
51
|
-
SUBNET_IDS=
|
|
42
|
+
DATABASE_NAME=your_database_name
|
|
43
|
+
DATABASE_USERNAME=your_username
|
|
44
|
+
DATABASE_PASSWORD=your_password
|
|
45
|
+
DATABASE_HOST=your_database_host
|
|
46
|
+
DATABASE_HOST_READ_ONLY=your_read_only_host # Optional
|
|
47
|
+
DATABASE_PORT=5432
|
|
48
|
+
SECURITY_GROUP_IDS=sg-xxxxx,sg-yyyyy
|
|
49
|
+
SUBNET_IDS=subnet-xxxxx,subnet-yyyyy
|
|
52
50
|
```
|
|
53
51
|
|
|
54
|
-
|
|
52
|
+
### Deployment
|
|
53
|
+
|
|
54
|
+
Add a deploy script to your `package.json`:
|
|
55
55
|
|
|
56
56
|
```json
|
|
57
57
|
{
|
|
@@ -61,44 +61,94 @@ Add the `deploy` script to the `package.json` file:
|
|
|
61
61
|
}
|
|
62
62
|
```
|
|
63
63
|
|
|
64
|
-
|
|
64
|
+
Deploy using Carlin:
|
|
65
65
|
|
|
66
66
|
```bash
|
|
67
67
|
pnpm deploy
|
|
68
68
|
```
|
|
69
69
|
|
|
70
|
-
|
|
70
|
+
**Note:** Set `lambdaFormat: 'cjs'` in your Carlin configuration, as the `pg` package requires CommonJS.
|
|
71
71
|
|
|
72
|
-
|
|
72
|
+
## Usage
|
|
73
73
|
|
|
74
|
-
### Querying
|
|
74
|
+
### Querying from External Lambdas
|
|
75
75
|
|
|
76
|
-
|
|
76
|
+
Query the database from Lambda functions outside the VPC:
|
|
77
77
|
|
|
78
78
|
```typescript
|
|
79
79
|
import { query } from '@ttoss/lambda-postgres-query';
|
|
80
80
|
import type { Handler } from 'aws-lambda';
|
|
81
81
|
|
|
82
82
|
export const handler: Handler = async (event) => {
|
|
83
|
-
const
|
|
84
|
-
const result = await query({ text });
|
|
83
|
+
const result = await query('SELECT * FROM users');
|
|
85
84
|
return result.rows;
|
|
86
85
|
};
|
|
87
86
|
```
|
|
88
87
|
|
|
89
|
-
|
|
88
|
+
### Advanced Query Options
|
|
89
|
+
|
|
90
|
+
```typescript
|
|
91
|
+
import { query } from '@ttoss/lambda-postgres-query';
|
|
90
92
|
|
|
91
|
-
|
|
93
|
+
// Query with parameters
|
|
94
|
+
const result = await query({
|
|
95
|
+
text: 'SELECT * FROM users WHERE id = $1',
|
|
96
|
+
values: [userId],
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
// Use read-only connection
|
|
100
|
+
const result = await query({
|
|
101
|
+
text: 'SELECT * FROM users',
|
|
102
|
+
readOnly: true, // Defaults to true
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
// Disable automatic camelCase conversion
|
|
106
|
+
const result = await query({
|
|
107
|
+
text: 'SELECT * FROM users',
|
|
108
|
+
camelCaseKeys: false, // Defaults to true
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
// Specify custom Lambda function name
|
|
112
|
+
const result = await query({
|
|
113
|
+
text: 'SELECT * FROM users',
|
|
114
|
+
lambdaPostgresQueryFunction: 'custom-function-name',
|
|
115
|
+
});
|
|
116
|
+
```
|
|
92
117
|
|
|
93
|
-
|
|
118
|
+
## API Reference
|
|
94
119
|
|
|
95
|
-
### `
|
|
120
|
+
### `createLambdaPostgresQueryTemplate(options?)`
|
|
96
121
|
|
|
97
|
-
|
|
122
|
+
Creates a CloudFormation template for the PostgreSQL query Lambda function.
|
|
98
123
|
|
|
99
124
|
#### Parameters
|
|
100
125
|
|
|
101
|
-
|
|
126
|
+
- `handler` (string, optional): Lambda handler function name. Default: `'handler.handler'`
|
|
127
|
+
- `memorySize` (number, optional): Lambda memory size in MB. Default: `128`
|
|
128
|
+
- `timeout` (number, optional): Lambda timeout in seconds. Default: `30`
|
|
129
|
+
|
|
130
|
+
#### Returns
|
|
131
|
+
|
|
132
|
+
A CloudFormation template object.
|
|
133
|
+
|
|
134
|
+
### `query(params)`
|
|
135
|
+
|
|
136
|
+
Queries the PostgreSQL database by invoking the VPC Lambda function.
|
|
137
|
+
|
|
138
|
+
#### Parameters
|
|
139
|
+
|
|
140
|
+
Accepts either a SQL string or an options object extending [`QueryConfig`](https://node-postgres.com/apis/client#queryconfig) with additional properties:
|
|
141
|
+
|
|
142
|
+
- `text` (string): SQL query text
|
|
143
|
+
- `values` (array, optional): Query parameter values
|
|
144
|
+
- `readOnly` (boolean, optional): Use read-only database host if available. Default: `true`
|
|
145
|
+
- `lambdaPostgresQueryFunction` (string, optional): Name of the query Lambda function. Default: `LAMBDA_POSTGRES_QUERY_FUNCTION` environment variable
|
|
146
|
+
- `camelCaseKeys` (boolean, optional): Convert snake_case column names to camelCase. Default: `true`
|
|
147
|
+
|
|
148
|
+
#### Returns
|
|
149
|
+
|
|
150
|
+
A [`QueryResult`](https://node-postgres.com/apis/result) object with transformed rows.
|
|
151
|
+
|
|
152
|
+
### `handler`
|
|
102
153
|
|
|
103
|
-
|
|
104
|
-
- `lambdaPostgresQueryFunction`: The name of the Lambda function that queries the database. Default is the value of the `LAMBDA_POSTGRES_QUERY_FUNCTION` environment variable.
|
|
154
|
+
AWS Lambda handler function for processing database queries within the VPC.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ttoss/lambda-postgres-query",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.18",
|
|
4
4
|
"description": "Create a Lambda function that queries a PostgreSQL database.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "ttoss",
|
|
@@ -32,7 +32,7 @@
|
|
|
32
32
|
"@aws-sdk/client-lambda": "^3.731.1",
|
|
33
33
|
"camelcase-keys": "^7.0.2",
|
|
34
34
|
"pg": "^8.16.3",
|
|
35
|
-
"@ttoss/cloudformation": "^0.11.
|
|
35
|
+
"@ttoss/cloudformation": "^0.11.9"
|
|
36
36
|
},
|
|
37
37
|
"devDependencies": {
|
|
38
38
|
"@types/jest": "^30.0.0",
|
|
@@ -41,7 +41,7 @@
|
|
|
41
41
|
"jest": "^30.2.0",
|
|
42
42
|
"tsup": "^8.5.1",
|
|
43
43
|
"@ttoss/config": "^1.35.12",
|
|
44
|
-
"@ttoss/test-utils": "^4.0.
|
|
44
|
+
"@ttoss/test-utils": "^4.0.2"
|
|
45
45
|
},
|
|
46
46
|
"keywords": [
|
|
47
47
|
"aws",
|