Scroll to top
© 2024, Global Digital Services LLC.

Serverless Infrastructure with Azure and Terraform


Fernando Noguera - June 22, 2023 - 0 comments

In this blog post, we will explore how to deploy Azure Functions using Terraform.

Azure Functions scenarios

Azure Functions can manage responses to critical events, build web APIs, database changes, and process event streams. Below are some common scenarios where Azure Functions are utilized.

Getting started with Azure Functions using Terraform

Let’s take it a step further to understand Azure Functions. We will utilize Terraform to automate the infrastructure setup. In this demo, we will create the following resources.

Prerequisites:

Before diving into the deployment process, ensure you have the following prerequisites in place:

  1. An Azure subscription: Sign up for Azure 
  2. Terraform installed: Install Terraform on your development machine from the official Terraform website.
  3. Azure CLI: Install the Azure CLI.

Deploying an Azure Function using Terraform

Step 1:  Set Up Your Terraform Project Start by creating a new directory for your Terraform project and navigate in your command-line interface (CLI). Initialize the Terraform project by running the following command:

terraform init

Step 2: Define Azure Provider and Variables Create a file named main.tf and add the following code to specify the Azure provider and declare any required variables:

provider "azurerm" {
features {}
}

variable "function_app_name" {
type = string
description = "The name of the Azure Function App"
default = "my-function-app" # Provide a valid default value or assign a value during runtime
}

Step 3: Create an Azure Function App Add the following code to the main.tf file to define the Azure Function App resource:

resource "azurerm_function_app" "example" { 
name = var.function_app_name location = "West Europe" 
resource_group_name = azurerm_resource_group.example.name 
app_service_plan_id = azurerm_app_service_plan.example.id 
storage_connection_string = azurerm_storage_account.example.primary_connection_string 
}

Step 4: Define Supporting Resources For your Azure Function to work correctly, you might need additional resources such as an application service plan and a storage account. Add the following code to main.tf to define these resources:

resource "azurerm_app_service_plan" "example" {
  name                = "${var.function_app_name}-plan"
  location            = "West Europe"
  resource_group_name = azurerm_resource_group.example.name
  kind                = "FunctionApp"

  sku {
    tier = "Dynamic"
    size = "Y1"
  }
}

resource "azurerm_storage_account" "example" {
  name                     = "${var.function_app_name}storage"
  resource_group_name      = azurerm_resource_group.example.name
  location                 = "West Europe"
  account_tier             = "Standard"
  account_replication_type = "LRS"
  access_tier              = "Hot"
  enable_https_traffic_only = true

  depends_on = [azurerm_resource_group.example]

}

Step 5: Initialize Azure and Deploy Resources Now, you need to authenticate with Azure and initialize the deployment. Run the following commands:

az login
terraform init

Step 6: Deploy the Azure Function Run the following command to apply the Terraform configuration and deploy the Azure Function:

terraform apply

Terraform will display a summary of the resources that will be created. Review the output and enter “yes” to proceed with the deployment. 

Deploying Azure Functions with Terraform simplifies managing serverless resources in Azure. Automate the infrastructure deployment to ensure consistency and repeatability across multiple environments.

 In this blog post, we walked through an example of deploying an Azure Function using Terraform. GDS’s team can create cloud-based infrastructure for your serverless needs. Reach out to us and find out how we can help you with your specific needs.

Contact us for more information or  visit our blog.

Related posts