Flask API deployed to Azure Container Instances. Secrets are fetched from Azure Key Vault and mounted as environment variables in the container. A CI/CD pipeline has been implemented which enables automatic deployment to ACI. All resources are provisioned with Terraform.
The shell script 'up' allocates Azure resources with Terraform.
The shell script 'down' deallocates Azure resources.
- Run the 'up' script to provision Azure resources.
- Open your browser and navigate to the Azure Portal.
In order to interact with our container registry, we need first need to authenticate ourselves and thus need its associated admin credentials.
Run the following command to receive a list of two passwords. Store the first password for later.
az acr credential show --name hvalfangstcontainerregistry
Run the following command to retrieve the keys associated with your storage account. Store the first key. We will be pushing a secret containing the first key to Azure Key Vault later.
az storage account keys list --resource-group hvalfangstresourcegroup --account-name hvalfangststorageaccount --output json
We will utilize a service principle as identification.
Run the following command to create SP with display name 'hvalfangst'. Make sure to store the JSON response for later use.
az ad sp create-for-rbac --name hvalfangst
Our Service Principal needs access to our resources, role assignment and will be responsible for provision resources. As such, it needs the 'Contributor' role.
Execute the following command to give our SP Contributor access to our subscription.
az role assignment create --assignee {SP_ID} --role Contributor --scope /subscriptions/{YOUR_SUBSCRIPTION}
In order for applications to read secrets from Key Vault on behalf of our SP we need a specific role. Execute the following to give our SP the 'get secrets' role. The ID of our SP was obtained in step #4.
az keyvault set-policy --name hvalfangstkeyvault --resource-group hvalfangstresourcegroup --secret-permissions get --spn {SP_ID}
In order for our container to be able to communicate with Azure Table Storage, it needs an access key. Since we are mounting environment variables onto the container from Azure Key Vault, we need to first store the desired secret.
Execute the following command to create a new secret in Azure Key Vault with name set to 'storage-account-key' and value set to our storage account access key retrieved in step #4.
az keyvault secret set --name storage-account-key --value {ACCESS_KEY} --vault-name hvalfangstkeyvault
- Open the 'Settings' tab of your GitHub repository.
- Click on 'Actions' under 'Security' -> 'Secrets and variables'.
- Create the following repository secrets:
- ACR_USERNAME: hvalfangstcontainerregistry
- ACR_PASSWORD: Value stored in step #3
- AZURE_CLIENT_ID: Value stored in step #4
- AZURE_TENANT_ID: Value stored in step #4
- AZURE_CLIENT_SECRET: Value stored in step #4
A GitHub Actions Workflow has been integrated with this repository. Once triggered, it will:
- Login to Azure utilizing our Service Principal.
- Get secret 'storage-account-key' from Azure Key Vault.
- Login to Azure Container Registry with our basic credentials.
- Build our docker image with the 'storage-account-key' key mounted as environment variable.
- Deploy the image to Azure Container Instances.