Skip to content

Commit 106a1af

Browse files
wgranthowbazaar
andauthored
chore: Upgrade to Python 3.12 (ENG-5440) (#13)
[ENG-5440](https://stacklet.atlassian.net/browse/ENG-5440) ### what Upgrade to Python 3.12, and upgrade other Python dependencies. Also build and publish the function code using native Terraform resources instead of execing the Azure CLI. ### why The dependencies were out of date and holey. And the Terraform was ugly and didn't no-op properly. These changes were mostly adapted from #11. ### testing Extensively on my and thumper's sandboxes. [ENG-5440]: https://stacklet.atlassian.net/browse/ENG-5440?atlOrigin=eyJpIjoiNWRkNTljNzYxNjVmNDY3MDlhMDU5Y2ZhYzA5YTRkZjUiLCJwIjoiZ2l0aHViLWNvbS1KU1cifQ --------- Co-authored-by: Tim Penhey <[email protected]>
1 parent ca1c036 commit 106a1af

File tree

6 files changed

+88
-115
lines changed

6 files changed

+88
-115
lines changed

.gitignore

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1-
function-app.zip
21
*.terraform
32
terraform.tfstate*
43
*.swp
54
settings.auto.tfvars
65
.terraform.lock.hcl
6+
7+
function-app.zip
8+
function-app-*.zip
9+
function-app-v1/host.json
710
function-app-v1/ProviderRelay/function.json
11+
812
venv

function-app-v1/.gitignore

Lines changed: 0 additions & 48 deletions
This file was deleted.

function-app-v1/ProviderRelay/function.json.tmpl

Lines changed: 0 additions & 12 deletions
This file was deleted.

function-app-v1/host.json

Lines changed: 0 additions & 16 deletions
This file was deleted.

function-app-v1/requirements.txt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
# The Python Worker is managed by the Azure Functions platform
1919
# Manually managing azure-functions-worker may cause unexpected issues
2020

21-
azure-functions==1.14.0
22-
azure-identity==1.13.0
23-
boto3==1.26.142
21+
# Core function dependencies - updated for Python 3.12 compatibility
22+
azure-functions==1.23.0
23+
azure-identity==1.23.0
24+
boto3==1.39.2

function.tf

Lines changed: 79 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -31,38 +31,106 @@ resource "azurerm_service_plan" "stacklet" {
3131
tags = local.tags
3232
}
3333

34+
# Generate the function.json with queue name
35+
resource "local_file" "function_json" {
36+
content = jsonencode({
37+
scriptFile = "__init__.py"
38+
bindings = [
39+
{
40+
name = "msg"
41+
type = "queueTrigger"
42+
direction = "in"
43+
queueName = azurerm_storage_queue.stacklet.name
44+
connection = "AzureWebJobsStorage"
45+
}
46+
]
47+
})
48+
filename = "${path.module}/function-app-v1/ProviderRelay/function.json"
49+
}
50+
51+
# Create host.json with enhanced logging configuration
52+
resource "local_file" "host_json" {
53+
content = jsonencode({
54+
version = "2.0"
55+
logging = {
56+
applicationInsights = {
57+
samplingSettings = {
58+
isEnabled = true
59+
excludedTypes = "Request"
60+
}
61+
}
62+
logLevel = {
63+
default = "Information"
64+
"ProviderRelay" = "Information"
65+
"azure.functions" = "Warning"
66+
"azure.storage" = "Warning"
67+
}
68+
}
69+
functions = ["ProviderRelay"]
70+
extensionBundle = {
71+
id = "Microsoft.Azure.Functions.ExtensionBundle"
72+
version = "[4.*, 5.0.0)"
73+
}
74+
})
75+
filename = "${path.module}/function-app-v1/host.json"
76+
}
77+
78+
# Create the function app deployment package
79+
data "archive_file" "function_app" {
80+
depends_on = [local_file.function_json, local_file.host_json]
81+
82+
type = "zip"
83+
source_dir = "${path.module}/function-app-v1"
84+
output_path = "${path.module}/function-app.zip"
85+
}
86+
87+
# azurerm_linux_function_app's zip_deploy_file will only redeploy the function
88+
# when the path changes, so copy the zip to a versioned filename.
89+
resource "local_file" "function_app_versioned" {
90+
filename = "${path.module}/function-app-${data.archive_file.function_app.output_sha256}.zip"
91+
source = "${path.module}/function-app.zip"
92+
}
93+
3494
resource "azurerm_linux_function_app" "stacklet" {
3595
name = "stacklet-${var.prefix}-function-app-${substr(random_string.storage_account_suffix.result, 0, 15)}"
3696
resource_group_name = azurerm_resource_group.stacklet_rg.name
3797
location = azurerm_resource_group.stacklet_rg.location
98+
service_plan_id = azurerm_service_plan.stacklet.id
3899

39100
storage_account_name = azurerm_storage_account.stacklet.name
40101
storage_account_access_key = azurerm_storage_account.stacklet.primary_access_key
41-
service_plan_id = azurerm_service_plan.stacklet.id
102+
103+
# Deploy from zip file
104+
zip_deploy_file = local_file.function_app_versioned.filename
42105

43106
site_config {
107+
application_insights_key = azurerm_application_insights.stacklet.instrumentation_key
108+
44109
application_stack {
45-
python_version = "3.10"
110+
python_version = "3.12"
46111
}
47-
application_insights_key = azurerm_application_insights.stacklet.instrumentation_key
48112
}
49113

50114
app_settings = {
115+
# Build and deployment settings
51116
SCM_DO_BUILD_DURING_DEPLOYMENT = true
52-
AZURE_CLIENT_ID = azurerm_user_assigned_identity.stacklet_identity.client_id
53-
AZURE_AUDIENCE = local.audience
54-
AZURE_STORAGE_QUEUE_NAME = azurerm_storage_queue.stacklet.name
55-
AWS_TARGET_ACCOUNT = var.aws_target_account
56-
AWS_TARGET_REGION = var.aws_target_region
57-
AWS_TARGET_ROLE_NAME = var.aws_target_role_name
58-
AWS_TARGET_PARTITION = var.aws_target_partition
59-
AWS_TARGET_EVENT_BUS = var.aws_target_event_bus
117+
118+
# Application configuration
119+
AZURE_CLIENT_ID = azurerm_user_assigned_identity.stacklet_identity.client_id
120+
AZURE_AUDIENCE = local.audience
121+
AZURE_STORAGE_QUEUE_NAME = azurerm_storage_queue.stacklet.name
122+
AWS_TARGET_ACCOUNT = var.aws_target_account
123+
AWS_TARGET_REGION = var.aws_target_region
124+
AWS_TARGET_ROLE_NAME = var.aws_target_role_name
125+
AWS_TARGET_PARTITION = var.aws_target_partition
126+
AWS_TARGET_EVENT_BUS = var.aws_target_event_bus
60127
}
61128

62129
identity {
63130
type = "UserAssigned"
64131
identity_ids = [azurerm_user_assigned_identity.stacklet_identity.id]
65132
}
133+
66134
tags = local.tags
67135

68136
lifecycle {
@@ -71,27 +139,3 @@ resource "azurerm_linux_function_app" "stacklet" {
71139
]
72140
}
73141
}
74-
75-
resource "local_file" "function_json" {
76-
content = templatefile(
77-
"${path.module}/function-app-v1/ProviderRelay/function.json.tmpl", { queue_name = azurerm_storage_queue.stacklet.name })
78-
filename = "${path.module}/function-app-v1/ProviderRelay/function.json"
79-
}
80-
81-
82-
resource "null_resource" "function_deploy" {
83-
depends_on = [azurerm_linux_function_app.stacklet, local_file.function_json]
84-
# ensures that publish always runs
85-
triggers = {
86-
build_number = "${timestamp()}"
87-
}
88-
89-
# initial deployment of the function-app could race with provisioning, so sleep 10 seconds
90-
provisioner "local-exec" {
91-
command = <<EOF
92-
cd ${path.module}/function-app-v1
93-
sleep 10
94-
func azure functionapp publish ${azurerm_linux_function_app.stacklet.name} --python
95-
EOF
96-
}
97-
}

0 commit comments

Comments
 (0)