-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.ts
More file actions
48 lines (41 loc) · 1.4 KB
/
main.ts
File metadata and controls
48 lines (41 loc) · 1.4 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
import { APIGatewayProxyHandler } from 'aws-lambda';
import { config } from 'dotenv';
import { Client } from 'pg';
config({
path: './.env',
});
// Lambda handler
export const handler: APIGatewayProxyHandler = async (event) => {
const body = event; // Lambda Payload Object
let dbConnection: Client;
try {
// Connect to the source database and fetch data
console.log('Connecting to the database through RDS Proxy');
dbConnection = new Client({
host: process.env.DB_PROXY_ENDPOINT,
port: process.env.DB_PORT,
database: process.env.DB_NAME,
user: process.env.DB_USERNAME,
password: process.env.DB_PASSWORD,
});
await dbConnection.connect();
console.log('Connected to the database through RDS Proxy');
// ADD LOGIC HERE
return {
statusCode: 200,
body: JSON.stringify({ message: 'Lambda function executed successfully.' }),
};
} catch (error) {
console.error('Error processing data:', error);
return {
statusCode: 500,
body: JSON.stringify({ message: 'Error processing data', error: error.message }),
};
} finally {
// Close connections
if (dbConnection) {
await dbConnection.end();
console.log('Database connection closed');
}
}
};