Skip to content

Commit ce066a6

Browse files
committed
Fix Lambda bundling by using NodejsFunction
1 parent 929750a commit ce066a6

File tree

1 file changed

+25
-14
lines changed

1 file changed

+25
-14
lines changed

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

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import * as lambda from 'aws-cdk-lib/aws-lambda';
66
import * as iam from 'aws-cdk-lib/aws-iam';
77
import * as cr from 'aws-cdk-lib/custom-resources';
88
import * as path from 'path';
9+
import * as nodejs from 'aws-cdk-lib/aws-lambda-nodejs';
910

1011
export class PostgresLambdaStack extends cdk.Stack {
1112
constructor(scope: Construct, id: string, props?: cdk.StackProps) {
@@ -30,11 +31,10 @@ export class PostgresLambdaStack extends cdk.Stack {
3031
credentials: rds.Credentials.fromGeneratedSecret('postgres'),
3132
});
3233

33-
// Create a Lambda function that calls PostgreSQL
34-
const lambdaToPostgres = new lambda.Function(this, 'LambdaToPostgres', {
34+
// Create a Lambda function that calls PostgreSQL using NodejsFunction
35+
const lambdaToPostgres = new nodejs.NodejsFunction(this, 'LambdaToPostgres', {
3536
runtime: lambda.Runtime.NODEJS_LATEST,
36-
handler: 'index.handler',
37-
code: lambda.Code.fromAsset(path.join(__dirname, '../lambda/lambda-to-postgres')),
37+
entry: path.join(__dirname, '../lambda/lambda-to-postgres/index.js'),
3838
vpc,
3939
vpcSubnets: {
4040
subnetType: ec2.SubnetType.PRIVATE_WITH_EGRESS,
@@ -43,6 +43,11 @@ export class PostgresLambdaStack extends cdk.Stack {
4343
DB_SECRET_ARN: dbCluster.secret?.secretArn || '',
4444
DB_NAME: 'demodb',
4545
},
46+
bundling: {
47+
externalModules: [
48+
'aws-sdk', // Use the AWS SDK available in the Lambda runtime
49+
],
50+
},
4651
timeout: cdk.Duration.seconds(30),
4752
});
4853

@@ -52,26 +57,28 @@ export class PostgresLambdaStack extends cdk.Stack {
5257
// Grant the Lambda function permission to read the database secret
5358
dbCluster.secret?.grantRead(lambdaToPostgres);
5459

55-
// Create a Lambda function that is called by PostgreSQL
56-
const postgresFunction = new lambda.Function(this, 'PostgresFunction', {
60+
// Create a Lambda function that is called by PostgreSQL using NodejsFunction
61+
const postgresFunction = new nodejs.NodejsFunction(this, 'PostgresFunction', {
5762
runtime: lambda.Runtime.NODEJS_LATEST,
58-
handler: 'index.handler',
59-
code: lambda.Code.fromAsset(path.join(__dirname, '../lambda/postgres-to-lambda')),
63+
entry: path.join(__dirname, '../lambda/postgres-to-lambda/index.js'),
6064
environment: {
6165
FUNCTION_NAME: 'PostgresFunction',
6266
},
67+
bundling: {
68+
externalModules: [
69+
'aws-sdk', // Use the AWS SDK available in the Lambda runtime
70+
],
71+
},
6372
timeout: cdk.Duration.seconds(30),
6473
});
6574

66-
6775
// Create a role for PostgreSQL to assume to invoke Lambda
6876
const postgresLambdaRole = new iam.Role(this, 'PostgresLambdaRole', {
6977
assumedBy: new iam.ServicePrincipal('rds.amazonaws.com'),
7078
});
7179

7280
postgresFunction.grantInvoke(postgresLambdaRole);
7381

74-
7582
const l1DbCluster = dbCluster.node.defaultChild as rds.CfnDBCluster
7683
const exisitingProperty = (l1DbCluster.associatedRoles as []) || [];
7784
console.log(exisitingProperty);
@@ -85,11 +92,10 @@ export class PostgresLambdaStack extends cdk.Stack {
8592

8693
l1DbCluster.addPropertyOverride('AssociatedRoles', updatedRoles);
8794

88-
// Create Lambda function for PostgreSQL setup
89-
const setupFunction = new lambda.Function(this, 'PostgresSetupFunction', {
95+
// Create Lambda function for PostgreSQL setup using NodejsFunction
96+
const setupFunction = new nodejs.NodejsFunction(this, 'PostgresSetupFunction', {
9097
runtime: lambda.Runtime.NODEJS_LATEST,
91-
handler: 'index.handler',
92-
code: lambda.Code.fromAsset(path.join(__dirname, '../lambda/postgres-setup')),
98+
entry: path.join(__dirname, '../lambda/postgres-setup/index.js'),
9399
vpc,
94100
vpcSubnets: {
95101
subnetType: ec2.SubnetType.PRIVATE_WITH_EGRESS,
@@ -99,6 +105,11 @@ export class PostgresLambdaStack extends cdk.Stack {
99105
DB_NAME: 'demodb',
100106
POSTGRES_FUNCTION_NAME: postgresFunction.functionName,
101107
},
108+
bundling: {
109+
externalModules: [
110+
'aws-sdk', // Use the AWS SDK available in the Lambda runtime
111+
],
112+
},
102113
timeout: cdk.Duration.minutes(5),
103114
});
104115

0 commit comments

Comments
 (0)