Repository navigation
Expand file tree
/
Copy pathinfrastructure-stack-patterns.mdc
More file actions
564 lines (485 loc) · 17.1 KB
/
Copy pathinfrastructure-stack-patterns.mdc
File metadata and controls
564 lines (485 loc) · 17.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
---
description:
globs: **/*/handler.ts
alwaysApply: false
---
# Infrastructure Stack Patterns
Guidelines for creating and maintaining CDK stacks for API services in the T6C project.
## MANDATORY Stack Structure
### Required Imports and Setup
**EVERY stack MUST follow this exact import and setup pattern:**
```typescript
import path from 'node:path'
import { fileURLToPath } from 'node:url'
import * as cdk from 'aws-cdk-lib'
import type * as apigateway from 'aws-cdk-lib/aws-apigateway'
import {
AttributeType,
type ITable,
ProjectionType,
} from 'aws-cdk-lib/aws-dynamodb'
import type { Construct } from 'constructs'
import 'source-map-support/register'
import {
ApiConstruct,
ApiGatewayHandlerLambda,
BaseStack,
type BaseStackProps,
DynamoDatabaseTableWithCompositeKey,
} from './constructs/index.js'
import { getEnvVarOrThrow } from './util/index.js'
const __filename = fileURLToPath(import.meta.url)
const __dirname = path.dirname(__filename)
```
### Required Stack Class Structure
**EVERY stack MUST extend BaseStack and follow this pattern:**
```typescript
type {Service}StackProps = {
// Dependencies from other stacks
userServiceUrl: string
} & BaseStackProps
export class {Service}Stack extends BaseStack {
// Public resources for other stacks
public readonly table: ITable
public readonly api: apigateway.RestApi
constructor(scope: Construct, props: {Service}StackProps) {
const prefix = `${props.productName}-${props.stageName}`
super(scope, `${prefix}-{Service}Stack`, props)
// 1. Create API Gateway using ApiConstruct
const {service}Api = new ApiConstruct(this, '{Service}Api', { ...props })
this.api = {service}Api.api
// 2. Create Database Table using DynamoDatabaseTableWithCompositeKey
const { table: {service}Table } = new DynamoDatabaseTableWithCompositeKey(
this,
`${prefix}-{Service}Table`,
{
tableName: `${prefix.toLowerCase()}-{service}`,
partitionKey: { name: 'PK', type: AttributeType.STRING },
sortKey: { name: 'SK', type: AttributeType.STRING },
stream: true,
globalSecondaryIndexes: [
{
indexName: 'GSI1',
partitionKey: { name: 'GSI1PK', type: AttributeType.STRING },
sortKey: { name: 'GSI1SK', type: AttributeType.STRING },
projectionType: ProjectionType.ALL,
},
{
indexName: 'GSI2',
partitionKey: { name: 'GSI2PK', type: AttributeType.STRING },
sortKey: { name: 'GSI2SK', type: AttributeType.STRING },
projectionType: ProjectionType.ALL,
},
],
},
)
this.table = {service}Table
// 3. Common Props with proper environment variables
const commonProps = {
description: prefix,
apiGatewayResource: this.api.root,
apiKeyRequired: true,
env: {
{SERVICE}_TABLE: {service}Table.tableName,
USER_SERVICE_URL: props.userServiceUrl,
USER_SERVICE_API_KEY: getEnvVarOrThrow('USER_SERVICE_API_KEY'),
// Add other environment variables as needed
},
}
// 4. Register ALL handlers from packages/{service}-api/src/lambdas/
// ... handler registrations
// 5. Outputs
new cdk.CfnOutput(this, '{Service}TableName', {
value: {service}Table.tableName,
})
}
}
```
## Stack Organization Principles
### One Stack Per API Service
- **ALWAYS** create a dedicated stack for each API service
- Stack naming convention: `{ServiceName}Stack` (e.g., `BlogStack`, `UsersStack`, `AuthStack`)
- File naming convention: `{service-name}-stack.ts` (e.g., `blog-stack.ts`, `users-stack.ts`)
### Stack Dependencies
- Define clear interfaces between stacks using props
- Pass required URLs and resources through constructor props
- Export public resources that other stacks need
```typescript
// ✅ CORRECT - Stack dependencies through props
type BlogStackProps = {
userServiceUrl: string // Dependency on UsersStack
} & BaseStackProps
// ✅ CORRECT - Export resources for other stacks
export class BlogStack extends BaseStack {
public readonly api: apigateway.RestApi // For FrontendStack
public readonly table: ITable // For monitoring/backup stacks
}
```
## Handler Registration Patterns
### Complete Handler Coverage
- **EVERY** lambda handler in the API package MUST be registered in its stack
- **NO** handlers should be missing from the infrastructure
- Use consistent naming patterns for lambda resources
### Handler Registration Template
```typescript
const {handlerName}Lambda = new ApiGatewayHandlerLambda(
this,
`${prefix}-{service}-{action}`,
{
lambdaName: `${prefix}-{service}-{action}`,
method: '{HTTP_METHOD}',
path: 'v1/{service}/{path}',
entry: path.join(
__dirname,
'../../packages/{service}-api/src/lambdas/{handler-dir}/handler.ts',
),
...commonProps,
},
)
```
### Handler Examples by HTTP Method
#### GET Handlers
```typescript
// Single resource retrieval
const getBlogByIdLambda = new ApiGatewayHandlerLambda(this, `${prefix}-blog-get-by-id`, {
method: 'GET',
path: 'v1/blog/{id}',
entry: path.join(__dirname, '../../packages/blog-api/src/lambdas/get-blog-by-id/handler.ts'),
})
// Collection retrieval
const getAllTagsLambda = new ApiGatewayHandlerLambda(this, `${prefix}-blog-get-all-tags`, {
method: 'GET',
path: 'v1/blog/tags',
entry: path.join(__dirname, '../../packages/blog-api/src/lambdas/get-all-tags/handler.ts'),
})
// Search/Query
const searchBlogPostsLambda = new ApiGatewayHandlerLambda(this, `${prefix}-blog-search`, {
method: 'GET',
path: 'v1/blog/search',
entry: path.join(__dirname, '../../packages/blog-api/src/lambdas/search/handler.ts'),
})
```
#### POST Handlers
```typescript
const createBlogPostLambda = new ApiGatewayHandlerLambda(this, `${prefix}-blog-create`, {
method: 'POST',
path: 'v1/blog',
entry: path.join(__dirname, '../../packages/blog-api/src/lambdas/create-blog/handler.ts'),
})
```
#### PUT Handlers
```typescript
const updateBlogPostLambda = new ApiGatewayHandlerLambda(this, `${prefix}-blog-update`, {
method: 'PUT',
path: 'v1/blog/{id}',
entry: path.join(__dirname, '../../packages/blog-api/src/lambdas/update-blog/handler.ts'),
timeout: cdk.Duration.seconds(30), // For long-running operations
})
```
#### DELETE Handlers
```typescript
const deleteBlogPostLambda = new ApiGatewayHandlerLambda(this, `${prefix}-blog-delete`, {
method: 'DELETE',
path: 'v1/blog/{id}',
entry: path.join(__dirname, '../../packages/blog-api/src/lambdas/delete-blog/handler.ts'),
})
```
## Environment Variables Pattern
### Common Environment Variables
- **ALWAYS** define common environment variables once and reuse
- Include all required service dependencies
- Use `getEnvVarOrThrow` for required environment variables
```typescript
const commonProps = {
description: prefix,
apiGatewayResource: this.api.root,
apiKeyRequired: true,
env: {
// Database
BLOG_TABLE: blogTable.tableName,
// External Services
USER_SERVICE_URL: props.userServiceUrl,
USER_SERVICE_API_KEY: getEnvVarOrThrow('USER_SERVICE_API_KEY'),
// Third-party APIs
ALGOLIA_APP_ID: getEnvVarOrThrow('ALGOLIA_APP_ID'),
ALGOLIA_SEARCH_API_KEY: getEnvVarOrThrow('ALGOLIA_SEARCH_API_KEY'),
ALGOLIA_WRITE_API_KEY: getEnvVarOrThrow('ALGOLIA_WRITE_API_KEY'),
},
}
```
### Handler-Specific Environment Variables
```typescript
// Handler with additional environment variables
const updateBlogPostLambda = new ApiGatewayHandlerLambda(this, `${prefix}-blog-update`, {
// ... other props
env: {
...commonProps.env,
OPENAI_API_KEY: getEnvVarOrThrow('OPENAI_API_KEY'), // Additional for this handler
},
})
```
## Database Permissions Pattern
### Grant Appropriate Permissions
- **READ ONLY** for GET handlers: `blogTable.grantReadData(lambda)`
- **WRITE ONLY** for POST handlers: `blogTable.grantWriteData(lambda)`
- **READ + WRITE** for PUT/PATCH handlers: Both `grantReadData` and `grantWriteData`
- **WRITE ONLY** for DELETE handlers: `blogTable.grantWriteData(lambda)`
```typescript
// ✅ CORRECT - Appropriate permissions per operation
blogTable.grantReadData(getBlogByIdLambda.lambda) // GET - read only
blogTable.grantWriteData(createBlogPostLambda.lambda) // POST - write only
blogTable.grantReadData(updateBlogPostLambda.lambda) // PUT - read first
blogTable.grantWriteData(updateBlogPostLambda.lambda) // PUT - then write
blogTable.grantWriteData(deleteBlogPostLambda.lambda) // DELETE - write only
```
## Special Handler Patterns
### DynamoDB Stream Handlers
```typescript
const dbStreamHandlerLambda = new LambdaFunction(this, `${prefix}-{service}-db-stream-handler`, {
runtime: Runtime.NODEJS_22_X,
code: Code.fromAsset(
path.join(__dirname, '../../packages/{service}-api/dist/lambdas/db-stream-handler'),
),
handler: 'handler.handler',
memorySize: 1024,
deadLetterQueue: dbStreamHandlerDlq,
environment: commonProps.env,
})
// Grant stream permissions
if (table.tableStreamArn) {
dbStreamHandlerLambda.addToRolePolicy(
new iam.PolicyStatement({
actions: [
'dynamodb:GetRecords',
'dynamodb:GetShardIterator',
'dynamodb:DescribeStream',
'dynamodb:ListStreams',
],
resources: [table.tableStreamArn],
}),
)
}
```
### Long-Running Handlers
```typescript
// Handlers that need more time (AI processing, file operations, etc.)
const updateBlogPostLambda = new ApiGatewayHandlerLambda(this, `${prefix}-blog-update`, {
// ... other props
timeout: cdk.Duration.seconds(30), // Increase from default 3 seconds
})
```
## ❌ CRITICAL ANTI-PATTERNS - Never Do These
### Wrong Base Class and Imports
```typescript
// ❌ WRONG - Using cdk.Stack instead of BaseStack
export class CommentStack extends cdk.Stack {
// ❌ WRONG - Using wrong construct imports
import * as dynamodb from 'aws-cdk-lib/aws-dynamodb'
import * as logs from 'aws-cdk-lib/aws-logs'
// ❌ WRONG - Using non-existent constructs
const lambda = new LambdaConstruct(this, 'Lambda', {})
const api = new ApiGatewayV2LambdaConstruct(this, 'Api', {})
// ❌ WRONG - Wrong props interface
export interface CommentStackProps extends CommonStackProps {
userServiceUrl: string
}
```
### Wrong Stack Structure
```typescript
// ❌ WRONG - Not using established constructs
this.table = new dynamodb.Table(this, 'CommentTable', {
tableName: `${props.productName}-Comment-${props.stageName}`,
// ... manual table configuration
})
// ❌ WRONG - Manual GSI addition instead of using construct
this.table.addGlobalSecondaryIndex({
indexName: 'GSI1',
// ... manual GSI configuration
})
// ❌ WRONG - Not using ApiConstruct
this.api = new ApiGatewayV2LambdaConstruct(this, 'CommentApi', {
// ... manual API configuration
})
```
### Wrong Environment Variables
```typescript
// ❌ WRONG - Hardcoded placeholder values
const commonEnvironment = {
COMMENT_TABLE: this.table.tableName,
USER_SERVICE_API_KEY: 'placeholder', // Never use placeholders
ALGOLIA_APP_ID: 'placeholder',
OPENAI_API_KEY: 'placeholder',
}
// ❌ WRONG - Not using getEnvVarOrThrow
USER_SERVICE_API_KEY: process.env.USER_SERVICE_API_KEY || 'default'
```
### Wrong Handler Registration
```typescript
// ❌ WRONG - Using non-existent constructs
const createCommentLambda = new LambdaConstruct(this, 'CreateComment', {
entry: '../packages/comment-api/src/lambdas/create-comment/handler.ts',
environment: commonEnvironment,
})
// ❌ WRONG - Manual route configuration instead of using ApiGatewayHandlerLambda
this.api = new ApiGatewayV2LambdaConstruct(this, 'CommentApi', {
routes: [
{
path: '/v1/comments',
method: 'POST',
lambda: createCommentLambda.lambda,
},
],
})
```
## Stack Validation Checklist
### Before Deploying
- [ ] Stack extends BaseStack (not cdk.Stack)
- [ ] Uses correct imports from './constructs/index.js'
- [ ] Uses ApiConstruct for API Gateway
- [ ] Uses DynamoDatabaseTableWithCompositeKey for tables
- [ ] Uses ApiGatewayHandlerLambda for all handlers
- [ ] All lambda handlers from `packages/{service}-api/src/lambdas/` are registered
- [ ] Each handler has appropriate database permissions
- [ ] Environment variables use getEnvVarOrThrow for required values
- [ ] HTTP methods match the handler's intended operation
- [ ] API paths follow RESTful conventions
- [ ] No placeholder values in environment variables
### Handler Discovery Command
```bash
# List all handlers in an API package
ls packages/{service}-api/src/lambdas/
# Compare with handlers in stack file
grep -o "lambdas/[^/]*/handler.ts" infra/src/{service}-stack.ts
```
## Stack Integration in app.ts
### Required Import and Usage
```typescript
// ✅ CORRECT - Import the stack
import { CommentStack } from './comment-stack.js'
// ✅ CORRECT - Create stack with proper dependencies
const commentStack = new CommentStack(app, {
...commonProps,
userServiceUrl: usersStack.api.url,
})
// ✅ CORRECT - Use stack outputs in dependent stacks
new FrontendStack(app, {
...commonProps,
commentServiceUrl: commentStack.api.url, // Use the API URL
})
```
## Maintenance Guidelines
### When Adding New Handlers
1. Create the handler in `packages/{service}-api/src/lambdas/{handler-name}/`
2. **IMMEDIATELY** add it to the corresponding stack in `infra/src/{service}-stack.ts`
3. Use ApiGatewayHandlerLambda construct (not manual Lambda + API Gateway)
4. Grant appropriate database permissions
5. Test the deployment
### When Modifying Handlers
1. Update the handler code
2. Check if stack configuration needs updates (timeouts, environment variables, permissions)
3. Update stack if needed
### Stack Synchronization
- **NEVER** have handlers in the API package that aren't in the stack
- **NEVER** have handlers in the stack that don't exist in the API package
- Keep the stack as the single source of truth for infrastructure
## Complete Working Example
```typescript
import path from 'node:path'
import { fileURLToPath } from 'node:url'
import * as cdk from 'aws-cdk-lib'
import type * as apigateway from 'aws-cdk-lib/aws-apigateway'
import {
AttributeType,
type ITable,
ProjectionType,
} from 'aws-cdk-lib/aws-dynamodb'
import type { Construct } from 'constructs'
import 'source-map-support/register'
import {
ApiConstruct,
ApiGatewayHandlerLambda,
BaseStack,
type BaseStackProps,
DynamoDatabaseTableWithCompositeKey,
} from './constructs/index.js'
import { getEnvVarOrThrow } from './util/index.js'
const __filename = fileURLToPath(import.meta.url)
const __dirname = path.dirname(__filename)
type CommentStackProps = {
userServiceUrl: string
} & BaseStackProps
export class CommentStack extends BaseStack {
public readonly table: ITable
public readonly api: apigateway.RestApi
constructor(scope: Construct, props: CommentStackProps) {
const prefix = `${props.productName}-${props.stageName}`
super(scope, `${prefix}-CommentStack`, props)
// 1. Create API Gateway
const commentApi = new ApiConstruct(this, 'CommentApi', { ...props })
this.api = commentApi.api
// 2. Create Database Table
const { table: commentTable } = new DynamoDatabaseTableWithCompositeKey(
this,
`${prefix}-CommentTable`,
{
tableName: `${prefix.toLowerCase()}-comment`,
partitionKey: { name: 'PK', type: AttributeType.STRING },
sortKey: { name: 'SK', type: AttributeType.STRING },
stream: true,
globalSecondaryIndexes: [
{
indexName: 'GSI1',
partitionKey: { name: 'GSI1PK', type: AttributeType.STRING },
sortKey: { name: 'GSI1SK', type: AttributeType.STRING },
projectionType: ProjectionType.ALL,
},
{
indexName: 'GSI2',
partitionKey: { name: 'GSI2PK', type: AttributeType.STRING },
sortKey: { name: 'GSI2SK', type: AttributeType.STRING },
projectionType: ProjectionType.ALL,
},
],
},
)
this.table = commentTable
// 3. Common Props
const commonProps = {
description: prefix,
apiGatewayResource: this.api.root,
apiKeyRequired: true,
env: {
COMMENT_TABLE: commentTable.tableName,
USER_SERVICE_URL: props.userServiceUrl,
USER_SERVICE_API_KEY: getEnvVarOrThrow('USER_SERVICE_API_KEY'),
ALGOLIA_APP_ID: getEnvVarOrThrow('ALGOLIA_APP_ID'),
ALGOLIA_SEARCH_API_KEY: getEnvVarOrThrow('ALGOLIA_SEARCH_API_KEY'),
ALGOLIA_WRITE_API_KEY: getEnvVarOrThrow('ALGOLIA_WRITE_API_KEY'),
OPENAI_API_KEY: getEnvVarOrThrow('OPENAI_API_KEY'),
},
}
// 4. Register ALL handlers
const createCommentLambda = new ApiGatewayHandlerLambda(
this,
`${prefix}-comment-create`,
{
lambdaName: `${prefix}-comment-create`,
method: 'POST',
path: 'v1/comments',
entry: path.join(
__dirname,
'../../packages/comment-api/src/lambdas/create-comment/handler.ts',
),
...commonProps,
},
)
commentTable.grantWriteData(createCommentLambda.lambda)
// ... register all other handlers
// 5. Outputs
new cdk.CfnOutput(this, 'CommentTableName', {
value: commentTable.tableName,
})
}
}
```
This rule ensures all stacks follow the established patterns and avoid the common mistakes that lead to compilation errors and infrastructure issues.