Skip to content

Commit 7085d03

Browse files
committed
Switched from Azure CLI to Bicep for provisioning infrastructure resources
1 parent 5c15714 commit 7085d03

File tree

4 files changed

+88
-256
lines changed

4 files changed

+88
-256
lines changed

.github/workflows/deploy_to_azure.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ jobs:
3434
run: cd hvalfangst_function && zip -r ../release.zip ./*
3535

3636
- name: Upload artifact for deployment job
37-
uses: actions/upload-artifact@v3
37+
uses: actions/upload-artifact@v4
3838
with:
3939
name: hvalfangst-function-app
4040
path: |
@@ -50,7 +50,7 @@ jobs:
5050

5151
steps:
5252
- name: Download artifact from build job
53-
uses: actions/download-artifact@v3
53+
uses: actions/download-artifact@v4
5454
with:
5555
name: hvalfangst-function-app
5656

@@ -61,7 +61,7 @@ jobs:
6161
uses: Azure/functions-action@v1
6262
id: deploy-to-function
6363
with:
64-
app-name: 'hvalfangstfunctionapp'
64+
app-name: 'hvalfangstlinuxfunctionapp'
6565
slot-name: 'Production'
6666
package: ${{ env.AZURE_FUNCTIONAPP_PACKAGE_PATH }}
6767
scm-do-build-during-deployment: true

infra/allocate_resources.sh

Lines changed: 3 additions & 163 deletions
Original file line numberDiff line numberDiff line change
@@ -1,165 +1,5 @@
1-
#!/bin/bash
1+
az bicep upgrade;
22

3-
# Load values from infra_config.env
4-
CONFIG_FILE="$(dirname "$0")/infra_config.env"
5-
if [ -f "$CONFIG_FILE" ]; then
6-
echo "Loading configuration from $CONFIG_FILE..."
7-
source "$CONFIG_FILE"
8-
else
9-
echo "Configuration file $CONFIG_FILE not found!"
10-
exit 1
11-
fi
3+
az bicep build --file infra/main.bicep;
124

13-
echo -e "\nSetting subscription context to $SUBSCRIPTION_ID..."
14-
az account set --subscription "$SUBSCRIPTION_ID"
15-
if [ $? -ne 0 ]; then
16-
echo "Failed to set subscription context."
17-
exit 1
18-
fi
19-
echo "Subscription context set successfully."
20-
21-
# Create the resource group
22-
echo -e "\nCreating resource group: $RESOURCE_GROUP_NAME in location $LOCATION..."
23-
az group create --name "$RESOURCE_GROUP_NAME" --location "$LOCATION"
24-
if [ $? -ne 0 ]; then
25-
echo "Failed to create resource group: $RESOURCE_GROUP_NAME."
26-
exit 1
27-
fi
28-
echo "Resource group $RESOURCE_GROUP_NAME created successfully."
29-
30-
# Create the Storage Account
31-
echo -e "\nCreating Storage Account: $STORAGE_ACCOUNT_NAME..."
32-
az storage account create --name "$STORAGE_ACCOUNT_NAME" --resource-group "$RESOURCE_GROUP_NAME" --location "$LOCATION" --sku Standard_LRS
33-
if [ $? -ne 0 ]; then
34-
echo "Failed to create Storage Account: $STORAGE_ACCOUNT_NAME."
35-
exit 1
36-
fi
37-
echo "Storage Account $STORAGE_ACCOUNT_NAME created successfully."
38-
39-
# Retrieve the Storage Account connection string
40-
echo -e "\nRetrieving connection string for Storage Account: $STORAGE_ACCOUNT_NAME..."
41-
STORAGE_CONNECTION_STRING=$(az storage account show-connection-string --name "$STORAGE_ACCOUNT_NAME" --resource-group "$RESOURCE_GROUP_NAME" --query "connectionString" -o tsv)
42-
if [ $? -ne 0 ]; then
43-
echo "Failed to retrieve connection string for Storage Account: $STORAGE_ACCOUNT_NAME."
44-
exit 1
45-
fi
46-
47-
# Create the Blob Container
48-
echo -e "\nCreating Blob Container: $BLOB_CONTAINER_NAME..."
49-
az storage container create --name "$BLOB_CONTAINER_NAME" --connection-string "$STORAGE_CONNECTION_STRING"
50-
if [ $? -ne 0 ]; then
51-
echo "Failed to create Blob Container: $BLOB_CONTAINER_NAME."
52-
exit 1
53-
fi
54-
echo "Blob Container $BLOB_CONTAINER_NAME created successfully."
55-
56-
57-
# Create an App Service Plan
58-
echo -e "\nCreating App Service Plan: $SERVICE_PLAN_NAME..."
59-
az appservice plan create \
60-
--name "$SERVICE_PLAN_NAME" \
61-
--resource-group "$RESOURCE_GROUP_NAME" \
62-
--location "$LOCATION" \
63-
--sku S1 \
64-
--is-linux
65-
if [ $? -ne 0 ]; then
66-
echo "Failed to create App Service Plan: $SERVICE_PLAN_NAME."
67-
exit 1
68-
fi
69-
echo "App Service Plan $SERVICE_PLAN_NAME created successfully."
70-
71-
72-
# Enable dynamic installation of extensions without prompting the user.
73-
echo -e "\nConfiguring Azure CLI to allow dynamic installation of extensions..."
74-
az config set extension.use_dynamic_install=yes_without_prompt
75-
if [ $? -ne 0 ]; then
76-
echo "Failed to configure Azure CLI for dynamic extension installation."
77-
exit 1
78-
fi
79-
echo "Azure CLI configured successfully for dynamic extension installation."
80-
81-
# Ensure the Application Insights extension is installed.
82-
echo -e "\nInstalling Application Insights extension if not already installed..."
83-
az extension add --name application-insights 2>/dev/null
84-
if [ $? -ne 0 ]; then
85-
echo "Failed to add the Application Insights extension. It might already be installed."
86-
else
87-
echo "Application Insights extension installed successfully."
88-
fi
89-
90-
# Verify that the Application Insights extension is installed.
91-
echo -e "\nVerifying Application Insights extension installation..."
92-
az extension show --name application-insights >/dev/null 2>&1
93-
if [ $? -ne 0 ]; then
94-
echo "Application Insights extension is not installed or failed to load."
95-
exit 1
96-
fi
97-
echo "Application Insights extension is verified as installed."
98-
99-
# Create an Application Insights resource
100-
echo -e "\nCreating Application Insights: $APP_INSIGHTS_NAME..."
101-
az monitor app-insights component create \
102-
--app "$APP_INSIGHTS_NAME" \
103-
--location "$LOCATION" \
104-
--resource-group "$RESOURCE_GROUP_NAME" \
105-
--application-type "web"
106-
if [ $? -ne 0 ]; then
107-
echo "Failed to create Application Insights: $APP_INSIGHTS_NAME."
108-
exit 1
109-
fi
110-
echo "Application Insights $APP_INSIGHTS_NAME created successfully."
111-
112-
# Retrieve Storage Account Key
113-
echo -e "\nRetrieving storage account key for $STORAGE_ACCOUNT_NAME..."
114-
STORAGE_ACCOUNT_KEY=$(az storage account keys list \
115-
--account-name "$STORAGE_ACCOUNT_NAME" \
116-
--resource-group "$RESOURCE_GROUP_NAME" \
117-
--query "[0].value" -o tsv)
118-
if [ $? -ne 0 ]; then
119-
echo "Failed to retrieve storage account key for $STORAGE_ACCOUNT_NAME."
120-
exit 1
121-
fi
122-
echo "Storage account key retrieved successfully."
123-
124-
# Retrieve Application Insights Keys
125-
echo -e "\nRetrieving Application Insights keys for $APP_INSIGHTS_NAME..."
126-
APP_INSIGHTS_KEY=$(az monitor app-insights component show \
127-
--app "$APP_INSIGHTS_NAME" \
128-
--resource-group "$RESOURCE_GROUP_NAME" \
129-
--query "instrumentationKey" -o tsv)
130-
if [ $? -ne 0 ]; then
131-
echo "Failed to retrieve Application Insights key."
132-
exit 1
133-
fi
134-
135-
# Create a Function App
136-
echo -e "\nCreating Function App: $FUNCTION_APP_NAME..."
137-
az functionapp create \
138-
--name "$FUNCTION_APP_NAME" \
139-
--resource-group "$RESOURCE_GROUP_NAME" \
140-
--storage-account "$STORAGE_ACCOUNT_NAME" \
141-
--plan "$SERVICE_PLAN_NAME" \
142-
--runtime python \
143-
--runtime-version 3.10 \
144-
--os-type Linux
145-
if [ $? -ne 0 ]; then
146-
echo "Failed to create Function App: $FUNCTION_APP_NAME."
147-
exit 1
148-
fi
149-
echo "Function App $FUNCTION_APP_NAME created successfully."
150-
151-
# Set Application Settings for Function App
152-
echo -e "\nConfiguring application settings for $FUNCTION_APP_NAME..."
153-
az functionapp config appsettings set \
154-
--name "$FUNCTION_APP_NAME" \
155-
--resource-group "$RESOURCE_GROUP_NAME" \
156-
--settings \
157-
"APPINSIGHTS_INSTRUMENTATIONKEY=$APP_INSIGHTS_KEY" \
158-
"AzureWebJobsFeatureFlags=EnableWorkerIndexing"
159-
if [ $? -ne 0 ]; then
160-
echo "Failed to configure application settings for $FUNCTION_APP_NAME."
161-
exit 1
162-
fi
163-
echo "Application settings configured successfully."
164-
165-
echo -e "\n\n - - - - | ALL RESOURCES WERE SUCCESSFULLY PROVISIONED | - - - - \n\n"
5+
az deployment group create --resource-group hvalfangstresourcegroup --template-file infra/main.bicep

infra/deallocate_resources.sh

Lines changed: 1 addition & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -1,90 +1 @@
1-
#!/bin/bash
2-
3-
# Load values from infra_config.env
4-
CONFIG_FILE="$(dirname "$0")/infra_config.env"
5-
if [ -f "$CONFIG_FILE" ]; then
6-
echo "Loading configuration from $CONFIG_FILE..."
7-
source "$CONFIG_FILE"
8-
else
9-
echo "Configuration file $CONFIG_FILE not found!"
10-
exit 1
11-
fi
12-
13-
echo -e "\nSetting subscription context to $SUBSCRIPTION_ID..."
14-
az account set --subscription "$SUBSCRIPTION_ID"
15-
if [ $? -ne 0 ]; then
16-
echo "Failed to set subscription context."
17-
exit 1
18-
fi
19-
echo "Subscription context set successfully."
20-
21-
# Delete Function App
22-
echo -e "\nDeleting Function App: $FUNCTION_APP_NAME..."
23-
az functionapp delete \
24-
--name "$FUNCTION_APP_NAME" \
25-
--resource-group "$RESOURCE_GROUP_NAME"
26-
if [ $? -ne 0 ]; then
27-
echo "Failed to delete Function App: $FUNCTION_APP_NAME."
28-
exit 1
29-
fi
30-
echo "Function App $FUNCTION_APP_NAME deleted successfully."
31-
32-
# Delete Application Insights
33-
echo -e "\nDeleting Application Insights: $APP_INSIGHTS_NAME..."
34-
az monitor app-insights component delete \
35-
--app "$APP_INSIGHTS_NAME" \
36-
--resource-group "$RESOURCE_GROUP_NAME"
37-
if [ $? -ne 0 ]; then
38-
echo "Failed to delete Application Insights: $APP_INSIGHTS_NAME."
39-
exit 1
40-
fi
41-
echo "Application Insights $APP_INSIGHTS_NAME deleted successfully."
42-
43-
# Delete Blob Container
44-
echo -e "\nDeleting Blob Container: $BLOB_CONTAINER_NAME..."
45-
az storage container delete \
46-
--name "$BLOB_CONTAINER_NAME" \
47-
--account-name "$STORAGE_ACCOUNT_NAME"
48-
if [ $? -ne 0 ]; then
49-
echo "Failed to delete Blob Container: $BLOB_CONTAINER_NAME."
50-
exit 1
51-
fi
52-
echo "Blob Container $BLOB_CONTAINER_NAME deleted successfully."
53-
54-
# Delete Storage Account
55-
echo -e "\nDeleting Storage Account: $STORAGE_ACCOUNT_NAME..."
56-
az storage account delete \
57-
--name "$STORAGE_ACCOUNT_NAME" \
58-
--resource-group "$RESOURCE_GROUP_NAME" \
59-
--yes
60-
if [ $? -ne 0 ]; then
61-
echo "Failed to delete Storage Account: $STORAGE_ACCOUNT_NAME."
62-
exit 1
63-
fi
64-
echo "Storage Account $STORAGE_ACCOUNT_NAME deleted successfully."
65-
66-
# Delete App Service Plan
67-
echo -e "\nDeleting App Service Plan: $SERVICE_PLAN_NAME..."
68-
az appservice plan delete \
69-
--name "$SERVICE_PLAN_NAME" \
70-
--resource-group "$RESOURCE_GROUP_NAME" \
71-
--yes
72-
if [ $? -ne 0 ]; then
73-
echo "Failed to delete App Service Plan: $SERVICE_PLAN_NAME."
74-
exit 1
75-
fi
76-
echo "App Service Plan $SERVICE_PLAN_NAME deleted successfully."
77-
78-
# Delete Resource Group
79-
echo -e "\nDeleting Resource Group: $RESOURCE_GROUP_NAME..."
80-
az group delete \
81-
--name "$RESOURCE_GROUP_NAME" \
82-
--yes \
83-
--no-wait
84-
if [ $? -ne 0 ]; then
85-
echo "Failed to delete Resource Group: $RESOURCE_GROUP_NAME."
86-
exit 1
87-
fi
88-
echo "Resource Group $RESOURCE_GROUP_NAME deletion initiated successfully."
89-
90-
echo -e "\n\n - - - - | ALL RESOURCES WERE SUCCESSFULLY DELETED | - - - - \n\n"
1+
az group delete --name hvalfangstresourcegroup --yes --no-wait

infra/main.bicep

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
targetScope = 'resourceGroup'
2+
3+
@description('The location where resources will be deployed.')
4+
param location string = 'westeurope'
5+
6+
@description('The name of the storage account.')
7+
param storageAccountName string = 'hvalfangststorageaccount'
8+
9+
@description('The name of the storage container.')
10+
param storageContainerName string = 'hvalfangstcontainer'
11+
12+
@description('The name of the App Service Plan.')
13+
param servicePlanName string = 'hvalfangstserviceplan'
14+
15+
@description('The name of the Application Insights instance.')
16+
param appInsightsName string = 'hvalfangstapplicationinsights'
17+
18+
@description('The name of the Linux Function App.')
19+
param functionAppName string = 'hvalfangstlinuxfunctionapp'
20+
21+
resource storageAccount 'Microsoft.Storage/storageAccounts@2022-09-01' = {
22+
name: storageAccountName
23+
location: location
24+
sku: {
25+
name: 'Standard_LRS'
26+
}
27+
kind: 'StorageV2'
28+
}
29+
30+
resource blobService 'Microsoft.Storage/storageAccounts/blobServices@2022-09-01' = {
31+
parent: storageAccount
32+
name: 'default'
33+
}
34+
35+
resource storageContainer 'Microsoft.Storage/storageAccounts/blobServices/containers@2022-09-01' = {
36+
parent: blobService
37+
name: storageContainerName
38+
}
39+
40+
resource appServicePlan 'Microsoft.Web/serverfarms@2022-09-01' = {
41+
name: servicePlanName
42+
location: location
43+
sku: {
44+
name: 'Y1'
45+
}
46+
kind: 'Linux'
47+
}
48+
49+
resource appInsights 'Microsoft.Insights/components@2020-02-02' = {
50+
name: appInsightsName
51+
location: location
52+
kind: 'other'
53+
properties: {
54+
Application_Type: 'other'
55+
}
56+
}
57+
58+
resource functionApp 'Microsoft.Web/sites@2022-09-01' = {
59+
name: functionAppName
60+
location: location
61+
kind: 'functionapp,linux'
62+
properties: {
63+
serverFarmId: appServicePlan.id
64+
siteConfig: {
65+
appSettings: [
66+
{
67+
name: 'APPINSIGHTS_INSTRUMENTATIONKEY'
68+
value: appInsights.properties.InstrumentationKey
69+
}
70+
{
71+
name: 'AzureWebJobsFeatureFlags'
72+
value: 'EnableWorkerIndexing'
73+
}
74+
]
75+
linux_fx_version: 'Python|3.10'
76+
}
77+
}
78+
identity: {
79+
type: 'SystemAssigned'
80+
}
81+
}

0 commit comments

Comments
 (0)