Open In App

How to Create Azure Function App using Terraform ?

Last Updated : 30 Apr, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Azure function app is a serverless implementation that provides execution of code without worrying about underlying infrastructure in Azure. It allows to writing of code in small functions called Azure functions which can be triggered using events in the Azure cloud. In this article let’s see how we can create a Azure function app using Terraform.

Understanding Of Primary Terminologies

The following are the primary components of Azure Vnet related to Terraform:

  • Terraform: It is Infrastructure as an infrastructure-as-a-service tool that allows the deployment of resources to multiple cloud providers through code.
  • Azure Function App: In Microsoft Azure, a function app is a serverless component that allows the execution of code without provisioning reserved infrastructure.
  • IaaC: Infrastructure as a Code allows the representation of cloud infrastructure in the form of code.

Setup Azure Function App Using Terraform: A Step-By-Step Guide

Step 1: Set Up Terraform

  • Download the Terraform zip from the installation page of the Terraform website.
  • Extract and paste the terraform folder to the required location and add the path to runnable in environment variables.
  • For MacOs install the terraform using HomeBrew.

Step 2: Set Up Azure CLI

  • Download the Azure CLI setup from the official website.
  • Run the installer and follow the steps to install.

Azure CLI

  • For MacOs install the Azure CLI using below HomeBrew Command.
brew update && brew install azure-cli

Step 3: Configure Azure CLI

  • Open terminal and run below command.
az login
  • A browser window will open for login. Login with your azure credentials. Once it is done you will see output as below.

Configuring Azure CLI

Step 4: Create Terraform Code

  • Goto your project folder and create main.tf file.
  • Add terraform block to code with azure as required provider with latest version. You can find the latest version at hashicorp registry.
  • Terraform block should look like below. You can add required version to avoid invalidation.
terraform {
required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = "~>3.0"
}
}
}
  • Now add provider as azurerm like below. Specify other details as required.
provider "azurerm" {
features {}
}
  • Before creating function app we must create storage account and app service plan. To create storage account add configuration as below.
resource "azurerm_storage_account" "deepslabteststorageacc" {
name = "deepslabteststorageacc"
resource_group_name = "DeepsLab"
location = "eastus"
account_tier = "Standard"
account_replication_type = "LRS"
}
  • Here we have mentioned account tier and replication type for storage account. Now add configuration for app service plan.
resource "azurerm_service_plan" "deepslabserviceplan" {
name = "deepslabserviceplan"
resource_group_name = "DeepsLab"
location = "eastus"
os_type = "Linux"
sku_name = "Y1"
}
  • The os type and SKU name has to be provided for service plan as mentioned above.
  • Finally add configuration for function app. For this article we will add only required arguments and use linux function app. You can specify other arguments if required.
resource "azurerm_linux_function_app" "deepslabtestfunctionapp" {
name = "deepslabtestfunctionapp"
resource_group_name = "DeepsLab"
location = "eastus"
storage_account_name = azurerm_storage_account.deepslabteststorageacc.name
storage_account_access_key = azurerm_storage_account.deepslabteststorageacc.primary_access_key
service_plan_id = azurerm_service_plan.deepslabserviceplan.id
depends_on = [ azurerm_service_plan.deepslabserviceplan , azurerm_storage_account.deepslabteststorageacc ]
site_config {}
}
  • We have specified name for function app. We have specified the location and resource group where virtual network should be created.
  • we have added storage account properties and service plan added earlier. We must add depends on so that storage account and service plan is created earlier.
  • The complete code will look like below.
terraform {
required_providers {
azurerm={
source = "hashicorp/azurerm"
version = "~>3.0"
}
}
}

provider "azurerm" {
features {}
}

resource "azurerm_storage_account" "deepslabteststorageacc" {
name = "deepslabteststorageacc"
resource_group_name = "DeepsLab"
location = "eastus"
account_tier = "Standard"
account_replication_type = "LRS"
}

resource "azurerm_service_plan" "deepslabserviceplan" {
name = "deepslabserviceplan"
resource_group_name = "DeepsLab"
location = "eastus"
os_type = "Linux"
sku_name = "Y1"
}

resource "azurerm_linux_function_app" "deepslabtestfunctionapp" {
name = "deepslabtestfunctionapp"
resource_group_name = "DeepsLab"
location = "eastus"
storage_account_name = azurerm_storage_account.deepslabteststorageacc.name
storage_account_access_key = azurerm_storage_account.deepslabteststorageacc.primary_access_key
service_plan_id = azurerm_service_plan.deepslabserviceplan.id
depends_on = [ azurerm_service_plan.deepslabserviceplan , azurerm_storage_account.deepslabteststorageacc ]
site_config {}
}

Step 5: Apply The Terraform Code

  • Once the code is ready you can apply it.
  • First init the terraform by running below command in project folder where main.tf is present.
terraform init

oie_WUV62j3y3sNs

  • After successful output of terraform apply the changes using below command.
terrraform apply
  • After verifying type “yes” to confirm and apply.
  • Terraform will start creating network.

oie_474qFoZ0qS2r

  • You can also verify deployment by visiting Virtual Networks page of Azure.

oie_2171834UmakcEeK

oie_2171857h6l86xUE

oie_2171919G8XXoDqy

Conclusion

We have successfully created Azure Function app with the help of terraform in this article. the configuration described can be further modified to make changes to OS type and function runtime in azure. This is how terraform allows reusable and modifiable configuration of infrastructure.

how to create azure function app using terraform – FAQ’s

Q. Can Terraform automatically manage the lifecycle of my Azure Function App?

Yes, Terraform can manage the lifecycle of your Azure Function App, including provisioning, updating, and destroying resources. This means you can easily make changes to your Function App configuration and apply them with Terraform, ensuring consistency and reliability.

Q. Can I manage multiple Azure Function Apps with Terraform?

Yes, Terraform supports managing multiple Azure Function Apps within the same configuration file. You can define multiple function app resources with different configurations, and Terraform will manage them accordingly.

Q. Can I customize my Azure Function App’s configuration using Terraform?

Yes, Terraform allows you to define various parameters such as runtime, trigger type, storage settings, and scaling options for your Function App, giving you full control over its configuration.

Q. What advantages does Terraform offer for managing Azure Function Apps?

Terraform enables infrastructure as code practices, ensuring consistent deployment and management of Function Apps. It simplifies provisioning, updates, and scaling, enhancing efficiency and reliability.

Q. Does Terraform support rollback and versioning for Function App deployments?

Terraform maintains state files that track infrastructure changes, allowing for easy rollback to previous versions if needed. Additionally, version control systems like Git can be used to manage Terraform configurations for versioning.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads