Skip to content

Commit 929750a

Browse files
committed
Implement yarn workspaces for Lambda functions management and update .gitignore
1 parent 75cc057 commit 929750a

File tree

7 files changed

+102
-31
lines changed

7 files changed

+102
-31
lines changed

typescript/postgres-lambda/.gitignore

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,34 @@
22
!jest.config.js
33
*.d.ts
44
node_modules
5+
**/node_modules
56

67
# CDK asset staging directory
78
.cdk.staging
89
cdk.out
10+
11+
# Yarn specific
12+
.yarn/*
13+
!.yarn/patches
14+
!.yarn/plugins
15+
!.yarn/releases
16+
!.yarn/sdks
17+
!.yarn/versions
18+
.pnp.*
19+
yarn-debug.log*
20+
yarn-error.log*
21+
22+
# Build artifacts
23+
dist/
24+
build/
25+
*.tsbuildinfo
26+
27+
# IDE files
28+
.idea/
29+
.vscode/
30+
*.swp
31+
*.swo
32+
33+
# OS files
34+
.DS_Store
35+
Thumbs.db

typescript/postgres-lambda/README.md

Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ A complete AWS CDK example demonstrating bidirectional integration between Auror
99
- **Secure Architecture**: Private subnets, IAM roles, and Secrets Manager integration
1010
- **Production-Ready**: Includes error handling, connection pooling, and security best practices
1111
- **Automated Setup**: Custom CDK resource automatically configures PostgreSQL extensions and functions
12+
- **Yarn Workspaces**: Organized monorepo structure for managing multiple Lambda functions
1213

1314
## Architecture
1415

@@ -23,10 +24,10 @@ graph TD
2324
end
2425
end
2526
26-
L1 -->|"(1) Connect and Query"| DB
27-
DB -->|"(2) Invoke via aws_lambda extension"| L2
28-
L2 -->|"(3) Return Result"| DB
29-
L3 -->|"(4) Setup Extensions & Functions"| DB
27+
L1 -->|"1. Connect and Query"| DB
28+
DB -->|"2. Invoke via aws_lambda extension"| L2
29+
L2 -->|"3. Return Result"| DB
30+
L3 -->|"4. Setup Extensions & Functions"| DB
3031
3132
SM[AWS Secrets Manager] -->|Provide Credentials| L1
3233
SM -->|Provide Credentials| L3
@@ -54,16 +55,17 @@ graph TD
5455

5556
- AWS CDK v2 installed (`npm install -g aws-cdk`)
5657
- Node.js 18.x or later
58+
- Yarn package manager installed
5759
- AWS CLI configured with appropriate credentials
5860

5961
### Deploy
6062

6163
```bash
62-
# Install dependencies
63-
npm install
64+
# Install dependencies using yarn workspaces
65+
yarn install
6466

6567
# Deploy the stack (setup is now automated!)
66-
npx cdk deploy
68+
yarn cdk deploy
6769
```
6870

6971
The deployment will automatically:
@@ -145,6 +147,30 @@ SELECT validate_data('{"id": 789, "value": "valid data"}'::JSONB);
145147
└── README.md # This file
146148
```
147149

150+
## Yarn Workspaces
151+
152+
This project uses Yarn Workspaces to manage multiple packages in a monorepo structure:
153+
154+
```bash
155+
# List all workspaces
156+
yarn workspaces list
157+
158+
# Run a command in all Lambda workspaces
159+
yarn workspaces foreach -v --include '@lambda/*' run <command>
160+
161+
# Run a command in a specific workspace
162+
yarn workspace @lambda/lambda-to-postgres run <command>
163+
164+
# Install dependencies for all workspaces
165+
yarn install
166+
```
167+
168+
The workspace structure allows for:
169+
- Shared dependencies between packages
170+
- Individual package management
171+
- Simplified build and deployment process
172+
- Better organization of Lambda functions
173+
148174
## Configuration
149175

150176
### Environment Variables
@@ -215,16 +241,16 @@ Before using in production:
215241

216242
```bash
217243
# Build and watch for changes
218-
npm run watch
244+
yarn watch
219245

220246
# Run tests
221-
npm run test
247+
yarn test
222248

223249
# View CloudFormation template
224-
npx cdk synth
250+
yarn cdk synth
225251

226252
# Compare deployed vs current state
227-
npx cdk diff
253+
yarn cdk diff
228254

229255
# View stack outputs
230256
aws cloudformation describe-stacks --stack-name PostgresLambdaStack --query 'Stacks[0].Outputs'
@@ -236,7 +262,7 @@ aws logs describe-log-groups --log-group-name-prefix /aws/lambda/PostgresLambdaS
236262
## Cleanup
237263

238264
```bash
239-
npx cdk destroy
265+
yarn cdk destroy
240266
```
241267

242268
**Note**: This will delete all resources including the database and any data stored in it.
@@ -265,4 +291,4 @@ npx cdk destroy
265291

266292
## License
267293

268-
This example is provided under the MIT-0 License. See the LICENSE file for details.
294+
This example is provided under the MIT-0 License. See the LICENSE file for details.
Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
{
2-
"name": "lambda-to-postgres",
2+
"name": "@lambda/lambda-to-postgres",
33
"version": "1.0.0",
4-
"description": "Lambda function that calls PostgreSQL",
4+
"description": "Lambda function that connects to PostgreSQL",
55
"main": "index.js",
6+
"scripts": {
7+
"build": "echo 'No build needed for JavaScript files'",
8+
"test": "echo \"Error: no test specified\" && exit 1"
9+
},
610
"dependencies": {
7-
"pg": "^8.11.3",
8-
"@aws-sdk/client-secrets-manager": "^3.450.0"
11+
"pg": "^8.16.3",
12+
"aws-sdk": "^2.1377.0"
913
}
1014
}
Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
{
2-
"name": "postgres-setup",
2+
"name": "@lambda/postgres-setup",
33
"version": "1.0.0",
4+
"description": "Lambda function for automated PostgreSQL setup",
5+
"main": "index.js",
6+
"scripts": {
7+
"build": "echo 'No build needed for JavaScript files'",
8+
"test": "echo \"Error: no test specified\" && exit 1"
9+
},
410
"dependencies": {
5-
"pg": "^8.11.3",
6-
"@aws-sdk/client-secrets-manager": "^3.0.0",
7-
"node-fetch": "^3.3.2"
11+
"pg": "^8.16.3",
12+
"aws-sdk": "^2.1377.0"
813
}
9-
}
14+
}
Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
{
2-
"name": "postgres-to-lambda",
2+
"name": "@lambda/postgres-to-lambda",
33
"version": "1.0.0",
4-
"description": "Lambda function that is called by PostgreSQL",
4+
"description": "Lambda function called by PostgreSQL",
55
"main": "index.js",
6-
"dependencies": {}
6+
"scripts": {
7+
"build": "echo 'No build needed for JavaScript files'",
8+
"test": "echo \"Error: no test specified\" && exit 1"
9+
},
10+
"dependencies": {
11+
"aws-sdk": "^2.1377.0"
12+
}
713
}

typescript/postgres-lambda/lib/postgres-lambda-stack.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,6 @@ export class PostgresLambdaStack extends cdk.Stack {
9898
DB_SECRET_ARN: dbCluster.secret?.secretArn || '',
9999
DB_NAME: 'demodb',
100100
POSTGRES_FUNCTION_NAME: postgresFunction.functionName,
101-
AWS_REGION: this.region,
102101
},
103102
timeout: cdk.Duration.minutes(5),
104103
});
@@ -142,4 +141,4 @@ export class PostgresLambdaStack extends cdk.Stack {
142141
description: 'The ARN of the role that PostgreSQL can assume to invoke Lambda',
143142
});
144143
}
145-
}
144+
}

typescript/postgres-lambda/package.json

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
{
22
"name": "postgres-lambda",
33
"version": "0.1.0",
4+
"private": true,
5+
"workspaces": [
6+
".",
7+
"lambda/*"
8+
],
49
"bin": {
510
"postgres-lambda": "bin/postgres-lambda.js"
611
},
712
"scripts": {
8-
"build": "tsc",
13+
"build": "tsc && yarn workspaces foreach -v -A run build",
14+
"build:lambda": "yarn workspaces foreach -v -A run build",
915
"watch": "tsc -w",
1016
"test": "jest",
1117
"cdk": "cdk"
@@ -21,10 +27,8 @@
2127
},
2228
"dependencies": {
2329
"@aws-cdk/aws-lambda-nodejs": "^1.203.0",
24-
"@types/pg": "^8.15.4",
2530
"aws-cdk-lib": "^2.204.0",
2631
"constructs": "^10.4.2",
27-
"esbuild": "^0.25.6",
28-
"pg": "^8.16.3"
32+
"esbuild": "^0.25.6"
2933
}
3034
}

0 commit comments

Comments
 (0)