Create Function App using Terraform with functions extension version 4 - azure

I have little problem that I am trying to solve.
I am trying to create function apps using terraform as IaC and my only problem is with functions extension version in version 4.
When I insert the variable like this:
functions_extension_version = "~4"
I get this version as custom (I am trying to create Dotnet version 6 so it must be version 4)
But when I use version 3 like this:
functions_extension_version = "~3"
it works fine
I know my app service plan is support functions extension version 4 becuase when I try to create manually it is working as expected.
Thanks
edit:
full TF code:
resource "azurerm_windows_function_app" "function_windows" {
# condition
count = var.to_create ? 1 : 0
# basic info configuration
name = var.name
resource_group_name = var.resource_group_name
location = var.location
storage_account_name = data.azurerm_storage_account.storage_account.name
storage_account_access_key = data.azurerm_storage_account.storage_account.primary_access_key
service_plan_id = var.app_service_plan_id
# function configuration
https_only = true
functions_extension_version = "~4"
site_config {
application_stack {
dotnet_version = var.runtime == "dotnet" ? var.runtime_version : null
java_version = var.runtime == "java" ? var.runtime_version : null
node_version = var.runtime == "node" ? var.runtime_version : null
}
scm_use_main_ip_restriction = true
elastic_instance_minimum = local.elastic_instance_minimum
}
identity {
type = "SystemAssigned"
}
tags = var.tags
}

Your app is pinned to an unsupported runtime version for ‘dotnet’. For better performance, we recommend using one of our supported versions instead: ~3.
There are no issues with your terraform code, but the issue I found is with the version running in the backend.
According to #Sandro Pereira's blog,
If you encounter this kind of error or a similar alert, it signifies that the runtime version you defined uses an unsupported version of .NET Framework. In some instances, although your
function runtime version = ~4 works with .NET framework v6.0, but it actually runs using v4.0 in the backend.
Runtime version ~3 normally runs on top of .NET Framework v4.0. However, runtime version ~4 requires .NET Framework v6.0.
By referring terraform registry, I created the replica of a function app with terraform and deployed successfully.
Code snippet:
resource "azurerm_function_app" "newfunc" {
name = "test-azure-functions"
location = azurerm_resource_group.newfunc.location
resource_group_name = azurerm_resource_group.newfunc.name
app_service_plan_id = azurerm_app_service_plan.newfunc.id
storage_account_name = "mystorage"
}
After successful deployment in Azure portal:
You can validate which version of .NET is running for your function app by checking:
Resource Explorer (azure.com)
Validate the netFrameworkVersion:
Note: Refer the above blog for path to validation

Related

How to set my azure function's Platform with Terraform?

I am trying to change my Azure function's Platform to 64 bit to be compatible with a new dll or project needs. I just have not been able to find the corresponding terraform key to set this azure function field.
The problem is that terraform is currently defaulting to 32 bit, so whenever I deploy the field changes.
Any help would be appreciated. Thank you!
I've tried poking around with some app_settings keys from Microsoft documentation, but none of them seem obviously connected to the platform version. I have also tried looking at the keys here terraform documentation and none of those jump out to me either.
Here is my terraform not showing app_settings
resource "azurerm_app_service_plan" "plan" {
count = length(var.resource_groups)
name = "${var.name}-asp${count.index + 1}"
location = var.resource_groups[count.index].location
resource_group_name = var.resource_groups[count.index].name
kind = "FunctionApp"
sku {
tier = var.app_service_plan_tier
size = var.app_service_plan_size
}
tags = var.tags
}
resource "azurerm_function_app" "function" {
count = length(azurerm_app_service_plan.plan.*)
name = "${var.name}${count.index + 1}"
location = azurerm_app_service_plan.plan[count.index].location
resource_group_name = azurerm_app_service_plan.plan[count.index].resource_group_name
app_service_plan_id = azurerm_app_service_plan.plan[count.index].id
storage_account_name = var.storage_account_name
storage_account_access_key = var.storage_account_access_key
app_settings = local.app_settings
version = "~2"
https_only = true
tags = var.tags
}
The resource that manages the configuration of the worker is the azurerm_function_app resource.
Setting the attribute use_32_bit_worker_process to true will run the applications in a 32-bit platform, which it's the default value.
Explicitly set use_32_bit_worker_process to false and be sure to use any other tiers than Free or Shared, as stated in the docs:
when using an App Service Plan in the Free or Shared Tiers use_32_bit_worker_process must be set to true.
javierlga is correct,
set use_32_bit_worker_process to false inside site_config block.
site_config = {
use_32_bit_worker_process = false
}
More info:
https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/function_app#use_32_bit_worker_process
You are able to set the bitness of your worker in the azurerm_windows_function_app resource.
Set use_32_bit_worker to false.
Note: azurerm_function_app is deprecated and has been superseeded by windows_function_app or azurerm_linux_function_app

How do I add a new API version via Terraform in Azure API Manager?

I would like to add a new API version under the existing API that is already there. I can do that easily via the Portal UI however, can someone please guide me to how to achieve this via Terraform? Any sample snippet would be helpful.
I am trying to reverse engineer v1, v2 of this into Terraform. Thanks.
Terraform supports API Version Set and reference it in the API:
version - (Optional) The Version number of this API, if this API is versioned.
version_set_id - (Optional) The ID of the Version Set which this API is associated with.
NOTE:
When version is set, version_set_id must also be specified
resource "azurerm_api_management_api_version_set" "example" {
name = "example-apimapi"
resource_group_name = var.resource_group_name
api_management_name = var.apim_name
display_name = "ExampleAPIVersionSet"
versioning_scheme = "Segment"
}
resource "azurerm_api_management_api" "example" {
name = "example-api"
resource_group_name = var.resource_group_name
api_management_name = var.apim_name
revision = "1"
display_name = "Example API"
path = "example_me"
protocols = ["https"]
service_url = "https://conferenceapi.azurewebsites.net/"
version = "v1"
version_set_id = azurerm_api_management_api_version_set.example.id
import {
content_format = "swagger-link-json"
content_value = "http://conferenceapi.azurewebsites.net/?format=json"
}
}
There's a tutorial for doing it the Portal: Tutorial: Publish multiple versions of your API
When you create multiple versions, the Azure portal creates a version set, which represents a set of versions for a single logical API. Select the name of an API that has multiple versions. The Azure portal displays its Version set. You can customize the Name and Description of a virtual set.

unable to sepcify backup in terraform cosmodb

I am using terraform to create CosmoDB , my build uses azurerm 2.56.0
resource "azurerm_cosmosdb_account" "testaccount" {
name = "testaccount"
location = var.location
resource_group_name = var.rgname
offer_type = "Standard"
Kind = "GlobalDocumentDB"
enable_automatic_failover = false
consistent_policy {
consistency_level = "Session"
}
backup {
type = "Periodic"
interval_in_minutes = "120"
retention_in_hours = "14"
}
}
I am getting following error
Error: Unsupported block type
When I comment out the backup section, it works fine.
I checked cosmosdb account https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/cosmosdb_account#backup
it does seem like I have declared it correctly. I have also checked that this version of azurerm supports backups
I am probably missing something obvious, does anyone see what the problem is?
Thanks
Dan
backup is not supported in 2.56.0. You are looking at newer docs. For your version, the docs are here.
If you want to use backup, you have to upgrade your provider.

How to deploy a Windows VM with Terraform Azure CAF?

I want to deploy a Windows VM with Azure Cloud Adoption Framework (CAF) using Terraform. In the example of configuration.tfvars, all the configuration is done.But I cannot find the correct terraform code to deploy this tfvars configuration.
The windows vm module is here.
So far, i have written the code below:
module "caf_virtual_machine" {
source = "aztfmod/caf/azurerm//modules/compute/virtual_machine"
version = "5.0.0"
# belows are the 7 required variables
base_tags = var.tags
client_config =
global_settings = var.global_settings
location = var.location
resource_group_name = var.resource_group_name
settings =
vnets = var.vnets
}
So the vnets, global_settings, resource_group_name variables already exists in the configuration.tfvars. I have added tags and location variables to the configuration.tfvars.
But what should i enter to settings and client_config variables?
The virtual machine is a private module. You should use it by calling the base CAF module.
The Readme of the terraform registry explains how to leverage the core CAF module - https://registry.terraform.io/modules/aztfmod/caf/azurerm/latest/submodules/virtual_machine
Source code of an example:
https://github.com/aztfmod/terraform-azurerm-caf/tree/master/examples/compute/virtual_machine/211-vm-bastion-winrm-agents/registry
You have a library of configuration files examples showing how to deploy virtual machines
https://github.com/aztfmod/terraform-azurerm-caf/tree/master/examples/compute/virtual_machine
module "caf" {
source = "aztfmod/caf/azurerm"
version = "5.0.0"
global_settings = var.global_settings
tags = var.tags
resource_groups = var.resource_groups
storage_accounts = var.storage_accounts
keyvaults = var.keyvaults
managed_identities = var.managed_identities
role_mapping = var.role_mapping
diagnostics = {
# Get the diagnostics settings of services to create
diagnostic_log_analytics = var.diagnostic_log_analytics
diagnostic_storage_accounts = var.diagnostic_storage_accounts
}
compute = {
virtual_machines = var.virtual_machines
}
networking = {
vnets = var.vnets
network_security_group_definition = var.network_security_group_definition
public_ip_addresses = var.public_ip_addresses
}
security = {
dynamic_keyvault_secrets = var.dynamic_keyvault_secrets
}
}
Note - it is recommended to leverage the VScode devcontainer provided in the source repository to execute the terraform deployment. The devcontainer includes the tooling required to deploy Azure solutions.

How Do I Set The Node Version In An Azure Web App?

I have created an Azure Web App using terraform but it has the wrong version of NodeJS in it.
resource "azurerm_app_service_plan" "app-plan" {
name = "${var.prefix}-app-plan"
resource_group_name = var.resource_group_name
location = var.resource_group_location
sku {
tier = "Free"
size = "F1"
}
}
#azurerm_app_service doesn't support creating Node.JS 8.10 apps
#https://github.com/terraform-providers/terraform-provider-azurerm/issues/4144
resource "azurerm_app_service" "app-service" {
name = "${var.prefix}-app"
resource_group_name = var.resource_group_name
location = var.resource_group_location
app_service_plan_id = azurerm_app_service_plan.app-plan.id
}
I have tried updating the configuration using the rest api
{
"properties": {
"nodeVersion": "8.10"
}
}
and also updating the application settings using the rest api
{
"properties": {
"WEBSITE_NODE_DEFAULT_VERSION": "8.10"
}
}
However, when I run the Console it still says node --version v0.10.40
When I run env it looks like the PATH variable is incorrect.
Node 8.10 does exist on the machine at D:\Program Files (x86)\nodejs\8.10.0
How can I update the path from the rest api?
Are there any alternatives?
My preferences are terraform > az cl > rest api
Note:
Bear in mind that when I create the web app in the portal, selecting Node 8.10 forces me to choose Windows as the O/S.
Under site_config, linux_fx_version should be set to "NODE|8.10"
I have gotten it to work with node 10.14, using:
site_config {
linux_fx_version = "NODE|10.14"
}
You can also see different examples of azure web apps over at:
https://github.com/terraform-providers/terraform-provider-azurerm/tree/master/examples/app-service
In the portal it specifies Node 8.10 as a Runtime stack.
The az cli specifies 8.10 as a runtime:
az webapp list-runtimes|grep "8.10"
"node|8.10",
However, as you can see in the question, the version installed is 8.10.0.
If we set this in the application settings with terraform this (unintuitively) sets the correct node version:
resource "azurerm_app_service" "app-service" {
name = "${var.prefix}-app"
resource_group_name = var.resource_group_name
location = var.resource_group_location
app_service_plan_id = azurerm_app_service_plan.app-plan.id
app_settings = {
#The portal and az cli list "8.10" as the supported version.
#"8.10" doesn't work here!
#"8.10.0" is the version installed in D:\Program Files (x86)\nodejs
"WEBSITE_NODE_DEFAULT_VERSION" = "8.10.0"
}
}

Resources