Skip to content
This repository was archived by the owner on Mar 27, 2025. It is now read-only.

Commit 1036d42

Browse files
committed
Refactor Lambda bundling options across stacks to use common configuration for improved consistency and maintainability
1 parent fef27c6 commit 1036d42

File tree

4 files changed

+43
-40
lines changed

4 files changed

+43
-40
lines changed

lib/auth-stack.ts

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,13 @@ export class AuthStack extends cdk.Stack {
1919

2020
const { envName } = props;
2121

22+
// Common bundling options for all Lambda functions
23+
const commonBundlingOptions = {
24+
minify: envName === 'prod',
25+
sourceMap: envName !== 'prod', // Enable source maps in non-prod environments
26+
externalModules: ['aws-sdk']
27+
};
28+
2229
const httpApiId = cdk.Fn.importValue(getResourceName(envName, 'BeejhoHttpApiId'));
2330
const httpApi = HttpApi.fromHttpApiAttributes(this, getResourceName(envName, 'BeejhoHttpApiId'), {
2431
httpApiId
@@ -38,9 +45,7 @@ export class AuthStack extends cdk.Stack {
3845
ENV_NAME: envName,
3946
},
4047
logRetention: RetentionDays.ONE_DAY,
41-
bundling: {
42-
externalModules: ['aws-sdk']
43-
},
48+
bundling: commonBundlingOptions,
4449
});
4550
userTable.grantReadWriteData(userLoginLambda);
4651

@@ -61,9 +66,7 @@ export class AuthStack extends cdk.Stack {
6166
ENV_NAME: envName,
6267
},
6368
logRetention: RetentionDays.ONE_DAY,
64-
bundling: {
65-
externalModules: ['aws-sdk']
66-
},
69+
bundling: commonBundlingOptions,
6770
});
6871

6972
new HttpRoute(this, getResourceName(envName, 'GenerateOTPRoute'), {
@@ -84,9 +87,7 @@ export class AuthStack extends cdk.Stack {
8487
ENV_NAME: envName,
8588
},
8689
logRetention: RetentionDays.ONE_DAY,
87-
bundling: {
88-
externalModules: ['aws-sdk']
89-
},
90+
bundling: commonBundlingOptions,
9091
});
9192

9293
const userAuthorizer = new HttpAuthorizer(this, getResourceName(envName, 'UserAuthorizer'), {
@@ -118,9 +119,7 @@ export class AuthStack extends cdk.Stack {
118119
functionName: getResourceName(envName, 'RestaurantLogin'),
119120
architecture: lambda.Architecture.ARM_64,
120121
logRetention: RetentionDays.ONE_DAY,
121-
bundling: {
122-
externalModules: ['aws-sdk']
123-
},
122+
bundling: commonBundlingOptions,
124123
environment: {
125124
JWT_SECRET: 'Abhi2199@321#',
126125
ENV_NAME: envName,
@@ -146,9 +145,7 @@ export class AuthStack extends cdk.Stack {
146145
ENV_NAME: envName,
147146
},
148147
logRetention: RetentionDays.ONE_DAY,
149-
bundling: {
150-
externalModules: ['aws-sdk']
151-
},
148+
bundling: commonBundlingOptions,
152149
});
153150

154151

lib/order-stack.ts

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,13 @@ export class OrderStack extends cdk.Stack {
4141
authorizerType: 'CUSTOM'
4242
})
4343

44+
// Common bundling options for all Lambda functions
45+
const commonBundlingOptions = {
46+
minify: envName === 'prod',
47+
sourceMap: envName !== 'prod', // Enable source maps in non-prod environments
48+
externalModules: ['aws-sdk']
49+
};
50+
4451
const postOrderLambda = new NodejsFunction(this, getResourceName(envName, 'CreateOrder'), {
4552
runtime: lambda.Runtime.NODEJS_LATEST,
4653
entry: './src/lambdas/orders.ts',
@@ -51,9 +58,7 @@ export class OrderStack extends cdk.Stack {
5158
environment: {
5259
ENV_NAME: envName,
5360
},
54-
bundling: {
55-
externalModules: ['aws-sdk']
56-
},
61+
bundling: commonBundlingOptions,
5762
});
5863
ordersTable.grantWriteData(postOrderLambda);
5964
menuTable.grantReadData(postOrderLambda);
@@ -75,9 +80,7 @@ export class OrderStack extends cdk.Stack {
7580
environment: {
7681
ENV_NAME: envName,
7782
},
78-
bundling: {
79-
externalModules: ['aws-sdk']
80-
},
83+
bundling: commonBundlingOptions,
8184
});
8285
ordersTableWithRestaurantIDResourceIdIndex.grantReadData(getOrderLambda);
8386
restaurantTable.grantReadData(getOrderLambda);
@@ -95,12 +98,11 @@ export class OrderStack extends cdk.Stack {
9598
handler: 'updateOrderHandler',
9699
functionName: getResourceName(envName, 'updateOrder'),
97100
architecture: lambda.Architecture.ARM_64,
98-
logRetention: RetentionDays.ONE_DAY, environment: {
101+
logRetention: RetentionDays.ONE_DAY,
102+
environment: {
99103
ENV_NAME: envName,
100104
},
101-
bundling: {
102-
externalModules: ['aws-sdk']
103-
},
105+
bundling: commonBundlingOptions,
104106
});
105107
ordersTable.grantReadWriteData(updateOrderLambda);
106108

lib/restaurant-stack.ts

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,13 @@ export class RestaurantStack extends cdk.Stack {
3232
authorizerType: 'CUSTOM'
3333
})
3434

35+
// Common bundling options for all Lambda functions
36+
const commonBundlingOptions = {
37+
minify: envName === 'prod',
38+
sourceMap: envName !== 'prod', // Enable source maps in non-prod environments
39+
externalModules: ['aws-sdk']
40+
};
41+
3542
const getMenuLambda = new NodejsFunction(this, getResourceName(envName, 'GetMenu'), {
3643
runtime: lambda.Runtime.NODEJS_LATEST,
3744
entry: './src/lambdas/restaurant.ts',
@@ -42,9 +49,7 @@ export class RestaurantStack extends cdk.Stack {
4249
environment: {
4350
ENV_NAME: envName,
4451
},
45-
bundling: {
46-
externalModules: ['aws-sdk']
47-
},
52+
bundling: commonBundlingOptions,
4853
});
4954
menuTable.grantReadData(getMenuLambda);
5055
restaurantTable.grantReadData(getMenuLambda);
@@ -65,9 +70,7 @@ export class RestaurantStack extends cdk.Stack {
6570
environment: {
6671
ENV_NAME: envName,
6772
},
68-
bundling: {
69-
externalModules: ['aws-sdk']
70-
},
73+
bundling: commonBundlingOptions,
7174
});
7275
menuTable.grantWriteData(postMenuItemLambda);
7376

@@ -88,9 +91,7 @@ export class RestaurantStack extends cdk.Stack {
8891
environment: {
8992
ENV_NAME: envName,
9093
},
91-
bundling: {
92-
externalModules: ['aws-sdk']
93-
},
94+
bundling: commonBundlingOptions,
9495
});
9596
menuTable.grantWriteData(deleteMenuItemLambda);
9697

@@ -111,9 +112,7 @@ export class RestaurantStack extends cdk.Stack {
111112
environment: {
112113
ENV_NAME: envName,
113114
},
114-
bundling: {
115-
externalModules: ['aws-sdk']
116-
},
115+
bundling: commonBundlingOptions,
117116
});
118117
restaurantTable.grantReadData(getRestaurantOrderDetailsLambda);
119118
ordersTable.grantReadData(getRestaurantOrderDetailsLambda);

lib/s3-stack.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,13 @@ export class S3Stack extends cdk.Stack {
4646
],
4747
});
4848

49+
// Common bundling options for all Lambda functions
50+
const commonBundlingOptions = {
51+
minify: envName === 'prod',
52+
sourceMap: envName !== 'prod', // Enable source maps in non-prod environments
53+
externalModules: ['aws-sdk']
54+
};
55+
4956
const getPreSignedURLLambda = new NodejsFunction(this, getResourceName(envName, 'PreSignedS3URL'), {
5057
runtime: lambda.Runtime.NODEJS_LATEST,
5158
entry: './src/lambdas/s3.ts',
@@ -56,9 +63,7 @@ export class S3Stack extends cdk.Stack {
5663
environment: {
5764
ENV_NAME: envName,
5865
},
59-
bundling: {
60-
externalModules: ['aws-sdk']
61-
},
66+
bundling: commonBundlingOptions,
6267
});
6368

6469
getPreSignedURLLambda.addToRolePolicy(

0 commit comments

Comments
 (0)