0

I've made a simple Azure function that when executed will run an insert query to a MySQL database. The MySQL database lives on a HostGator server (shared plan). The Azure function I wrote is able to insert to the DB table as expected when running locally, but after I deploy to Azure the function appears to run fine, but then no new records show in the database table.

Below is the function code:

const mysql = require('mysql');

module.exports = async function (context, req) {
    var connection = mysql.createConnection({
        host: '*****************',
        user: '*************',
        password: '*************',
        database: '***************'
    });

    connection.connect();

    const insertQuery = `INSERT into emails (email_address, first_name, last_name, preferred_game, date_joined) VALUES ('[email protected]', 'Bob', 'Jones', 'both', NOW());`;
    await connection.query(insertQuery, function (error, results, fields) {
        if (error) throw error;
      });

    connection.end();
    context.res = {
        // status: 200, /* Defaults to 200 */
        body: '200'
    };
}

The strange thing is that I don't see any errors in the logs anywhere, so it appears to work, but when I look in the DB no new records have been inserted. I thought at first it might be an IP whitelist issue, but after adding the Azure Function App IP it still failed to insert a record to the DB (Virtual IP as shown under app properties in the Azure portal).

I can't think of why else this wouldn't be able to insert records to the DB from the deployed Azure function.

3
  • I'd suggest adding context.log to trace if the function is actually running firstly and then if it is and the connection is a problem, there'll be an error message that may help Commented Jan 13, 2023 at 6:10
  • Hello John! How did you deploy the function, was it a pure Zip-deploy or did you run it through a pipeline? What I'm thinking about is the package mysql and if it is installed or not. Commented Jan 13, 2023 at 6:10
  • I do have some context.logs in there, I just took them out when I pasted the code here. I think it was a zip deploy. I deployed it from VS Code using the Azure add-on for functions. Commented Jan 13, 2023 at 6:11

1 Answer 1

3

What you've whitelisted, sounds like the "Inbound" IP address. It may be because you haven't whitelisted your "outbound" IP address(es).

Get the outbound IPs with az CLI

az functionapp show --resource-group <GROUP_NAME> --name <APP_NAME> --query outboundIpAddresses --output tsv

Or get the outbound IPs in the portal properties (where you found Virtual IP).

enter image description here

See here for more details

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.