Skip to content

Commit 2fab975

Browse files
committed
fix: replace fetch with native https module in postgres-setup Lambda function
1 parent f4e657b commit 2fab975

File tree

2 files changed

+50
-20
lines changed

2 files changed

+50
-20
lines changed

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

Lines changed: 45 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
const { Client } = require('pg');
22
const { SecretsManagerClient, GetSecretValueCommand } = require('@aws-sdk/client-secrets-manager');
3+
const https = require('https');
4+
const url = require('url');
35

46
const secretsManager = new SecretsManagerClient();
57

@@ -74,24 +76,48 @@ exports.handler = async (event) => {
7476
}
7577
};
7678

77-
async function sendResponse(event, status, reason) {
78-
const response = {
79-
Status: status,
80-
Reason: reason,
81-
PhysicalResourceId: 'postgres-setup-' + Date.now(),
82-
StackId: event.StackId,
83-
RequestId: event.RequestId,
84-
LogicalResourceId: event.LogicalResourceId
85-
};
86-
87-
console.log('Response:', JSON.stringify(response, null, 2));
88-
89-
const fetch = (await import('node-fetch')).default;
90-
await fetch(event.ResponseURL, {
91-
method: 'PUT',
92-
headers: { 'Content-Type': '' },
93-
body: JSON.stringify(response)
94-
});
79+
function sendResponse(event, status, reason) {
80+
return new Promise((resolve, reject) => {
81+
const responseBody = JSON.stringify({
82+
Status: status,
83+
Reason: reason,
84+
PhysicalResourceId: 'postgres-setup-' + Date.now(),
85+
StackId: event.StackId,
86+
RequestId: event.RequestId,
87+
LogicalResourceId: event.LogicalResourceId,
88+
Data: {}
89+
});
90+
91+
console.log('Response:', responseBody);
92+
93+
// Parse the URL
94+
const parsedUrl = url.parse(event.ResponseURL);
95+
96+
// Prepare the request options
97+
const options = {
98+
hostname: parsedUrl.hostname,
99+
port: 443,
100+
path: parsedUrl.path,
101+
method: 'PUT',
102+
headers: {
103+
'Content-Type': '',
104+
'Content-Length': responseBody.length
105+
}
106+
};
107+
108+
// Send the response
109+
const request = https.request(options, (response) => {
110+
console.log(`Status code: ${response.statusCode}`);
111+
resolve({ status, reason });
112+
});
95113

96-
return response;
114+
request.on('error', (error) => {
115+
console.error('Error sending response:', error);
116+
reject(error);
117+
});
118+
119+
// Write the response body and end the request
120+
request.write(responseBody);
121+
request.end();
122+
});
97123
}

typescript/postgres-lambda/lambda/postgres-setup/package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@
33
"version": "1.0.0",
44
"description": "Lambda function for PostgreSQL setup",
55
"main": "index.js",
6+
"scripts": {
7+
"build": "mkdir -p node_modules && npm install --no-package-lock"
8+
},
69
"dependencies": {
7-
"pg": "^8.11.0"
10+
"pg": "^8.11.0",
11+
"@aws-sdk/client-secrets-manager": "^3.350.0"
812
}
913
}

0 commit comments

Comments
 (0)