Skip to content

Commit eaee3a3

Browse files
update bicep deployment
1 parent 79a3bb0 commit eaee3a3

File tree

1 file changed

+68
-86
lines changed

1 file changed

+68
-86
lines changed

articles/static-web-apps/publish-bicep.md

Lines changed: 68 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -5,75 +5,62 @@ services: static-web-apps
55
author: craigshoemaker
66
ms.service: azure-static-web-apps
77
ms.topic: how-to
8-
ms.date: 08/13/2024
8+
ms.date: 08/28/2024
99
ms.author: cshoe
1010
#customer intent: As a developer, I want create a Static Web App on Azure with a Bicep file so that the process can to automated.
1111
---
1212

1313
# Deploy Azure Static Web Apps with Bicep
1414

15-
Use a Bicep file to create your Azure Static Web Apps resource. Bicep provides a declarative syntax to define and create Azure resources, which can be automated and repeated for consistency.
15+
You can use a Bicep file to create an instance of Azure Static Web Apps. Bicep provides a declarative syntax to define and create Azure resources, which can be automated and repeated for consistency.
1616

17-
This article details how to create the resource group that holds all the resources for your application, the Azure Static Web Apps resource, which contains your statically generated client application such as React, Vue, or Svelte, and the linked Azure Functions backend.
17+
The steps in this article show you how to use Bicep to create a resource group and a Static Web Apps instance. After your static web app is created you still need to deploy your code using the typical methods of GitHub Actions, or using Azure Pipelines.
1818

1919
## Tools for resource creation
2020

21-
Bicep is one of several tools of resource creation. These tools include:
21+
Bicep is one of several tools you can use to create resources in Azure. These tools include:
2222

23-
* [Azure Resource Management](/azure/azure-resource-manager/) (ARM): To implement infrastructure as code for your Azure solutions, use Azure Resource Manager templates (ARM templates). The template is a JavaScript Object Notation (JSON) file that defines the infrastructure and configuration for your project. This older style is still in use.
24-
* [Bicep](/azure/azure-resource-manager/Bicep/): Bicep is a domain-specific language (DSL) that uses declarative syntax to deploy Azure resources. Bicep provides concise syntax, reliable type safety, and support for code reuse.
25-
* [Azure verified modules (AVM)](https://azure.github.io/Azure-Verified-Modules): These modules represent the only standard from Microsoft for Bicep modules in the [Bicep Public Registry](https://github.com/Azure/bicep-registry-modules/tree/main/avm). Use AVM when possible because it consolidates and set the standards for what a good infrastructure as code module looks like.
26-
* [Azure CLI](/cli/azure/)/[PowerShell](/powershell): These command line apps allow you to create resources. They have generally been superseded by Bicep and AVM but are still used for minor or quick fixes while the larger Bicep update may be more time-consuming. Learn to [create resources with the Azure CLI and a Bicep file](/azure/azure-resource-manager/bicep/deploy-cli#deploy-local-bicep-file).
27-
* [Azure portal](https://portal.azure.com/): Azure portal is a web-based visual interface for resource creation and configuration.
23+
| Tool | Description |
24+
|---|---|
25+
| [Azure Resource Manager (ARM)](/azure/azure-resource-manager/) | To implement infrastructure as code for your Azure solutions, use Azure Resource Manager templates (ARM templates). The template is a JavaScript Object Notation (JSON) file that defines the infrastructure and configuration for your project. This older style is still in use. |
26+
| [Bicep](/azure/azure-resource-manager/Bicep/) | Bicep is a domain-specific language (DSL) that uses declarative syntax to deploy Azure resources. Bicep provides concise syntax, reliable type safety, and support for code reuse. |
27+
| [Azure verified modules (AVM)](https://azure.github.io/Azure-Verified-Modules) | These modules represent the only standard from Microsoft for Bicep modules in the [Bicep Public Registry](https://github.com/Azure/bicep-registry-modules/tree/main/avm). Use AVM when possible because it consolidates and set the standards for what a good infrastructure as code module looks like. |
28+
| [Azure CLI](/cli/azure/)/[PowerShell](/powershell) | These command line apps allow you to create resources. Bicep and AVM are often used in favor of these tools, but are still used for minor or quick fixes while the larger Bicep update might be more time-consuming. Learn to [create resources with the Azure CLI and a Bicep file](/azure/azure-resource-manager/bicep/deploy-cli#deploy-local-bicep-file). |
29+
| [Azure portal](https://portal.azure.com/) | Azure portal is a web-based visual interface for resource creation and configuration. |
2830

29-
Creation and configuration can be done across all the tools listed above.
31+
You can create and configure resources using any of the listed tools.
3032

31-
## Bicep by example
33+
## Verified modules
3234

33-
The bicep examples in this article use [Azure Verified Modules (AVM)](https://azure.github.io/Azure-Verified-Modules/) when possible and [bicep](/azure/azure-resource-manager/bicep/) when AVM isn't available. AVM modules are recognizable because they reference modules that include `avm/res`, such as `br/public:avm/res/web/static-site:0.3.0`.
35+
The Bicep examples in this article use [Azure Verified Modules (AVM)](https://azure.github.io/Azure-Verified-Modules/) when possible and [Bicep](/azure/azure-resource-manager/bicep/) when AVM isn't available. AVM modules are recognizable because they reference modules that include `avm/res`, such as `br/public:avm/res/web/static-site:0.3.0`.
3436

35-
```Bicep
36-
module swa 'br/public:avm/res/web/static-site:0.3.0' = {
37-
name: 'client'
38-
scope: rg
39-
params: {
40-
name: 'swa-${resourceToken}'
41-
location: location
42-
sku: 'Free'
43-
tags: union(tags, { 'service-name': 'client' })
44-
}
45-
}
46-
```
47-
48-
AVM allows you to use managed Bicep code, which has been built and is maintained by professional engineers fluent in Bicep. These modules aren't only supported and maintained, they're opinionated about what proper Bicep files look like.
49-
50-
Due to the work involved in owning and maintaining the AVM files, it takes time to specify the module, determine best practices, and find the appropriate owner/maintainer. For this reason, the module you need may not be available at this time.
51-
52-
If the AVM isn't available, you can use _vanilla_ [Bicep for your resources](/azure/templates/).
37+
Using a verified module allows you to use opinionated managed Bicep code maintained by professional engineers fluent in Bicep. Since verified modules require support and dedicated attention, sometimes a module you need might not be available. If an AVM isn't available, you can code your [Bicep files](/azure/templates/) by hand.
5338

5439
## Prerequisites
5540

56-
- [Bicep tools](../azure-resource-manager/Bicep/install.md): Learn how to install Bicep tools.
41+
- [Bicep tools](../azure-resource-manager/Bicep/install.md): Learn how to install Bicep tools.
5742
- [Visual Studio Code extension for Bicep](https://marketplace.visualstudio.com/items?itemName=ms-azuretools.vscode-bicep): An optional extension that creates resources for you by running your Bicep file.
5843

5944
## Create a static web app resource
6045

61-
Create a file named `main.bicep` file and paste in the following code:
46+
The following code creates a new resource group and a Static Web Apps resource and then outputs the default host name and static web app name.
47+
48+
Create a file named `main.bicep` file and paste in the following code.
49+
50+
Before you run this code, make sure to replace the `<REGION_NAME>` placeholder with your region name.
6251

6352
```Bicep
6453
targetScope = 'subscription'
6554
66-
@description('The name of the Azure region that will be used for the deployment.')
67-
param location string ='eastus2'
55+
param location string ='<REGION_NAME>'
6856
69-
@description('Random string to make resource names unique')
57+
@description('String to make resource names unique')
7058
var resourceToken = uniqueString(subscription().subscriptionId, location)
7159
7260
@description('Create a resource group')
7361
resource rg 'Microsoft.Resources/resourceGroups@2024-03-01' = {
7462
name: 'rg-swa-app-${resourceToken}'
7563
location: location
76-
tags: tags
7764
}
7865
7966
@description('Create a static web app')
@@ -87,99 +74,94 @@ module swa 'br/public:avm/res/web/static-site:0.3.0' = {
8774
}
8875
}
8976
90-
@description('Put the default hostname in an output')
77+
@description('Output the default hostname')
9178
output endpoint string = swa.outputs.defaultHostname
9279
93-
@description('Save the static web app name in an output')
80+
@description('Output the static web app name')
9481
output staticWebAppName string = swa.outputs.name
9582
```
9683

97-
This file creates the following resources for you:
98-
* An application name as the `resourceToken` value
99-
* Tags associated with your app to help you find and filter resources in the Azure portal
100-
* A resource group for this application
101-
* A static web app
84+
This code:
10285

103-
Save the values of the output variables to a text editor. You'll need these to find and configure the resources in the Azure portal. The next step is to include a linked backend Azure Functions app, shown in the next section.
86+
- Scopes the action to the current Azure subscription.
87+
- Generates a unique string to ensure the static web app name is globally unique.
88+
- Creates a new resource group.
89+
- Creates a new Static Web Apps instance using the free tier.
90+
- Outputs the web app's URL.
91+
- Outputs the static web app name.
10492

105-
## Link a Functions app
93+
After you execute this file, save the values of the output variables to a text editor.
10694

107-
To link a Functions app for your backend, use the Static Web Apps standard plan for your web app and complete the following steps.
95+
See the next section if you want to link an existing Azure Functions app to your static web app.
10896

109-
To create the Azure Function app, follow the instructions provided in the [Quickstart: Create and deploy Azure Functions resources using Bicep](/azure/azure-functions/functions-create-first-function-bicep) guide. When you're done creating your resource, you'll need the resourceId for the Function app, which looks like: `/subscriptions/<SUBSCRIPTION-ID>/resourcegroups/<RESOURCE-GROUP-NAME>/providers/Microsoft.Web/sites/<FUNCTION-APP-NAME>`, in order to link that function app to your static web app.
97+
## Link a Functions app
11098

111-
Next, create the static web app using the Bicep template provided in the previous section. This sets up the necessary resources for your static web app. Finally, use the following Bicep file to link the static web app to the function app to enable seamless integration between your front-end and back-end services.
99+
The code in the previous section demonstrates how to create a static web app using the free tier. If you want to link a managed backend to your static web app, you need to change to the [Standard hosting plan](/azure/static-web-apps/plans).
112100

113-
Create a file named `config.bicep` file and paste in the following code:
101+
To link your Functions app to your static web app, you need the `resourceId` of your Functions App. You can get this value from the Azure portal, or you can use the following command to return your Functions app `resourceId`.
114102

115-
```Bicep
116-
targetScope = 'resourceGroup'
117-
118-
@description('The name of the Azure region that will be used for the deployment.')
119-
param location string = 'eastus2'
103+
```bash
104+
az functionapp show -n <FUNCTION-APP-NAME> -g <RESOURCE-GROUP-NAME> --query id --output tsv
105+
```
120106

121-
@description('The Subscription ID')
122-
param subscriptionId string = '<SUBSCRIPTION-ID>'
107+
Create a file named `config.bicep` file and paste in the following code.
123108

124-
@description('The name of the Azure resource group.')
125-
param resourceGroup string = '<RESOURCE-GROUP-NAME>'
109+
Before you run this code, make sure to replace the placeholders surrounded by `<>` with your values.
126110

127-
@description('Azure Statoc web app name')
128-
param staticWebAppName string = '<STATIC-WEB-APP-NAME>'
111+
```Bicep
112+
targetScope = 'resourceGroup'
129113
130-
@description('Azure Function App name')
131-
param functionAppName string = '<FUNCTION-APP-NAME>'
114+
param location string = '<REGION_NAME>'
115+
param staticWebAppName string = '<STATIC_WEB_APP_NAME>'
116+
param functionsAppResourceId = '<FUNCTIONS_APP_RESOURCE_ID>'
132117
133-
@description('Get reference to static web app')
118+
@description('Get reference to the static web app')
134119
resource staticWebApp 'Microsoft.Web/staticSites@2023-12-01' existing = {
135120
name: staticWebAppName
136121
}
137122
138-
param functionAppResourceId string = '/subscriptions/${subscriptionId}/resourceGroups/${resourceGroup}/providers/Microsoft.Web/sites/${functionAppName}'
139-
140123
@description('Link backend to static web app')
141124
resource linkedStaticWebAppBackend 'Microsoft.Web/staticSites/linkedBackends@2023-12-01' = {
142125
parent: staticWebApp
143126
name: 'linkedBackend'
144127
properties: {
145-
backendResourceId: functionAppResourceId
128+
backendResourceId: functionsAppResourceId
146129
region: location
147130
}
148131
}
149132
```
150133

151-
This file handles the following tasks:
134+
This code:
152135

153-
* Creates variables to use in resource configuration.
154-
* Create a reference to the existing static web app.
155-
* Create a reference string for the existing functions app.
156-
* Configure the static web app to link to the functions app.
136+
- Scopes the action to the Azure resource group.
137+
- Gets access to the existing static web app by name.
138+
- Links the static web app to a Functions app using the Functions app `resourceId`.
157139

158140
## Deployment
159141

160-
Deploy your source code to the static web app with one of the following tools:
161-
* [Azure Developer CLI (Recommended)](/azure/developer/azure-developer-cli)
162-
* [Static Web Apps CLI](https://github.com/Azure/static-web-apps-cli)
163-
* [GitHub Action](https://docs.github.com/actions)
164-
* [Azure DevOps](/azure/devops/pipelines/overview-azure)
142+
Now that your Azure Static Web Apps instance is created, you can deploy your source code to the static web app with one of the following tools:
143+
144+
- [Azure Developer CLI (Recommended)](/azure/developer/azure-developer-cli)
145+
- [Static Web Apps CLI](https://github.com/Azure/static-web-apps-cli)
146+
- [GitHub Action](https://docs.github.com/actions)
147+
- [Azure Pipelines](/azure/devops/pipelines/overview-azure)
165148

166-
* **Local development environment**: Use [Azure Developer CLI](/azure/developer/azure-developer-cli) to deploy from your local machine. When running locally, you define your deployment in an `azure.yml` file. This file includes hooks that plug into the resource creation process at any point to help you during deployment, especially when different parts of your app need to know about each other at build time.
149+
- **Local development environment**: Use [Azure Developer CLI](/azure/developer/azure-developer-cli) to deploy from your local machine. When running locally, you define your deployment in an `azure.yml` file. This file includes hooks that plug into the resource creation process at any point. These extensibility points help you during deployment, especially when different parts of your app need to know about each other at build time.
167150

168-
* **Production environment**: The ability to deploy from a GitHub Actions workflow file is a built-in feature when you create your static web app. Once the file is in your repository, you can edit the file as needed. Deployment from [other source code providers](external-providers.md) is also supported.
151+
- **Production environment**: The ability to deploy from a GitHub Actions workflow file is a built-in feature when you create your static web app. Once the file is in your repository, you can edit the file as needed. Deployment from [other source code providers](external-providers.md) is also supported.
169152

170153
To learn more from a full end-to-end application that includes resource creation and application deployment, see [Serverless AI Chat with RAG using LangChain.js](https://github.com/Azure-Samples/serverless-chat-langchainjs).
171154

172-
## Speeding up deployments with Azure Developer CLI
155+
## Faster deployments with the Azure Developer CLI
173156

174-
Azure Developer CLI (`azd`) uses Bicep files along with deployment configurations to create and provision your application. Since version 1.4, azd checks the Bicep against cloud resources to understand if the underlying infrastructure as code (IaC) state requires updates. If the state hasn't changed, creation and configuration are skipped. Learn more about this [performance improvement](
157+
Azure Developer CLI (`azd`) uses Bicep files along with deployment configurations to create your application. Since version 1.4, `azd` checks the Bicep against cloud resources to determine if the underlying infrastructure as code (IaC) state requires updates. If the state remains unchanged, creation and configuration are skipped. To learn more about this performance improvement, see [azd provision is now faster when there are no infrastructure changes](
175158
https://devblogs.microsoft.com/azure-sdk/azure-developer-cli-azd-october-2023-release/#azd-provision-is-now-faster-when-there-are-no-infrastructure-changes
176159
).
177160

178161
## Related content
179162

180-
* [Awesome AZD](https://azure.github.io/awesome-azd/?tags=swa)
181-
* [Public Bicep Registry](https://github.com/Azure/bicep-registry-modules)
182-
* [Azure Developer CLI](/azure/developer/azure-developer-cli)
183-
* [Static Web Apps CLI](https://github.com/Azure/static-web-apps-cli)
184-
* [GitHub Actions](https://docs.github.com/actions)
185-
163+
- [Awesome AZD](https://azure.github.io/awesome-azd/?tags=swa)
164+
- [Public Bicep Registry](https://github.com/Azure/bicep-registry-modules)
165+
- [Azure Developer CLI](/azure/developer/azure-developer-cli)
166+
- [Static Web Apps CLI](https://github.com/Azure/static-web-apps-cli)
167+
- [GitHub Actions](https://docs.github.com/actions)

0 commit comments

Comments
 (0)