Terraform On Azure - Creating A Azure Function Running On Consumption Plan

Introduction

Azure Functions which run on the Consumption Plan are serverless offerings on Azure Functions. You can host code that runs for shorter time intervals using Azure Functions. You get billed only when the code executes, and you don't ned to worry about the scaling aspects as the underlying Azure platform manages that. Azure Functions are powerful enough to cater to applications based on modern architectures like Microservices.  In this article, we will learn how to create a Azure Function running on Consumption Plan using Terraform.

In the previous articles, we learned the basics of Terraform. We created an Azure WebApp using Terraform and then we created Azure Virtual Machine and a Storage Account. 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 Consumption 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. 

Terraform On Azure - Creating A Azure Function Running On Consumption Plan

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 a Consumption Plan. Replace {ResourceGroup} with the name of your Resource Group, {Plan} with the name of your Consumption Plan.

resource "azurerm_app_service_plan"
"{Plan}" {
    name = "{Plan}"
    location = azurerm_resource_group. {
        ResourceGroup
    }.location
    resource_group_name = azurerm_resource_group. {
        ResourceGroup
    }.name
    kind = "FunctionApp"
    sku {
        tier = "Dynamic"
        size = "Y1"
    }
}

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 Consumption 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
    kind = "FunctionApp"
    sku {
        tier = "Dynamic"
        size = "Y1"
    }
}
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 Consumption Plan. In the next article, we will learn how to create an Azure Function using App Service Plan.