Skip to content

Commit 5f6de4a

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

File tree

5 files changed

+159
-248
lines changed

5 files changed

+159
-248
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

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010

1111
## Allocate resources
1212

13-
The shell script [allocate_resources](infra/allocate_resources.sh) creates Azure resources by calling the Azure CLI, which in turn
14-
makes HTTP calls to the resource-specific API on Azure.
13+
The shell script [allocate_resources](infra/allocate_resources.sh) creates Azure resources specified in a
14+
[Bicep](https://learn.microsoft.com/en-us/azure/azure-resource-manager/bicep/overview?tabs=bicep) template [file](infra/main.bicep).
1515

1616
It will create the following hierarchy of resources:
1717

infra/allocate_resources.sh

Lines changed: 27 additions & 155 deletions
Original file line numberDiff line numberDiff line change
@@ -1,165 +1,37 @@
11
#!/bin/bash
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
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-
# 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."
3+
# Define colors for console output
4+
GREEN="\e[32m"
5+
RED="\e[31m"
6+
BLUE="\e[34m"
7+
CYAN="\e[36m"
8+
YELLOW="\e[33m"
9+
RESET="\e[0m"
10+
# echo -e "${GREEN}This text is green.${RESET}"
7011

12+
# Variables
13+
RESOURCE_GROUP="hvalfangstresourcegroup"
14+
LOCATION="westeurope"
15+
BICEP_FILE="infra/main.bicep"
7116

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
17+
# Function to handle errors
18+
handle_error() {
19+
echo -e "${RED}Error occurred in script at line: ${BASH_LINENO[0]}. Exiting...${RESET}"
20+
exit 1
21+
}
8922

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."
23+
# Set trap to catch errors and execute handle_error
24+
trap 'handle_error' ERR
9825

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."
26+
# Create Resource Group
27+
echo -e "${YELLOW}Creating resource group $RESOURCE_GROUP in $LOCATION ${RESET}"
28+
az group create --name $RESOURCE_GROUP --location $LOCATION
15029

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"
30+
# Deploy Bicep template
31+
echo -e "${YELLOW}Deploying Bicep template...${RESET}"
32+
az deployment group create --resource-group $RESOURCE_GROUP --template-file $BICEP_FILE -c
15933
if [ $? -ne 0 ]; then
160-
echo "Failed to configure application settings for $FUNCTION_APP_NAME."
161-
exit 1
34+
echo -e "${RED}Failed to deploy Bicep template.${RESET}"
16235
fi
163-
echo "Application settings configured successfully."
16436

165-
echo -e "\n\n - - - - | ALL RESOURCES WERE SUCCESSFULLY PROVISIONED | - - - - \n\n"
37+
echo -e "${GREEN}Deployment completed.${RESET}"

infra/deallocate_resources.sh

Lines changed: 26 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -1,90 +1,28 @@
11
#!/bin/bash
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
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"
3+
# Define colors for console output
4+
GREEN="\e[32m"
5+
RED="\e[31m"
6+
BLUE="\e[34m"
7+
CYAN="\e[36m"
8+
YELLOW="\e[33m"
9+
RESET="\e[0m"
10+
# echo -e "${GREEN}This text is green.${RESET}"
11+
12+
# Variables
13+
RESOURCE_GROUP="hvalfangstresourcegroup"
14+
LOCATION="westeurope"
15+
BICEP_FILE="infra/main.bicep"
16+
17+
# Function to handle errors
18+
handle_error() {
19+
echo -e "${RED}Error occurred in script at line: ${BASH_LINENO[0]}. Exiting...${RESET}"
20+
exit 1
21+
}
22+
23+
# Set trap to catch errors and execute handle_error
24+
trap 'handle_error' ERR
25+
26+
echo -e "${YELLOW}Deallocating resources asynch...${RESET}"
27+
28+
az group delete --name $RESOURCE_GROUP --yes --no-wait

0 commit comments

Comments
 (0)