I am having a difficult time with the Networking between my Azure Container App (ACA) and Azure Storage Account Table.
Overview
I have a python (Flask) app API that is containerized with Docker. I am utilizing Azure Container Registry (ACR) and ACA with the CICD setup on commits to my main branch in GitHub. I recently wanted to implement authentication, so I created an Azure Storage Account Table to verify API keys. This works GREAT locally. It does not work at all from the cloud. I have narrowed it down to a networking error which I will elaborate on. Below I will provide what I'm doing locally as well as in the cloud in the hopes that you can help me troubleshoot.
Local (working)
Building the Docker Image
docker build -t testapp_${env}:${VERSION} --platform linux/amd64 .
Running the Docker image with the table connection string
docker run -p 8000:80 -e AZURE_TABLE_CONN_STRING='hardcodingfortesting' testapp_${env}:${VERSION}
POST successfully
curl -X POST http://localhost:8000/parse-report \
-H "X-API-KEY: 123" \
-H "Content-Type: application/json" \
The logic works great I can alter the API key and it will tell me it does not match.
Cloud (not working)
Now the exact same package is being pushed to ACR and ACA and I get an error when I try to post.
POST unsuccessful
curl -X POST https://removingforsecurity.io \
-H "X-API-KEY: 123" \
-H "Content-Type: application/json" \
I know with a great deal of certainty that this is a networking issue. This is because in the Networking tab of my Storage Account if I change Public Network Access to Enabled from all networks I can successfully POST.
Error log in ACA after attempting POST:
Content: {"odata.error":{"code":"AuthorizationFailure","message":{"lang":"en-US","value":"This request is not authorized to perform this operation.\nRequestId:9e37b393-b002-006b-1217-64ed0a000000\nTime:2024-02-20T16:08:39.0510443Z"}}}
I have tried the following to resolve this issue:
- IAM settings the managed identity of the ACA to Storage Table Data Contributor. I tried this at the subscription level and at the resource level for the specific storage account. I waited hours to be sure this was not a timing issue.
- Got the outbound IP Address from the ACA and added it to the storage account Firewall.
- Storage account networking attempted to change from Microsoft network routing to internet routing.
Please let me know what I should do.
EDIT: Adding Dockerfile
# Python runtime image base
FROM python:3.10-slim
# Set the working directory in the container
WORKDIR /app
# Ensure requirements are copied
COPY requirements.txt /app/
# Install requirements
RUN pip install --no-cache-dir -r requirements.txt
# Copy application
COPY . /app
# Where to find application instance
ENV FLASK_APP=main_parser.py
# Expose port
EXPOSE 80
# Run command
CMD ["gunicorn", "-w", "4", "-b", "0.0.0.0:80", "main_parser:app"]
The authorization issue between your Azure Container App (ACA) and Azure Storage Account Table may be due to IP address restrictions or Managed Identity permissions read access roles.
Guide on using storage mounts in Azure Container Apps
I followed DOC to Deploy a Flask Web App as a Container in Azure Container Apps .
Connect to an Azure File Share via a Private Endpoint.
Set up a private endpoint with a private link for Azure Container Registry. If you are using a private link, make sure to run Azure storage and the Azure container on the same private endpoint.
The below Flask code with Azure Storage Table display, which is deployed to Azure Container Apps:
app .py:
hello.html:
index.html:
requirements.txt:
Dockerfile:
.dockerignore
Local:
build a Docker image from a Dockerfile.
docker build --tag flask-demo1 .docker tag 8xxxxxe63 sampath344.azurecr.io/samapth-app/flask-demo:v2az acr logincommand, and push a Docker image to an Azure Container Registry (ACR).Azure: