Unleash Azure's Magic: Mastering Serverless Function Apps with Terraform Automation!

Unleash Azure's Magic: Mastering Serverless Function Apps with Terraform Automation!

Introduction :-

This document provides a step-by-step guide on how to create a Function App in Azure using Terraform. Function Apps in Azure allow you to run code in a serverless environment, enabling you to focus on writing business logic without worrying about infrastructure management. Terraform, on the other hand, is an infrastructure as code tool that can help automate the provisioning and management of your Azure resources. By combining Azure and Terraform, you can easily create and deploy scalable Function Apps.

Prerequisites :-

Before proceeding with the creation of a Function App in Azure using Terraform, ensure that you have the following prerequisites in place:

  • An Azure subscription: You will need an active Azure subscription to create and manage Azure resources.

  • Terraform installed: Install Terraform on your local machine. You can find the installation instructions for your operating system on the official Terraform website.

Step 1:- Set up Azure credentials

To interact with Azure resources using Terraform, you need to configure your Azure credentials. Follow these steps to set up your credentials:

  1. Log in to the Azure portal (portal.azure.com) using your Azure account.

  2. Navigate to the Azure Active Directory page.

  3. Click on “App Registrations” and then select “New Registration”.

  4. Provide a name for your application, select the appropriate account type, and set the redirect URI if required.

  5. Once the registration is complete, note down the “Application (client) ID” and “Directory (tenant) ID”, Subscription_Id and Client Secret.

Step 2:- Create the Terraform configuration file

The next step is to create a Terraform configuration file (.tf) that defines the storage account resource. Follow these steps to create the file:

  1. Create a directory with name “azure_fa” and Open the directory using a text editor(VS Code) or an integrated development environment (IDE) of your choice.

  2. Create a new file in the azure directory and save it with a meaningful name, using the “.tf” extension. For example, “main.tf”.

  3. In the main.tf file, add the following Terraform code snippet:

#main.tf 
resource "azurerm_storage_account" "storage_account" {
  name                     = var.storage_account_name
  resource_group_name      = azurerm_resource_group.corp_network.name
  location                 = azurerm_resource_group.corp_network.location
  account_tier             = var.account_tier
  account_replication_type = var.repl_type
  is_hns_enabled           = var.datalake_v2
  tags                     = var.tags
  min_tls_version          = var.tls_ver

}

resource "azurerm_resource_group" "corp_network"{
  name     = var.resource_group_name
  location = var.location
}

resource "azurerm_service_plan" "corpnetworks_azure" {
  name                = "azure-corpnetworks"
  resource_group_name = azurerm_resource_group.corp_network.name
  location            = azurerm_resource_group.corp_network.location
  os_type             = "Windows"
  sku_name            = "EP1"
}

resource "azurerm_windows_function_app" "bulk_claim_create_ww_uatt" {
  name                     = "create-ww-uatt"
  resource_group_name      = azurerm_resource_group.corp_network.name
  location                 = azurerm_resource_group.corp_network.location
  storage_account_name       = azurerm_storage_account.storage_account.name
  storage_account_access_key = azurerm_storage_account.storage_account.primary_access_key
  service_plan_id            = azurerm_service_plan.corpnetworks_azure.id

  site_config {}
}

resource "azurerm_windows_function_app" "bulk_claim_process_ww_uatt" {
  name                     = "process-ww-uatt"
  resource_group_name      = azurerm_resource_group.corp_network.name
  location                 = azurerm_resource_group.corp_network.location
  storage_account_name       = azurerm_storage_account.storage_account.name
  storage_account_access_key = azurerm_storage_account.storage_account.primary_access_key
  service_plan_id            = azurerm_service_plan.corpnetworks_azure.id

  site_config {}
}

resource "azurerm_windows_function_app" "redeem_ww_uatt" {
  name                     = "redeem-ww-uatt"
  resource_group_name      = azurerm_resource_group.corp_network.name
  location                 = azurerm_resource_group.corp_network.location
  storage_account_name       = azurerm_storage_account.storage_account.name
  storage_account_access_key = azurerm_storage_account.storage_account.primary_access_key
  service_plan_id            = azurerm_service_plan.corpnetworks_azure.id

 site_config {}
}

4. Create a variables.tf and provider.tf files in the same directory and paste the below code into those files

#variables.tf
variable "resource_group_name" {
  description = "The name of the resource group in which the resources will be created."
  type        = string
  default     = "corp-network"
}

variable "location" {
  description = "(Optional) The location in which the resources will be created."
  type        = string
  default     = "East US 2"
}

variable "storage_account_name" {
  description = "The name of the new Storage Account."
  default     = "azure-sa"
}

variable "repl_type" {
  description = "The replication type required for the new Storage Account. Options are LRS; GRS; RAGRS; ZRS"
  type        = string
  default     = "LRS"
}

variable "account_tier" {
  description = "The Storage Tier for the new Account. Options are 'Standard' or 'Premium'"
  type        = string
  default     = "Standard"
}

variable "datalake_v2" {
  description = "Enabled Hierarchical name space for Data Lake Storage Gen 2"
  type        = bool
  default     = false
}

variable "tls_ver" {
  description = "Minimum version of TLS that must be used to connect to the storage account"
  type        = string
  default     = "TLS1_2"
}

variable "tags" {
  description = "tags to apply to the new resources"
  type        = map(string)
  default     = null
}
#provider.tf
terraform {
  required_providers {
    azurerm = {
      source = "hashicorp/azurerm"
      version = "3.42.0"
    }
  }
}
provider "azurerm" {
  subscription_id = "YOUR SUBSCRIPTION-ID"
  client_id       = "YOUR CLIENT-ID"
  client_secret   = "YOUR CLIENT-SECRET"
  tenant_id       = "YOUR TENANT-ID"
  features {}
}

Initialize and apply the Terraform configuration

Once the Terraform configuration file is set up, follow these steps to initialize and apply the configuration to create the storage account:

  1. Open a command-line interface (e.g., Windows Command Prompt, PowerShell, or Terminal).

  2. Navigate to the directory where you saved the Terraform configuration files.

  3. Run the following command to initialize the Terraform working directory:

terraform init
  1. After the initialization is complete, run the following command to apply the Terraform configuration:
terraform apply
  1. Terraform will display a summary of the planned actions. Review the changes to ensure they align with your expectations.

  2. If everything looks correct, approve the changes by entering “yes” when prompted.

  3. Terraform will then create the storage account based on the configuration provided. The process may take a few moments, so please be patient.

Conclusion :-

In conclusion, this document has provided a comprehensive guide on creating a Function App in Azure using Terraform. By following the outlined steps, you can leverage the power of Azure and Terraform to create and deploy scalable serverless applications. Remember to adhere to the best practices and security guidelines provided by Azure and Terraform to ensure the reliability and performance of your Function App. Happy coding!