0
0
Azurecloud~5 mins

Connection from applications in Azure - Commands & Configuration

Choose your learning style9 modes available
Introduction
Applications often need to connect to cloud services like databases or storage. This concept shows how to set up a secure connection from your app to Azure services using connection strings.
When your web app needs to read and write data to an Azure SQL Database.
When a mobile app needs to upload files to Azure Blob Storage.
When a backend service needs to access Azure Cosmos DB for fast data retrieval.
When you want to keep your connection details safe and not hard-code them in your app.
When you want to update connection details without changing your application code.
Commands
This command sets a connection string named MyDbConnection for the Azure Web App named example-webapp. It tells the app how to connect to the Azure SQL Database securely.
Terminal
az webapp config connection-string set --resource-group example-group --name example-webapp --settings MyDbConnection='Server=tcp:example-server.database.windows.net,1433;Database=exampledb;User ID=exampleuser;Password=ExamplePass123;Encrypt=true;' --connection-string-type SQLAzure
Expected OutputExpected
No output (command runs silently)
--resource-group - Specifies the Azure resource group where the web app is located.
--name - Specifies the name of the Azure Web App.
--connection-string-type - Defines the type of connection string, here SQLAzure for Azure SQL Database.
This command lists all connection strings configured for the example-webapp. It helps verify that the connection string was set correctly.
Terminal
az webapp config connection-string list --resource-group example-group --name example-webapp
Expected OutputExpected
{ "MyDbConnection": "Server=tcp:example-server.database.windows.net,1433;Database=exampledb;User ID=exampleuser;Password=ExamplePass123;Encrypt=true;" }
--resource-group - Specifies the Azure resource group.
--name - Specifies the Azure Web App name.
This command restarts the web app to apply the new connection string settings. Restarting ensures the app uses the updated connection details.
Terminal
az webapp restart --resource-group example-group --name example-webapp
Expected OutputExpected
No output (command runs silently)
--resource-group - Specifies the resource group of the web app.
--name - Specifies the web app name.
Key Concept

If you remember nothing else from this pattern, remember: store connection details securely in Azure Web App settings and never hard-code them in your application.

Common Mistakes
Hard-coding the database password directly in the application code.
This exposes sensitive information and makes it hard to update credentials securely.
Use Azure Web App connection strings to store credentials securely and access them via environment variables.
Not restarting the web app after changing connection strings.
The app will continue using old connection details until restarted, causing connection failures.
Always restart the web app after updating connection strings to apply changes.
Using incorrect connection string type flag or format.
Azure won't recognize the connection string properly, causing connection errors.
Use the correct --connection-string-type flag like SQLAzure for Azure SQL Database and verify the string format.
Summary
Use 'az webapp config connection-string set' to securely add connection strings to your Azure Web App.
Verify connection strings with 'az webapp config connection-string list' to ensure they are set correctly.
Restart your web app with 'az webapp restart' to apply new connection settings.