@@ -6,7 +6,6 @@ import * as lambda from 'aws-cdk-lib/aws-lambda';
66import * as iam from 'aws-cdk-lib/aws-iam' ;
77import * as cr from 'aws-cdk-lib/custom-resources' ;
88import * as path from 'path' ;
9- import * as nodejs from 'aws-cdk-lib/aws-lambda-nodejs' ;
109
1110export class PostgresLambdaStack extends cdk . Stack {
1211 constructor ( scope : Construct , id : string , props ?: cdk . StackProps ) {
@@ -31,10 +30,24 @@ export class PostgresLambdaStack extends cdk.Stack {
3130 credentials : rds . Credentials . fromGeneratedSecret ( 'postgres' ) ,
3231 } ) ;
3332
34- // Create a Lambda function that calls PostgreSQL using NodejsFunction
35- const lambdaToPostgres = new nodejs . NodejsFunction ( this , 'LambdaToPostgres' , {
33+ // Create a Lambda function that calls PostgreSQL with Docker bundling
34+ const lambdaToPostgres = new lambda . Function ( this , 'LambdaToPostgres' , {
3635 runtime : lambda . Runtime . NODEJS_LATEST ,
37- entry : path . join ( __dirname , '../lambda/lambda-to-postgres/index.js' ) ,
36+ handler : 'index.handler' ,
37+ code : lambda . Code . fromAsset ( path . join ( __dirname , '../lambda/lambda-to-postgres' ) , {
38+ bundling : {
39+ image : cdk . DockerImage . fromRegistry ( 'public.ecr.aws/sam/build-nodejs18.x' ) ,
40+ command : [
41+ 'bash' , '-c' , [
42+ 'cp -r . /tmp' ,
43+ 'cd /tmp' ,
44+ 'npm init -y' ,
45+ 'npm install pg' ,
46+ 'cp -r . /asset-output/'
47+ ] . join ( ' && ' )
48+ ] ,
49+ } ,
50+ } ) ,
3851 vpc,
3952 vpcSubnets : {
4053 subnetType : ec2 . SubnetType . PRIVATE_WITH_EGRESS ,
@@ -43,11 +56,6 @@ export class PostgresLambdaStack extends cdk.Stack {
4356 DB_SECRET_ARN : dbCluster . secret ?. secretArn || '' ,
4457 DB_NAME : 'demodb' ,
4558 } ,
46- bundling : {
47- externalModules : [
48- 'aws-sdk' , // Use the AWS SDK available in the Lambda runtime
49- ] ,
50- } ,
5159 timeout : cdk . Duration . seconds ( 30 ) ,
5260 } ) ;
5361
@@ -57,28 +65,26 @@ export class PostgresLambdaStack extends cdk.Stack {
5765 // Grant the Lambda function permission to read the database secret
5866 dbCluster . secret ?. grantRead ( lambdaToPostgres ) ;
5967
60- // Create a Lambda function that is called by PostgreSQL using NodejsFunction
61- const postgresFunction = new nodejs . NodejsFunction ( this , 'PostgresFunction' , {
68+ // Create a Lambda function that is called by PostgreSQL
69+ const postgresFunction = new lambda . Function ( this , 'PostgresFunction' , {
6270 runtime : lambda . Runtime . NODEJS_LATEST ,
63- entry : path . join ( __dirname , '../lambda/postgres-to-lambda/index.js' ) ,
71+ handler : 'index.handler' ,
72+ code : lambda . Code . fromAsset ( path . join ( __dirname , '../lambda/postgres-to-lambda' ) ) ,
6473 environment : {
6574 FUNCTION_NAME : 'PostgresFunction' ,
6675 } ,
67- bundling : {
68- externalModules : [
69- 'aws-sdk' , // Use the AWS SDK available in the Lambda runtime
70- ] ,
71- } ,
7276 timeout : cdk . Duration . seconds ( 30 ) ,
7377 } ) ;
7478
79+
7580 // Create a role for PostgreSQL to assume to invoke Lambda
7681 const postgresLambdaRole = new iam . Role ( this , 'PostgresLambdaRole' , {
7782 assumedBy : new iam . ServicePrincipal ( 'rds.amazonaws.com' ) ,
7883 } ) ;
7984
8085 postgresFunction . grantInvoke ( postgresLambdaRole ) ;
8186
87+
8288 const l1DbCluster = dbCluster . node . defaultChild as rds . CfnDBCluster
8389 const exisitingProperty = ( l1DbCluster . associatedRoles as [ ] ) || [ ] ;
8490 console . log ( exisitingProperty ) ;
@@ -92,10 +98,24 @@ export class PostgresLambdaStack extends cdk.Stack {
9298
9399 l1DbCluster . addPropertyOverride ( 'AssociatedRoles' , updatedRoles ) ;
94100
95- // Create Lambda function for PostgreSQL setup using NodejsFunction
96- const setupFunction = new nodejs . NodejsFunction ( this , 'PostgresSetupFunction' , {
101+ // Create Lambda function for PostgreSQL setup with Docker bundling
102+ const setupFunction = new lambda . Function ( this , 'PostgresSetupFunction' , {
97103 runtime : lambda . Runtime . NODEJS_LATEST ,
98- entry : path . join ( __dirname , '../lambda/postgres-setup/index.js' ) ,
104+ handler : 'index.handler' ,
105+ code : lambda . Code . fromAsset ( path . join ( __dirname , '../lambda/postgres-setup' ) , {
106+ bundling : {
107+ image : cdk . DockerImage . fromRegistry ( 'public.ecr.aws/sam/build-nodejs18.x' ) ,
108+ command : [
109+ 'bash' , '-c' , [
110+ 'cp -r . /tmp' ,
111+ 'cd /tmp' ,
112+ 'npm init -y' ,
113+ 'npm install pg' ,
114+ 'cp -r . /asset-output/'
115+ ] . join ( ' && ' )
116+ ] ,
117+ } ,
118+ } ) ,
99119 vpc,
100120 vpcSubnets : {
101121 subnetType : ec2 . SubnetType . PRIVATE_WITH_EGRESS ,
@@ -105,11 +125,6 @@ export class PostgresLambdaStack extends cdk.Stack {
105125 DB_NAME : 'demodb' ,
106126 POSTGRES_FUNCTION_NAME : postgresFunction . functionName ,
107127 } ,
108- bundling : {
109- externalModules : [
110- 'aws-sdk' , // Use the AWS SDK available in the Lambda runtime
111- ] ,
112- } ,
113128 timeout : cdk . Duration . minutes ( 5 ) ,
114129 } ) ;
115130
0 commit comments