Skip to content

Commit fd85e9c

Browse files
committed
Created script for creating and deleting Azure resources
1 parent c7f3f4b commit fd85e9c

File tree

4 files changed

+308
-1
lines changed

4 files changed

+308
-1
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/infra/infra_config.env

README.md

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,52 @@
1-
# azure-blob-triggered-event-grid-published-to-service-bus
1+
# Python API integrated with Azure Service Bus Queue
2+
3+
## Requirements
4+
5+
- **Platform**: x86-64, Linux/WSL
6+
- **Programming Language**: [Python 3](https://www.python.org/downloads/)
7+
- **Cloud Account**: [Azure](https://azure.microsoft.com/en-us/pricing/purchase-options/azure-account)
8+
- **Resource provisioning**: [Azure CLI](https://learn.microsoft.com/en-us/cli/azure/)
9+
10+
11+
## Allocate resources
12+
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.
15+
16+
It will create the following hierarchy of resources:
17+
18+
```mermaid
19+
graph TD
20+
A[Subscription]
21+
A --> B[Resource Group]
22+
B --> C[Storage Account]
23+
C --> D[Blob Container]
24+
B --> E[App Service Plan]
25+
E -->|Hosts| G[Function App]
26+
G -->|Uses| F[Application Insights]
27+
28+
A -->|Contains| B
29+
B -->|Contains| C
30+
C -->|Contains| D
31+
B -->|Contains| E
32+
B -->|Contains| F
33+
```
34+
35+
For this script to work it is necessary to have a configuration file named **infra_config.env** in your [infra](infra) directory. It contains sensitive information
36+
such as tenant and subscription id as well as information used to reference resources. The file has been added to our [.gitignore](.gitignore) so that you don't accidentally commit it.
37+
### Structure of 'infra/infra_config.env'
38+
```bash
39+
TENANT_ID={TO_BE_SET_BY_YOU_MY_FRIEND}
40+
SUBSCRIPTION_ID={TO_BE_SET_BY_YOU_MY_FRIEND}
41+
LOCATION=northeurope
42+
RESOURCE_GROUP_NAME=hvalfangstresourcegroup
43+
STORAGE_ACCOUNT_NAME=hvalfangststorageaccount
44+
BLOB_CONTAINER_NAME=hvalfangstblobcontainer
45+
FUNCTION_APP_NAME=hvalfangstfunctionapp
46+
SERVICE_PLAN_NAME=hvalfangstserviceplan
47+
APP_INSIGHTS_NAME=hvalfangstappinsights
48+
```
49+
50+
## Deallocate resources
51+
52+
The shell script [deallocate_resources](infra/deallocate_resources.sh) deletes our Azure service bus queue, namespace and resource group.

infra/allocate_resources.sh

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
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+
# 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 B1 \
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"

infra/deallocate_resources.sh

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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"

0 commit comments

Comments
 (0)