Deploying Azure Functions with Terraform and Visual Studio Code

Abhimanyubajaj
5 min readOct 2, 2023

Introduction

Azure Functions revolutionize application development on Microsoft Azure by enabling developers to focus on specific tasks without being bogged down by the application’s full scale or its underlying infrastructure. With support for triggers and bindings, Azure Functions can handle essential system events like responding to database changes, processing IoT data streams, and managing message queues.

Terraform, an Infrastructure as Code (IAC) tool, provides the power to template infrastructure components and share them across teams using platforms like GitHub.

In this guide, we will dive into setting up Azure resources using Terraform and deploying a Python-based Azure Function through Visual Studio Code.

Setting Up Azure Resources with Terraform

Our Terraform configuration targets several Azure resources: a resource group, storage account, service plan, and a Linux-based function app, all centered in the East US region. Additionally, we’re going with Python 3.11 as our chosen runtime.

terraform {
required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = "3.75.0"
}
}
}

provider "azurerm" {
features {
resource_group {
prevent_deletion_if_contains_resources = false
}
}
}

resource "azurerm_resource_group" "linux-function-rg" {
name = var.resource_group_name
location = "East US"
}

resource "azurerm_storage_account" "linux-storage-account" {
name = var.storage_account_name
resource_group_name = azurerm_resource_group.linux-function-rg.name
location = azurerm_resource_group.linux-function-rg.location
account_tier = "Standard"
account_replication_type = "LRS"
}

resource "azurerm_service_plan" "linux-service-plan" {
name = var.service_plan_name
resource_group_name = azurerm_resource_group.linux-function-rg.name
location = azurerm_resource_group.linux-function-rg.location
os_type = "Linux"
sku_name = "Y1"
}

resource "azurerm_application_insights" "linux-application-insights" {
name = "application-insights-${var.azurerm_linux_function_app}"
location = "${azurerm_resource_group.linux-function-rg.location}"
resource_group_name = "${azurerm_resource_group.linux-function-rg.name}"
application_type = "other"
}

resource "azurerm_linux_function_app" "linux-python-linux-function-app" {
name = var.azurerm_linux_function_app
resource_group_name = azurerm_resource_group.linux-function-rg.name
location = azurerm_resource_group.linux-function-rg.location

service_plan_id = azurerm_service_plan.linux-service-plan.id
storage_account_name = azurerm_storage_account.linux-storage-account.name
storage_account_access_key = azurerm_storage_account.linux-storage-account.primary_access_key
https_only = true
site_config {
application_insights_key = azurerm_application_insights.linux-application-insights.instrumentation_key
application_insights_connection_string = azurerm_application_insights.linux-application-insights.connection_string
application_stack {
python_version = 3.11 #FUNCTIONS_WORKER_RUNTIME
}
}
app_settings = {
"APPINSIGHTS_INSTRUMENTATIONKEY" = "${azurerm_application_insights.linux-application-insights.instrumentation_key}"
}
}


output "resource_group_id" {
value = azurerm_resource_group.linux-function-rg.id
}

output "storage_account_id" {
value = azurerm_storage_account.linux-storage-account.id
}

output "service_plan_id" {
value = azurerm_service_plan.linux-service-plan.id
}

output "linux_function_app_id" {
value = azurerm_linux_function_app.linux-python-linux-function-app.id
}

For a breakdown of variables and further customization, refer to the variables.tf file in the project. All source code is accessible on GitHub.

Getting Started with Deployment:

terraform init
terraform apply

Creating and Deploying an HTTP Trigger with Visual Studio Code

With our Azure infrastructure in place, let’s create an HTTP trigger and deploy it:
Download Visual Studio Code.
Once you have downloaded, Open VS Code, Go to extension tab on the left and search for “azure
- Install Azure Tools

Once you have installed Azure tools, You will see Azure option pop up in your left hand panel,
- Login to your azure account to view all services.

Once you have signed in, All Azure resources will show up.

So far so good!!
It’s time to deploy your http trigger on Azure function.

- Go to Workspace section at the bottom and click on Function App icon, Click on Create New Project. Select or create a new folder in your local which will be our Project Folder. I created a folder called azurefunctionfolderforcode

- Select Python -> Model V2 -> python3 3.11.1 -> HTTP Trigger -> Add to Workspace

Once you have done this, You will see your Workspace populate with your local folder as the Local Project.

On top left, If you click on Explorer you can see and edit the code that was inserted by HTTP Trigger function!!

Great! To deploy the function, Go to WorkSpace option at the bottom left again, click the Azure Function icon and select “Deploy to Function App..”

You will get option to select the function you wish to deploy in, I selected “abhi-python-function” and click on Deploy

You can check your deployment status in the bottom under output tab

Conclusion

Congratulations! Navigate to your Azure console, and you should spot your http_trigger under the functions tab. Not only have you deployed an Azure Function, but you've also set up monitoring capabilities since we've integrated App Insight.

Happy coding, and may your functions run smoothly!

On a side note:

To uninstall Visual Studio code on Mac:
rm -fr ~/Library/Preferences/com.microsoft.VSCode.helper.plist
rm -fr ~/Library/Preferences/com.microsoft.VSCode.plist
rm -fr ~/Library/Caches/com.microsoft.VSCode
rm -fr ~/Library/Caches/com.microsoft.VSCode.ShipIt/
rm -fr ~/Library/Application\ Support/Code/
rm -fr ~/Library/Saved\ Application\ State/com.microsoft.VSCode.savedState/
rm -fr ~/.vscode/

For more information or in case you get stuck, Feel free to connect with me on LinkedIn

https://www.linkedin.com/in/theabhibajaj/

--

--

Abhimanyubajaj

I solve problems. CKAD, CKA, Azure, AWS, GCP, Terraform Certified. Senior Software Engineer at Cisco.