Skip to content

Commit 48c2fc0

Browse files
committed
Updated resource provisioning script with creation of app registration for our function app, where the client id is automatically set in the appsettings of said function using config set
1 parent 988bebd commit 48c2fc0

File tree

3 files changed

+56
-32
lines changed

3 files changed

+56
-32
lines changed

README.md

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
# Static web app invoking Azure functions
22

3-
Static web app built with the React framework. The [application](client/src/App.js) allows users to upload CSV files to a storage blob via an HTTP-triggered function.
4-
The uploaded files are then processed by a blob-triggered function, which stores the results in a separate container. Aforementioned functions
5-
are present in the [function_app.py](hvalfangst_function/function_app.py) python script - which is the main entrypoint of our Azure Function App instance.
3+
This [static web app](client/src/App.js), built with the React framework, enables users to upload CSV files containing demographic and financial data about individuals to a designated storage blob (**in**) via an HTTP-triggered Azure Function.
4+
Once uploaded, a blob-triggered Azure Function processes the files to calculate correlations between various variables, such as experience, state, gender, and income. The computed statistics are then stored in a separate storage blob (**out**).
5+
These functions are implemented in the python script [function_app.py](hvalfangst_function/function_app.py); which is the main entrypoint of our Azure Function App instance.
66

7-
A pipeline has been set up to deploy the function app and the static web app to Azure using GitHub Actions. The pipeline is triggered by a push to the main branch or by manually running the workflow.
7+
A branch-triggered pipeline has been set up to deploy the function app and the static web app to Azure using a GitHub Actions Workflow [script](.github/workflows/deploy_to_azure.yml). A service principal has been created as part of the resource provisioning script, which is used
8+
to authenticate our requests in said pipeline script. It is therefore important to set the associated GitHub secrets in the repository settings.
89

910

1011

@@ -18,8 +19,8 @@ A pipeline has been set up to deploy the function app and the static web app to
1819

1920
## Allocate resources
2021

21-
The shell script [allocate_resources](infra/allocate_resources.sh) creates Azure resources specified in a
22-
[Bicep](https://learn.microsoft.com/en-us/azure/azure-resource-manager/bicep/overview?tabs=bicep) template [file](infra/main.bicep).
22+
The shell script [allocate_resources](infra/allocate_resources.sh) creates Azure resources using the Azure CLI and a
23+
[Bicep](https://learn.microsoft.com/en-us/azure/azure-resource-manager/bicep/overview?tabs=bicep) template [file](infra/main.bicep).
2324

2425
It will create the following hierarchy of resources:
2526

@@ -41,20 +42,11 @@ graph TD
4142
B -->|Contains| F
4243
```
4344

44-
## Deallocate resources
45-
46-
The shell script [deallocate_resources](infra/deallocate_resources.sh) deletes our Azure resources.
47-
48-
# CI/CD
49-
50-
A CI/CD pipeline for deploying our [Function App](hvalfangst_function/function_app.py) to Azure has been set up using a GitHub Actions workflows [script](.github/workflows/deploy_to_azure.yml). The pipeline is either triggered by a push to the main branch or by manually running the workflow.
51-
In order for the pipeline to work, the following secrets must be set in the repository settings:
52-
53-
![img.png](img.png)
54-
55-
The associated values of the aforementioned secret can be retrieved from the Azure portal, under our deployed Function App.
56-
Click on the **Get publish profile** button and copy/paste the file content into the secret value field.
57-
58-
![img_1.png](img_1.png)
59-
45+
## GitHub secrets
46+
As touched upon earlier, the GitHub secret **AZURE_CREDENTIALS** must be set in the repository settings. This secret comprises a JSON object containing the service principal credentials.
47+
It is generated by the Azure CLI command `az ad sp create-for-rbac`, which
48+
was executed as part of our [resource provisioning script](infra/allocate_resources.sh). The resulting terminal output of said command needs to be copy/pasted to the secret field.
49+
Again, it is used to authenticate our requests in the [GitHub Actions Workflow script](.github/workflows/deploy_to_azure.yml) as contributor access to the resource group is necessary
50+
in order to deploy our function and static web app. There are many ways to do this, but this is obviously a simple example.
6051

52+
![img_2.png](img_2.png)

img_2.png

63.3 KB
Loading

infra/allocate_resources.sh

Lines changed: 42 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
11
#!/bin/bash
22

3-
# Define colors for console output
3+
# Colors for console output
44
GREEN="\e[32m"
55
RED="\e[31m"
66
BLUE="\e[34m"
77
CYAN="\e[36m"
88
YELLOW="\e[33m"
99
RESET="\e[0m"
1010

11-
# Variables
12-
SUBSCRIPTION_ID=$(az account show --query id --output tsv)
11+
# Constants
1312
RESOURCE_GROUP="hvalfangstresourcegroup"
1413
STORAGE_ACCOUNT_NAME="hvalfangststorageaccount"
1514
FUNCTION_APP_NAME="hvalfangstlinuxfunctionapp"
1615
LOCATION="westeurope"
1716
BICEP_FILE="infra/main.bicep"
1817

18+
# Set environment variable to prevent path conversion in MSYS (https://github.com/Azure/azure-cli/blob/dev/doc/use_cli_with_git_bash.md#auto-translation-of-resource-ids)
19+
export MSYS_NO_PATHCONV=1;
20+
1921
# Function to handle errors
2022
handle_error() {
2123
echo -e "${RED}Error occurred in script at line: ${BASH_LINENO[0]}. Exiting...${RESET}"
@@ -25,7 +27,7 @@ handle_error() {
2527
# Set trap to catch errors and execute handle_error
2628
trap 'handle_error' ERR
2729

28-
# Check if logged in to Azure
30+
# Check if you are logged in to Azure
2931
echo -e "${YELLOW}Checking if logged in to Azure...${RESET}"
3032
az account show
3133

@@ -34,9 +36,13 @@ if [ $? -ne 0 ]; then
3436
exit 1
3537
fi
3638

39+
# Variables retrieved from Azure CLI
40+
SUBSCRIPTION_ID=$(az account show --query id --output tsv)
41+
TENANT_ID=$(az account show --query tenantId --output tsv)
42+
3743
# Create Resource Group
38-
echo -e "${YELLOW}Creating resource group $RESOURCE_GROUP in $LOCATION ${RESET}"
39-
az group create --name $RESOURCE_GROUP --location $LOCATION
44+
echo -e "${YELLOW}Creating resource group ${RESOURCE_GROUP} in ${LOCATION} ${RESET}"
45+
az group create --name ${RESOURCE_GROUP} --location ${LOCATION}
4046
if [ $? -ne 0 ]; then
4147
echo -e "${RED}Failed to create resource group.${RESET}"
4248
exit 1
@@ -51,13 +57,15 @@ if [ $? -ne 0 ]; then
5157
exit 1
5258
fi
5359

60+
# Create service principal used by GitHub Actions, the returned JSON is stored as secret in the GitHub repository
5461
echo -e "${YELLOW}Creating service principal...${RESET}"
55-
az ad sp create-for-rbac --name hvalfangst --role contributor --scopes /subscriptions/$SUBSCRIPTION_ID/resourceGroups/$RESOURCE_GROUP --sdk-auth
62+
az ad sp create-for-rbac --name hvalfangst-github-actions-sp --role contributor --scopes /subscriptions/${SUBSCRIPTION_ID}/resourceGroups/${RESOURCE_GROUP}
5663
if [ $? -ne 0 ]; then
5764
echo -e "${RED}Failed to create service principal.${RESET}"
5865
exit 1
5966
fi
6067

68+
# Set up our storage container to serve static website with default index and 404 page
6169
echo -e "${YELLOW}Setting up static website...${RESET}"
6270
az storage blob service-properties update \
6371
--account-name ${STORAGE_ACCOUNT_NAME} \
@@ -69,12 +77,36 @@ if [ $? -ne 0 ]; then
6977
exit 1
7078
fi
7179

80+
# Set up CORS for our Function App, which is used for our HTTP-triggered function
7281
echo -e "${YELLOW}Setting up CORS for function app...${RESET}"
73-
az functionapp cors add --name ${FUNCTION_APP_NAME} --resource-group $RESOURCE_GROUP --allowed-origins http://localhost:3000
74-
az functionapp cors add --name ${FUNCTION_APP_NAME} --resource-group $RESOURCE_GROUP --allowed-origins https://hvalfangststorageaccount.z6.web.core.windows.net
82+
az functionapp cors add --name ${FUNCTION_APP_NAME} --resource-group ${RESOURCE_GROUP} --allowed-origins http://localhost:3000
83+
az functionapp cors add --name ${FUNCTION_APP_NAME} --resource-group ${RESOURCE_GROUP} --allowed-origins https://hvalfangststorageaccount.z6.web.core.windows.net
7584
if [ $? -ne 0 ]; then
7685
echo -e "${RED}Failed to set up CORS for function app.${RESET}"
7786
exit 1
7887
fi
7988

80-
echo -e "${GREEN}All resources have been provisioned.${RESET}"
89+
# Set up app registration for function app
90+
echo -e "${YELLOW}Setting up app registration for function app...${RESET}"
91+
FUNCTION_APP_CLIENT_ID=$(az ad app create \
92+
--display-name "hvalfangst-function-app" \
93+
--query appId -o tsv)
94+
95+
if [ $? -ne 0 ] || [ -z "$FUNCTION_APP_CLIENT_ID" ]; then
96+
echo -e "${RED}Failed to set up app registration or retrieve the app ID.${RESET}"
97+
exit 1
98+
fi
99+
100+
# Set up app settings for the function app
101+
echo -e "${YELLOW}Setting up app settings for function app...${RESET}"
102+
az functionapp config appsettings set \
103+
--name ${FUNCTION_APP_NAME} \
104+
--resource-group ${RESOURCE_GROUP} \
105+
--settings TENANT_ID=${TENANT_ID} FUNCTION_APP_CLIENT_ID=${FUNCTION_APP_CLIENT_ID}
106+
if [ $? -ne 0 ]; then
107+
echo -e "${RED}Failed to set up app settings for function app.${RESET}"
108+
exit 1
109+
fi
110+
111+
112+
echo -e "${GREEN}All resources have been provisioned.${RESET}"

0 commit comments

Comments
 (0)