iacconfDemo2026/iac/functions.tf

91 lines
3.3 KiB
Terraform
Raw Permalink Normal View History

# Create a random pet to generate a unique resource group name
resource "random_pet" "rg_name" {
prefix = var.resource_group_name_prefix
}
# Create a resource group
resource "azurerm_resource_group" "example" {
location = var.resource_group_location
name = random_pet.rg_name.id
}
# Random String for unique naming of resources
resource "random_string" "name" {
length = 8
special = false
upper = false
lower = true
numeric = false
}
# Create a storage account
resource "azurerm_storage_account" "example" {
name = coalesce(var.sa_name, random_string.name.result)
resource_group_name = azurerm_resource_group.example.name
location = azurerm_resource_group.example.location
account_tier = var.sa_account_tier
account_replication_type = var.sa_account_replication_type
}
# Create a Log Analytics workspace for Application Insights
resource "azurerm_log_analytics_workspace" "example" {
name = coalesce(var.ws_name, random_string.name.result)
location = azurerm_resource_group.example.location
resource_group_name = azurerm_resource_group.example.name
sku = "PerGB2018"
retention_in_days = 30
}
# Create an Application Insights instance for monitoring
resource "azurerm_application_insights" "example" {
name = coalesce(var.ai_name, random_string.name.result)
location = azurerm_resource_group.example.location
resource_group_name = azurerm_resource_group.example.name
application_type = "web"
2026-09-22 00:03:45 +00:00
workspace_id = azurerm_log_analytics_workspace.example.id
}
2026-09-22 00:03:45 +00:00
# Create a service plan. Custom container images require a Dedicated (or Premium)
# plan - Consumption/Flex Consumption plans don't support bring-your-own-container.
resource "azurerm_service_plan" "example" {
name = coalesce(var.asp_name, random_string.name.result)
resource_group_name = azurerm_resource_group.example.name
location = azurerm_resource_group.example.location
2026-09-22 00:03:45 +00:00
sku_name = var.asp_sku_name
os_type = "Linux"
}
2026-09-22 00:03:45 +00:00
# Create a function app that runs the prebuilt container image straight from Docker Hub
resource "azurerm_linux_function_app" "example" {
name = coalesce(var.fa_name, random_string.name.result)
resource_group_name = azurerm_resource_group.example.name
location = azurerm_resource_group.example.location
service_plan_id = azurerm_service_plan.example.id
2026-09-22 00:03:45 +00:00
storage_account_name = azurerm_storage_account.example.name
storage_account_access_key = azurerm_storage_account.example.primary_access_key
site_config {
application_insights_connection_string = azurerm_application_insights.example.connection_string
2026-09-22 00:03:45 +00:00
application_stack {
docker {
registry_url = "https://index.docker.io"
image_name = var.container_image_name
image_tag = var.container_image_tag
}
}
}
app_settings = {
2026-09-22 00:03:45 +00:00
"OLLAMA_HOST" = "http://harbor.freshbrewed.science:11444"
"OLLAMA_MODEL" = "gemma4:e4b"
"JOKE_PROMPT" = "tell me a dad joke about programming"
"PORT" = "80"
"WEBSITES_PORT" = "80"
"WEBSITES_ENABLE_APP_SERVICE_STORAGE" = "false"
}
}
2026-09-22 00:03:45 +00:00