Terraform On Azure - Creating A Azure Function Running On App Service Plan

Introduction

If you are using the Consumption Plan for Azure Function, then the Function execution will get timed out after 5 minutes by default, or at max you can set the timeout for 10 minutes. There can be scenarios where you may have to host long running code in Azure Functions. For example, you are planning to host a long running background task on Azure Function. In such scenarios, you can use App Service Plan for Azure Functions. We created an Azure WebApp using Terraform and then we created Azure Virtual Machine and a Storage Account. In the last article, we created an Azure Function running on Consumption Plan. The following are links to the previous articles.

Create Azure Function using Terraform

Let us create a Resource Group and inside it, we can create an Azure Function.

Log in to the Azure portal at https://portal.azure.com. Let us use Azure Cloud Shell to create resources using Terraform. Azure Cloud Shell has Terraform installed and you need not do any installation or configuration to work with Terraform.

Terraform On Azure - Creating A Azure Function Running On App Service Plan

Once the Azure Cloud Shell opens up, select Bash. Let us start creating scripts to create an Azure Function. We can use nano editor to create the Infrastructure as Code script for the Azure Function using Terraform.

Execute the following command to open a nano editor and create a file named myterraformscript.tf.

nano myterraformscript.tf

Add the following code to the nano editor. This would create a Resource Group. Replace {ResourceGroup} with the name of your Resource Group that you are planning to create.

terraform {
    required_providers {
        azurerm = {
            source = "hashicorp/azurerm"
        }
    }
}
provider "azurerm" {
    features {}
}
resource "azurerm_resource_group"
"{ResourceGroup}" {
    name = "{ResourceGroup}"
    location = "eastus"
}

Add the following code in nano editor to create a Storage Account. Replace {ResourceGroup} with the name of your Resource Group and {StorageAccount} with the name of your Storage Account.

resource "azurerm_storage_account"
"{StorageAccount}" {
    name = "{StorageAccount}"
    resource_group_name = azurerm_resource_group. {
        ResourceGroup
    }.name
    location = azurerm_resource_group. {
        ResourceGroup
    }.location
    account_tier = "Standard"
    account_replication_type = "LRS"
}

Add the following code to create an App Service Plan. Replace {ResourceGroup} with the name of your Resource Group, {Plan} with the name of your App Service Plan.

resource "azurerm_app_service_plan"
"{Plan}" {
    name = "{Plan}"
    location = azurerm_resource_group. {
        ResourceGroup
    }.location
    resource_group_name = azurerm_resource_group. {
        ResourceGroup
    }.name
    sku {
        tier = "Standard"
        size = "S1"
    }
}

Add the following code to create the Azure Function. Replace {ResourceGroup} with the name of your Resource Group, {StorageAccount} with the name of your Storage Account and {Plan} with the name of your App Service Plan.

resource "azurerm_function_app"
"{Function}" {
    name = "{Function}"
    location = azurerm_resource_group. {
        ResourceGroup
    }.location
    resource_group_name = azurerm_resource_group. {
        ResourceGroup
    }.name
    app_service_plan_id = azurerm_app_service_plan. {
        Plan
    }.id
    storage_account_name = azurerm_storage_account. {
        StorageAccount
    }.name
    storage_account_access_key = azurerm_storage_account. {
        StorageAccount
    }.primary_access_key
}

The following is the script file that you created. You can refer to the attached script file and try out the sample.

terraform {
    required_providers {
        azurerm = {
            source = "hashicorp/azurerm"
        }
    }
}
provider "azurerm" {
    features {}
}
resource "azurerm_resource_group"
"{ResourceGroup}" {
    name = "{ResourceGroup}"
    location = "eastus"
}
resource "azurerm_storage_account"
"{StorageAccount}" {
    name = "{StorageAccount}"
    resource_group_name = azurerm_resource_group. {
        ResourceGroup
    }.name
    location = azurerm_resource_group. {
        ResourceGroup
    }.location
    account_tier = "Standard"
    account_replication_type = "LRS"
}
resource "azurerm_app_service_plan"
"{Plan}" {
    name = "{Plan}"
    location = azurerm_resource_group. {
        ResourceGroup
    }.location
    resource_group_name = azurerm_resource_group. {
        ResourceGroup
    }.name
    sku {
        tier = "Standard"
        size = "S1"
    }
}
resource "azurerm_function_app"
"{Function}" {
    name = "{Function}"
    location = azurerm_resource_group. {
        ResourceGroup
    }.location
    resource_group_name = azurerm_resource_group. {
        ResourceGroup
    }.name
    app_service_plan_id = azurerm_app_service_plan. {
        Plan
    }.id
    storage_account_name = azurerm_storage_account. {
        StorageAccount
    }.name
    storage_account_access_key = azurerm_storage_account. {
        StorageAccount
    }.primary_access_key
}

Run the following command to initiate Terraform. This would fetch all dependencies needed to execute the Terraform script.

terraform init

Now let us create an execution plan for Terraform. Let us provide the name of the execution plan in the out parameter.

terraform plan -out myfunc.tfplan

Execute the execution plan using the following command. The Azure Function gets created.

terraform apply "myfunc.tfplan"

Conclusion

In this article, we learned how to create an Azure Function with App Service Plan. In the next article, we will learn how to create an Azure SQL Database using Terraform.


Similar Articles