# Employee Profile app: AWS → Azure (manual setup) ## 1. What we are building | AWS (aws_infra/) | Azure (this guide) | Free? | |-----------------------------|---------------------------------------------|------------------------------------------| | S3 (index.html, scripts.js) | **Static Web Apps**, Free plan | Yes | | API Gateway | **API Management**, Consumption tier | First 1M calls/month free | | Lambda (2 functions) | **Azure Functions**, Flex Consumption (Python) | First 250k executions + 100k GB-s/month free | | DynamoDB | **Cosmos DB** (NoSQL API), Free tier | First 1000 RU/s + 25 GB free (1 account per subscription) | Request flow: `Browser → Static Web App (page) → APIM (/employees) → Function → Cosmos DB` Code in this repo: - [profile.html](profile.html), [scripts.js](scripts.js): the front end (S3 files, updated). - [azure_functions/function_app.py](azure_functions/function_app.py): GET and POST in one file (replaces `getEmployee.py` and `insertEmployeeData.py`). What changed from the AWS code, and why: - `boto3` is replaced by the `azure-cosmos` library. - Cosmos DB requires every item to have a field named `id`. We set `id = empId`. Saving the same empId twice overwrites the item, same as DynamoDB `put_item`. - Cosmos DB needs a **partition key**, the field it uses to spread data around. We use `/empId`, like DynamoDB's partition key. - `scripts.js` now sends GET and POST to `/employees`, and table cells are filled with `.text()` instead of string concatenation (prevents script injection). - jQuery was loaded after `scripts.js`; the order is fixed. ## 2. Prerequisites - An Azure subscription (a free account is fine). - Tools: `az` (Azure CLI) and `func` (Functions Core Tools) are installed on your Mac. Run `az login` once. - **Python 3.11** for local testing. Your default Python is 3.14, which Azure Functions does not support. Install 3.11 with `brew install python@3.11`. - Pick ONE region for everything (for example `eastus`) so latency and data-transfer costs stay low. Below, replace names in ``. Storage, Function and APIM names must be globally unique. > The steps use the Azure Portal (https://portal.azure.com) because you are learning. The `az` command for each step is shown too; it is what Terraform will model later. ## 3. Step 1: Resource group A resource group is a folder that holds everything for this project. Deleting it later removes everything, so cleanup is one click. Portal: **Resource groups → Create** → name `rg-employee-app`, region `East US` → **Review + create**. ```bash az group create --name rg-employee-app --location eastus ``` ## 4. Step 2: Cosmos DB (replaces DynamoDB) Portal: **Create a resource → Azure Cosmos DB → Azure Cosmos DB for NoSQL → Create**. - Resource group: `rg-employee-app` - Account name: `` (unique) - Location: same region - **Capacity mode: Provisioned throughput** - **Apply Free Tier Discount: Apply** (shown only if you have not used it in this subscription) - Leave the rest as default → **Review + create**. This takes about 5 minutes. Why provisioned and not serverless? The free tier only works with provisioned throughput. It gives 1000 RU/s free, and we will use 400. If the Apply option is greyed out, another account in the subscription already uses the free tier. Choose **Serverless** instead; at this scale it costs cents. Then create the database and container: 1. Open the account → **Data Explorer → New Container**. 2. Database id: **Create new** → `employeesdb`. Leave "Provision database throughput" **unchecked**. 3. Container id: `employeeProfile` 4. Partition key: `/empId` 5. Container throughput: **Manual**, `400` RU/s (the minimum, within the free grant). **OK**. Copy the connection string: account → **Settings → Keys → Primary Connection String**. Treat it like a password. ## 5. Step 3: Function App (replaces Lambda) Portal: **Create a resource → Function App → Flex Consumption → Create**. - Resource group: `rg-employee-app`; name ``; region: same - Runtime stack: **Python**, version **3.11** - Instance size: **2048 MB** (default) - On the Storage tab keep the default (a storage account is created; it costs a few cents a month) - On the Monitoring tab, **Application Insights: No** (saves money; turn on later if you need logs) - **Review + create** Flex Consumption scales to zero when idle, so you pay nothing when nobody uses the app. Do not set "Always ready instances". Add the secret as an app setting: Function App → **Settings → Environment variables → App settings → Add**: - Name `COSMOS_CONNECTION_STRING`, value = the string from Step 2 → **Apply**, then **Confirm**. ## 6. Step 4: Deploy and test the function Optional local test first: ```bash cd azure_functions python3.11 -m venv .venv && source .venv/bin/activate pip install -r requirements.txt cp local.settings.json.sample local.settings.json # then paste the connection string into it func start # in another terminal: curl -X POST localhost:7071/api/employees -H "Content-Type: application/json" \ -d '{"empId":"1","empFirstName":"Sharath","empLastName":"Holla","empAge":"26"}' curl localhost:7071/api/employees ``` `local.settings.json` is in `.gitignore`; never commit it. Deploy to Azure: ```bash az login cd azure_functions func azure functionapp publish ``` The output lists the function URLs. Test the live function: ```bash curl https://.azurewebsites.net/api/employees ``` (Flex Consumption may use a longer hostname like `-xxxx.-01.azurewebsites.net`. Copy it from the Function App **Overview → URL**.) You should see the Sharath item (or `[]` if you skipped the local test). The first call after idle can take a few seconds (cold start). In Data Explorer (Cosmos), open `employeeProfile → Items` to confirm the data is there. ## 7. Step 5: API Management (replaces API Gateway) Portal: **Create a resource → API Management → Create**. - Resource group `rg-employee-app`; region: same; name `` - Organization name / admin email: anything valid - **Pricing tier: Consumption** (serverless, 1M calls/month free). Do NOT choose Developer/Basic/Standard; they bill by the hour. - **Review + create**. Wait about 5-10 minutes. Add the API: 1. APIM → **APIs → Add API → HTTP** (not "Function App"). 2. Display name `employee-api`; **Web service URL** = `https://.azurewebsites.net/api` (use the real function hostname from Step 4) 3. **API URL suffix**: leave empty. **Create**. Add the two operations (like the two API Gateway methods): 1. **Add operation** → Display name `get employees`, verb **GET**, URL `/employees` → Save 2. **Add operation** → Display name `save employee`, verb **POST**, URL `/employees` → Save Turn off the subscription key requirement: API → **Settings** tab → untick **Subscription required** → Save. (Your API Gateway had no key either. The key would otherwise have to be placed in browser JavaScript, where anyone could read it.) Add CORS. Browsers block calls from one domain (your Static Web App) to another (APIM) unless the API allows it. API → **All operations → Inbound processing → (code editor)** and replace the `` section with: ```xml https:// GET POST
Content-Type
``` Find `` in the Portal: Static Web App → **Overview → URL** (for example `yellow-plant-07edff50f.azurestaticapps.net`; no trailing slash). Note: the Consumption tier does not allow `rate-limit-by-key` or `quota-by-key` policies (APIM rejects them), so there is no per-IP limit. To cap cost, keep the Function App's maximum instance count low (Function App → Scale and concurrency) and set an Azure budget alert. Test in APIM: **Test** tab → `get employees` → **Send**. You should get HTTP 200 with the JSON. Your gateway URL is on the APIM **Overview** page: `https://.azure-api.net`. Your endpoint is `https://.azure-api.net/employees`. ## 8. Step 6: Front end (replaces S3) 1. Edit [scripts.js](scripts.js) line 2 and set `API_ENDPOINT` to `https://.azure-api.net/employees`. 2. Commit and push to `main`: ```bash git add index.html profile.html scripts.js azure_functions AZURE_SETUP.md git commit -m "Add employee profile app on Azure" git push ``` 3. Your Azure DevOps pipeline (`azure-static-web-apps-yellow-plant-07edff50f.yml`, `app_location: "/"`) uploads the repo root to the Static Web App. The `azure_functions` folder is ignored because `api_location` is empty, which is intended. 4. Open `https:///profile.html`. Save a profile, then click "View all the Employee Profiles". ## 9. Troubleshooting | Symptom | Likely cause | |---|---| | Browser console shows a CORS error | The origin in the APIM `` policy doesn't exactly match the page URL (check `https://`, no trailing slash) | | `alert("error")` on page, APIM test works | Wrong `API_ENDPOINT`, or the CORS issue above (press F12 → Network tab) | | APIM returns 500/404 | Web service URL wrong or missing `/api`; test the Function URL directly first | | Function returns 500 | `COSMOS_CONNECTION_STRING` missing, or the database/container names differ from `employeesdb` / `employeeProfile` | | Pipeline doesn't pick up the change | Check the build in Azure DevOps Pipelines | ## 10. Cost and cleanup Everything is serverless or free-tier. Expect about a few cents per month, mostly from the Function's storage account. To remove everything: ```bash az group delete --name rg-employee-app ``` (The Static Web App is in its own resource group; delete that too if you want it gone.) ## 11. Next: Terraform After this works, each portal step maps to a Terraform resource: `azurerm_resource_group`, `azurerm_cosmosdb_account` (`free_tier_enabled = true`) with `azurerm_cosmosdb_sql_database` and `azurerm_cosmosdb_sql_container`, `azurerm_storage_account`, `azurerm_function_app_flex_consumption`, `azurerm_api_management` (`sku_name = "Consumption_0"`) with `azurerm_api_management_api` / `_api_operation` / `_api_policy`, and `azurerm_static_web_app`.