iacconfDemo2026/iac/functions.tf

142 lines
4.7 KiB
Terraform
Raw 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 storage container
resource "azurerm_storage_container" "example" {
name = "example-flexcontainer"
storage_account_id = azurerm_storage_account.example.id
container_access_type = "private"
}
# 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"
workspace_id = azurerm_log_analytics_workspace.example.id
}
# Create a service plan
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
sku_name = "FC1"
os_type = "Linux"
}
data "archive_file" "function_app" {
type = "zip"
source_dir = "${path.module}/.."
output_path = "${path.module}/function_app.zip"
excludes = [".git", ".gitea", "iac", "helm-chart", ".terraform*", ".vscode"]
}
resource "azurerm_storage_blob" "appcode" {
name = "app-${random_string.name.result}.zip"
storage_account_name = azurerm_storage_account.example.name
storage_container_name = azurerm_storage_container.example.name
type = "Block"
source = data.archive_file.function_app.output_path
}
data "azurerm_storage_account_sas" "sas" {
connection_string = azurerm_storage_account.example.primary_connection_string
https_only = true
resource_types {
service = false
container = false
object = true
}
services {
blob = true
queue = false
table = false
file = false
}
start = "2023-01-01T00:00:00Z"
expiry = "2029-01-01T00:00:00Z"
permissions {
read = true
write = false
delete = false
list = false
add = false
create = false
update = false
process = false
tag = false
filter = false
}
}
# Create a function app
resource "azurerm_function_app_flex_consumption" "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
storage_container_type = "blobContainer"
storage_container_endpoint = "${azurerm_storage_account.example.primary_blob_endpoint}${azurerm_storage_container.example.name}"
storage_authentication_type = "StorageAccountConnectionString"
storage_access_key = azurerm_storage_account.example.primary_access_key
runtime_name = var.runtime_name
runtime_version = var.runtime_version
maximum_instance_count = 50
instance_memory_in_mb = 2048
site_config {
application_insights_connection_string = azurerm_application_insights.example.connection_string
}
app_settings = {
"OLLAMA_HOST" = "http://harbor.freshbrewed.science:11444"
"OLLAMA_MODEL" = "gemma4:e4b"
"JOKE_PROMPT" = "tell me a dad joke about programming"
"PORT" = "80"
"WEBSITE_RUN_FROM_PACKAGE" = "${azurerm_storage_account.example.primary_blob_endpoint}${azurerm_storage_container.example.name}/${azurerm_storage_blob.appcode.name}${data.azurerm_storage_account_sas.sas.sas}"
}
}