Skip to content

Commit b357791

Browse files
committed
Improve PostgreSQL Lambda integration
- Update SQL functions to use JSONB casting instead of text for proper JSON handling - Change Lambda invocation type from 'Event' to 'RequestResponse' for synchronous execution - Add parameter group to RDS cluster for custom configuration - Update Aurora PostgreSQL version to 17.4 - Fix code formatting and whitespace in Lambda functions
1 parent 9afb6bf commit b357791

File tree

3 files changed

+26
-16
lines changed

3 files changed

+26
-16
lines changed

typescript/postgres-lambda/lambda/postgres-setup/index.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,26 +40,26 @@ exports.handler = async (event, context) => {
4040
RETURNS JSONB AS $$
4141
SELECT payload FROM aws_lambda.invoke(
4242
aws_commons.create_lambda_function_arn('${POSTGRES_FUNCTION_NAME}', '${AWS_REGION}'),
43-
json_build_object('action', 'process', 'data', data)::text,
44-
'Event'
43+
json_build_object('action', 'process', 'data', data)::JSONB,
44+
'RequestResponse'
4545
);
4646
$$ LANGUAGE SQL;
4747
4848
CREATE OR REPLACE FUNCTION transform_data(data JSONB)
4949
RETURNS JSONB AS $$
5050
SELECT payload FROM aws_lambda.invoke(
5151
aws_commons.create_lambda_function_arn('${POSTGRES_FUNCTION_NAME}', '${AWS_REGION}'),
52-
json_build_object('action', 'transform', 'data', data)::text,
53-
'Event'
52+
json_build_object('action', 'transform', 'data', data)::JSONB,
53+
'RequestResponse'
5454
);
5555
$$ LANGUAGE SQL;
5656
5757
CREATE OR REPLACE FUNCTION validate_data(data JSONB)
5858
RETURNS JSONB AS $$
5959
SELECT payload FROM aws_lambda.invoke(
6060
aws_commons.create_lambda_function_arn('${POSTGRES_FUNCTION_NAME}', '${AWS_REGION}'),
61-
json_build_object('action', 'validate', 'data', data)::text,
62-
'Event'
61+
json_build_object('action', 'validate', 'data', data)::JSONB,
62+
'RequestResponse'
6363
);
6464
$$ LANGUAGE SQL;
6565
`;

typescript/postgres-lambda/lambda/postgres-to-lambda/index.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
/**
22
* Lambda function that is called by PostgreSQL
3-
*
3+
*
44
* This function can be invoked from PostgreSQL using the aws_lambda extension
55
* Example SQL:
6-
*
6+
*
77
* SELECT * FROM aws_lambda.invoke(
88
* aws_commons.create_lambda_function_arn('PostgresFunction', 'us-east-1'),
99
* '{"action": "process", "data": {"id": 123, "value": "test"}}',
@@ -12,14 +12,14 @@
1212
*/
1313
exports.handler = async (event) => {
1414
console.log('Event received from PostgreSQL:', JSON.stringify(event));
15-
15+
1616
try {
1717
// Process the event data
1818
const action = event.action || 'default';
1919
const data = event.data || {};
20-
20+
2121
let result;
22-
22+
2323
// Perform different actions based on the event
2424
switch (action) {
2525
case 'process':
@@ -34,7 +34,7 @@ exports.handler = async (event) => {
3434
default:
3535
result = { status: 'success', message: 'Default action performed', data };
3636
}
37-
37+
3838
return {
3939
statusCode: 200,
4040
body: result,

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

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@ export class PostgresLambdaStack extends cdk.Stack {
1616
maxAzs: 2,
1717
natGateways: 1,
1818
});
19+
const parameterGroup = new rds.ParameterGroup(this, 'PGClusterParamGroup', {
20+
engine: rds.DatabaseClusterEngine.auroraPostgres({
21+
version: rds.AuroraPostgresEngineVersion.VER_17_4
22+
}),
23+
parameters: {
24+
},
25+
});
1926

2027
// Create a PostgreSQL Aurora Serverless v2 cluster
2128
const dbCluster = new rds.DatabaseCluster(this, 'PostgresCluster', {
@@ -28,13 +35,16 @@ export class PostgresLambdaStack extends cdk.Stack {
2835
serverlessV2MaxCapacity: 1,
2936
defaultDatabaseName: 'demodb',
3037
credentials: rds.Credentials.fromGeneratedSecret('postgres'),
38+
parameterGroup: parameterGroup,
3139
});
3240

41+
42+
3343
const bundleCommand = [
34-
'bash', '-c', [
35-
'cp -r . /asset-output/',
36-
].join(' && ')
37-
]
44+
'bash', '-c', [
45+
'cp -r . /asset-output/',
46+
].join(' && ')
47+
]
3848

3949
// Create a Lambda function that calls PostgreSQL with Docker bundling
4050
const lambdaToPostgres = new lambda.Function(this, 'LambdaToPostgres', {

0 commit comments

Comments
 (0)