The process for setting up Azure can be tricky if not followed carefully. There are many pitfalls where a wrong selection will cause the application not to start correctly. Typically, if the application starts correctly it is going to work right and any issues you have will be in your code.
This guide has setup and troubleshooting information for getting an Azure App Service and Azure SQL database to work well together. We also use a Static Web App, but those are typically simple to set up.
The following steps are a fast way to get it going, and allow the steps to be done in a logical order. This is not the only way.
Follow the instructions here to get your student credits set up. https://azure.microsoft.com/en-us/free/students/
All instructions assume you are logged into the portal at https://portal.azure.com
- Create a Resource
- Web App
- Subscription should be the student one
- Resource Group: click Create new. Provide a name
- Name the web app
- Runtime stack: .NET 7
- For the OS choose Linux. Windows will also work but has different options once created
- Region: West US (none, 2, or 3)
- Linux Plan: click Create new and provide a name. Probably like the name above. Many apps can run on an App service Plan. To change the pricing plan, navigate to the App Service Plan and choose Scale up and choose another option. App Services run on App Service Plans. You pay for App Service Plans regardless of number of App Services running on it, but they are resourece constrained.
- Pricing Plan: Basic B1
- Click Review+Create
- Click Create
- It will take several minutes to deploy the resources (also Application Insights will be added automatically)
- Navigate to the App Service
- Click Identity on the left side
- Change the System assigned status to On and click Save and then Yes
- Note the object principal Id or at least the first few characters of the GUID
- Click Overview
- Near the center top click Download publish profile
- Paste entire content of this file into the secret for the GitHub action deployment
- Note the Default Domain: This is the URL of the website. Note if this is an API website there will not be a default page unless you specifically create one. Use a public get endpoint to test.
- In the search bar type 'aad' and click Azure Active Directory
- Click Groups on the left
- Click New Group
- Group name: choose a name
- Group description: This group controls access to the Wordle database
- Click no members selected
- Search for your app service name
- Click on the name making sure the GUID matches the one noted above
- Click Select
- Add more members (this will give you access to the database from tools like SSMS)
- Search for you name
- Click your name and click the Select button
- Add any other members necessary (a partner for example)
- Click Create
- Note the GUID of this group
- Click Create Resource
- Click SQL Database
- Subscription: Should be set
- Resource Group: Select the one created above
- Database name: choose a name
- Server: click Create new (Databases run on Database Servers, you pay per database)
- Server name: pick a name, again globally unique
- Location: West US (none, 2 or 3) should be the same region as above to minimize latency.
- For authentication method leave the option for AAD only. You can choose hybrid if you want to use SQL authentication, but it is less secure.
- Click Set admin
- Search for the group created in the last step, click it and then click Select. GUID should match above.
- Click OK
- No elastic pool (default)
- Workload environment: Development
- Compute + Storage: click Configure database (price should be about $5/month)
- Service Tier: Choose Basic
- Click Apply
- Click Review + Create
- Click Create
- Deployment will take a few minutes
- Navigate to the database once it is created
- Click Set server Firewall
- Public network access: Selected Networks
- If you want to access the server from your computer, click Add your client IPv4 address
- Check the Allow Azure services and resources to access this server
- Click Save
- You will be back at the SQL Server level and will need to click SQL Databases and then the database name to navigate back to the actual database
- Navigate to your Azure SQL database and the Overview page (default)
- Next to Connection strings, click Show database connection strings. Copy the first entry.
- Navigate to your App Service
- On the left, choose Configuration
- Near the bottom in the Connection Strings area, click New Connection String
- Name: DefaultConnection All one word
- Value: Paste from above
- Type: SQLAzure
- Click OK
- Click Save
- Click Continue
Now, once you deploy your site to the App Service, it should hook up to the database and you should be able to make API calls.
So, if this doesn't work, here is how to test things out.
- Try connecting with SSMS. The hostname is in the connection string and will look something like this: wordle.database.windows.net. Use one of the AAD auth options in SSMS until it works.
- If you can't get a connection at all, the hostname is wrong or the firewall rule isn't set. Recent versions of SSMS will add the firewall rule for you.
- If you get an authentication error, then you likely have an issue with not setting the admin group on the database, not adding yourself to the AAD security group, or keying in a user name or password wrong.
- You can see the logs for the app service
- Select App Service logs from the left bar
- Click File System and then click save
- Click Log Stream from the left bar
- This will show you the logs from the app. Look for errrors about SQLClient and use similar troubleshooting instructions as above
- Sometimes restarting the app service makes a difference
- Sometimes recreating the database and app service helps eliminate the chance of a misconfigured service.
Getting Azure set up can be tricky for those not accustomed to the environment. Hopefully this guide will help get you onto the right track.