To set up your Azure architecture with MySQL Flexible Server and the two App Services (Node.js backend and React.js frontend), follow these steps:
1. MySQL Flexible Server Setup
- Create MySQL Flexible Server: Use the Azure CLI to create a MySQL Flexible Server instance with public access or within a Virtual Network (VNet) for enhanced security. Ensure to configure firewall rules to allow access from your App Services.
az mysql flexible-server create \ --name <your-mysql-server-name> \ --resource-group <your-resource-group> \ --location <your-location> \ --admin-user <your-mysql-admin-username> \ --admin-password <your-mysql-admin-password> - Firewall Configuration: Set up firewall rules to allow your App Services to connect to the MySQL server. You can do this by creating a firewall rule that allows the IP addresses of your App Services.
2. Backend App Service (Node.js)
- Create App Service: Create your Node.js App Service using the Azure CLI. Make sure to set the runtime to Node.js.
az webapp create \ --resource-group <your-resource-group> \ --plan <your-app-service-plan> \ --name <your-backend-app-name> \ --runtime "NODE|<node-version>" - Environment Variables: Configure environment variables for your database connection in the App Service settings.
az webapp config appsettings set \ --name <your-backend-app-name> \ --resource-group <your-resource-group> \ --settings DB_HOST="<your-mysql-server-name>.mysql.database.azure.com" \ DB_DATABASE="<your-database-name>" \ DB_USERNAME="<your-mysql-admin-username>" \ DB_PASSWORD="<your-mysql-admin-password>"
3. Frontend App Service (React.js)
- Create Frontend App Service: Similarly, create your React.js App Service.
az webapp create \ --resource-group <your-resource-group> \ --plan <your-app-service-plan> \ --name <your-frontend-app-name> \ --runtime "NODE|<node-version>" - CORS Configuration: Set up CORS in your backend to allow requests from your frontend App Service. You can do this in your Node.js application by adding the appropriate headers or using a middleware like
cors.
4. Connecting Both App Services to MySQL Flexible Server
- Ensure that both App Services have the correct connection strings set in their environment variables to connect to the MySQL Flexible Server.
5. Recommended Deployment Steps
- Deploy Backend: Use Local Git or Azure DevOps to deploy your Node.js application.
- Deploy Frontend: Deploy your React.js application similarly, ensuring it can communicate with the backend API.
By following these steps, you should be able to set up your Azure architecture effectively. Make sure to test the connections and configurations after deployment to ensure everything is working as expected.
References: