Hi every one, I started learning GCP one and half months ago and I’m blocking in a basic task like insert data in a cloud sql instance. I’m trying it throgh cloud functions like this:
const mysql = require(‘mysql2/promise’);
let pool;
exports.completePayment = async (req, res) => {
res.set(‘Access-Control-Allow-Origin’, ‘*’);
res.set(‘Access-Control-Allow-Methods’, ‘POST’);
res.set(‘Access-Control-Allow-Headers’, ‘Content-Type’);
if (req.method === ‘OPTIONS’) {
res.status(204).send(‘’);
return;
}
const { menu, price, cardNumber } = req.body;
if (!menu || !price || !cardNumber) {
res.status(400).send(‘Missing data’);
return;
}
const priceDecimal = parseFloat(price);
if (isNaN(priceDecimal)) {
res.status(400).send(‘Invalid price’);
return;
}
if (!pool) {
try {
pool = await mysql.createPool({
user: process.env.DB_USER,
password: process.env.DB_PASS,
database: process.env.DB_NAME,
host: ‘34.175.115.163’,
port: 3306,
connectTimeout: 10000
});
console.log(‘Database pool created successfully’);
} catch (error) {
console.error(‘Error creating database pool:’, error);
res.status(500).send(‘Error connecting to the database’);
return;
}
}
//cnx test
try {
const connection = await pool.getConnection();
console.log(‘Database connection successful’);
connection.release();
} catch (error) {
console.error(‘Error connecting to the database:’, error.message);
res.status(500).send('Error connecting to the database: ’ + error.message);
return;
}
console.log(‘Inserting data:’, { menu, price: priceDecimal, cardNumber });
try {
const query = INSERT INTO transactions (transaction_date, menu, price, card_number) VALUES (CURDATE(), ?, ?, ?);
const [result] = await pool.query(query, [menu, priceDecimal, cardNumber]);
console.log(‘Query result:’, result);
res.status(200).send(‘Payment successful’);
} catch (error) {
console.error(‘Error executing query:’, error.message);
res.status(500).send('Error executing query: ’ + error.message);
}
};
At the cloud sql instance, I got next instance:
It has a public IP and even I’ve created a firewall rule in order to allo traffic between cloud function and cloud sql instance:
hoever I’m getting next error in executing app: no coneccting to data base:
2024-07-19 18:37:16.543 CEST
POST500285 B10.1 sChrome 126 https://europe-southwest1-app-menu-427017.cloudfunctions.net/completePayment
2024-07-19 18:37:16.598 CEST
Database pool created successfully
2024-07-19 18:37:26.601 CEST
2024-07-19 18:37:16.543 CEST
POST500285 B10.1 sChrome 126 https://europe-southwest1-app-menu-427017.cloudfunctions.net/completePayment
2024-07-19 18:37:16.598 CEST
Database pool created successfully
2024-07-19 18:37:26.601 CEST
Error connecting to the database: connect ETIMEDOUT
Any suggestion - idea about the error origin? Thanks a lot

