# Azure Functions with Consumption Plan on Linux

- URL: https://baeke.info/2019/04/16/azure-functions-with-consumption-plan-on-linux/
- Author: Geert Baeke
- Published: April 16, 2019 (updated March 12, 2025)
- Topics: iot, azure, functions, iot, postgress, serverless, timescaledb, timeseries

In a [previous post](https://baeke.info/2019/04/15/iot-with-azure-database-for-postgresql-and-timescaledb/), I talked about saving time-series data to TimescaleDB, which is an extension on top of PostgreSQL. The post used an Azure Function with an Event Hub trigger to save the data in TimescaleDB with a regular INSERT INTO statement.

The Function App used the Windows runtime which gave me networking errors (ECONNRESET) when connecting to PostgreSQL. I often encounter those issues with the Windows runtime. In general, for Node.js, I try to stick to the Linux runtime whenever possible. In this post, we will try the same code but with a Function App that uses the Linux runtime in a Consumption Plan.

Make sure [Azure CLI](https://docs.microsoft.com/en-us/cli/azure/install-azure-cli?view=azure-cli-latest) is installed and that you are logged in. First, create a Storage Account:

```bash
az storage account create --name gebafuncstore --location westeurope --resource-group funclinux --sku Standard_LRS
```

Next, create the Function App. It references the storage account you created above:

```bash
az functionapp create --resource-group funclinux --name funclinux --os-type Linux --runtime node --consumption-plan-location westeurope --storage-account gebafuncstore
```

You can also use a script to achieve the same results. For an example, see  
[https://docs.microsoft.com/en-us/azure/azure-functions/scripts/functions-cli-create-serverless.](https://docs.microsoft.com/en-us/azure/azure-functions/scripts/functions-cli-create-serverless)

Now, in the Function App, set the following **Application Settings**. These settings will be used in the code we will deploy later.

-   host: hostname of the PostgreSQL server (e.g. servername.postgres.database.azure.com)
-   user: user name (e.g. user@servername)
-   password
-   database: name of the PostgreSQL database
-   EH: connection string to the Event Hub interface of your IoT Hub; if your are unsure how to set this, see [this post](https://technology.amis.nl/2017/08/31/serverless-computing-with-azure-functions-interaction-with-event-hub/)

You can set the above values from the Azure Portal:

![](https://baeke.info/_astro/2019-04-image-26.xi6KAhgd_JdeSV.webp)

*Application Settings of the Function App*

The function uses the first four Application Settings in the function code via process.env:

![](https://baeke.info/_astro/2019-04-image-22.CTxzgtEQ_ZoJ4OW.webp)

*Using Application Settings in JavaScript*

The application setting EH is used to reference the Event Hub in function.json:

![](https://baeke.info/_astro/2019-04-image-23.X4wX3y9p_Z1zo6Sk.webp)

*function.json with Event Hub details such as the connection, cardinality and the consumerGroup*

Now let’s get the code from my [GitHub repo](https://github.com/gbaeke/pgfunc) in the Azure Function. First install [Azure Function Core Tools 2.x](https://docs.microsoft.com/en-us/azure/azure-functions/functions-run-local#v2). Next, create a folder called funcdemo. In that folder, run the following commands:

```bash
git clone https://github.com/gbaeke/pgfunc.git
cd pgfunc
npm install
az login
az account show
```

The **npm install** command installs the pg module as defined in package.json. The last two commands log you in and show the active subscription. Make sure that subscription contains the Function App you deployed above. Now run the following command:

```bash
func init
```

Answer the questions: we use Node and JavaScript. You should now have a local.settings.json file that sets the FUNCTIONS\_WORKER\_RUNTIME to node. If you do not have that, the next command will throw an error.

Now issue the following command to package and deploy the function to the Function App we created earlier:

```bash
func azure functionapp publish funclinux
```

This should result in the following feedback:

![](https://baeke.info/_astro/2019-04-image-24.BW3m2HqV_1evTJL.webp)

*Feedback from function deployment*

You should now see the function in the Function App:

![](https://baeke.info/_astro/2019-04-image-27.DMv_d0rR_Z1N7BF5.webp)

*Deployed function*

To verify that the function works as expected, I started my [IoT Simulator](https://github.com/gbaeke/iot-simulator) with 100 devices that send data every 5 seconds. I also deleted all the existing data from the TimescaleDB hypertable. The **Live Metrics** stream shows the results. In this case, the function is running smoothly without connection reset errors. The consumption plan spun up **4 servers**:

![](https://baeke.info/_astro/2019-04-image-28.BpxkWYBM_Z27wLU3.webp)

*Live Metrics Stream of IoT Hub to PostgreSQL function*
