Skip to content

Commit ab45b4b

Browse files
committed
add a Bicep VS extension quickstart
1 parent b6237d2 commit ab45b4b

File tree

2 files changed

+257
-0
lines changed

2 files changed

+257
-0
lines changed
Lines changed: 255 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,255 @@
1+
---
2+
title: Create Bicep files - Visual Studio
3+
description: Use Visual Studio and the Bicep extension to create Bicep files for deploy Azure resources
4+
ms.date: 07/28/2022
5+
ms.topic: quickstart
6+
7+
#Customer intent: As a developer new to Azure deployment, I want to learn how to use Visual Studio to create and edit Bicep files, so I can use them to deploy Azure resources.
8+
---
9+
10+
# Quickstart: Create Bicep files with Visual Studio
11+
12+
This quickstart guides you through the steps to create a [Bicep file](overview.md) with Visual Studio. You'll create a storage account and a virtual network. You'll also learn how the Bicep extension simplifies development by providing type safety, syntax validation, and autocompletion.
13+
14+
## Prerequisites
15+
16+
- Azure Subscription. If you don't have an Azure subscription, [create a free account](https://azure.microsoft.com/free/) before you begin.
17+
- Visual Studio version 17.3.0 preview 3 or newer. See [Visual Studio Preview](https://visualstudio.microsoft.com/vs/preview/).
18+
- Visual Studio Bicep extension. See [Visual Studio Marketplace](https://marketplace.visualstudio.com/).
19+
20+
(*** include a screenshot once the extension is published.)
21+
22+
- Bicep file deployment requires either the latest [Azure CLI](/cli/azure/) or the latest [Azure PowerShell module](/powershell/azure/new-azureps-module-az).
23+
24+
## Add resource snippet
25+
26+
Launch Visual Studio and create a new file named **main.bicep**.
27+
28+
Visual Studio with the Bicep extension simplifies development by providing pre-defined snippets. In this quickstart, you'll add a snippet that creates a virtual network.
29+
30+
In *main.bicep*, type **vnet**. Select **res-vnet** from the list, and then press **[TAB]** or **[ENTER]**.
31+
32+
:::image type="content" source="./media/quickstart-create-bicep-use-visual-studio/add-snippet.png" alt-text="Screenshot of adding snippet for virtual network":::
33+
34+
> [!TIP]
35+
> If you don't see those intellisense options in Visual Studio, make sure you've installed the Bicep extension as specified in [Prerequisites](#prerequisites). If you have installed the extension, give the Bicep language service some time to start after opening your Bicep file. It usually starts quickly, but you will not have intellisense options until it starts.
36+
37+
Your Bicep file now contains the following code:
38+
39+
```bicep
40+
resource virtualNetwork 'Microsoft.Network/virtualNetworks@2019-11-01' = {
41+
name: 'name'
42+
location: location
43+
properties: {
44+
addressSpace: {
45+
addressPrefixes: [
46+
'10.0.0.0/16'
47+
]
48+
}
49+
subnets: [
50+
{
51+
name: 'Subnet-1'
52+
properties: {
53+
addressPrefix: '10.0.0.0/24'
54+
}
55+
}
56+
{
57+
name: 'Subnet-2'
58+
properties: {
59+
addressPrefix: '10.0.1.0/24'
60+
}
61+
}
62+
]
63+
}
64+
}
65+
```
66+
67+
This snippet contains all of the values you need to define a virtual network. However, you can modify this code to meet your requirements. For example, `name` isn't a great name for the virtual network. Change the `name` property to `exampleVnet`.
68+
69+
```bicep
70+
name: 'exampleVnet'
71+
```
72+
73+
Notice **location** has a red curly underline. This indicates a problem. Hover your cursor over **location**. The error message is - *The name "location" doesn't exist in the current context.* We'll create a location parameter in the next section.
74+
75+
## Add parameters
76+
77+
Now, we'll add two parameters for the storage account name and the location. At the top of file, add:
78+
79+
```bicep
80+
param storageName
81+
```
82+
83+
When you add a space after **storageName**, notice that intellisense offers the data types that are available for the parameter. Select **string**.
84+
85+
:::image type="content" source="./media/quickstart-create-bicep-use-visual-studio/add-param.png" alt-text="Screenshot of adding string type to parameter":::
86+
87+
You have the following parameter:
88+
89+
```bicep
90+
param storageName string
91+
```
92+
93+
This parameter works fine, but storage accounts have limits on the length of the name. The name must have at least 3 characters and no more than 24 characters. You can specify those requirements by adding decorators to the parameter.
94+
95+
Add a line above the parameter, and type **@**. You see the available decorators. Notice there are decorators for both **minLength** and **maxLength**.
96+
97+
:::image type="content" source="./media/quickstart-create-bicep-use-visual-studio/add-decorators.png" alt-text="Screenshot of adding decorators to parameter":::
98+
99+
Add both decorators and specify the character limits, as shown below:
100+
101+
```bicep
102+
@minLength(3)
103+
@maxLength(24)
104+
param storageName string
105+
```
106+
107+
You can also add a description for the parameter. Include information that helps people deploying the Bicep file understand the value to provide.
108+
109+
```bicep
110+
@minLength(3)
111+
@maxLength(24)
112+
@description('Provide a name for the storage account. Use only lower case letters and numbers. The name must be unique across Azure.')
113+
param storageName string
114+
```
115+
116+
The storage account name parameter is ready to use.
117+
118+
Add another location parameter:
119+
120+
```bicep
121+
param location string = resourceGroup().location
122+
```
123+
124+
## Add resource
125+
126+
Instead of using a snippet to define the storage account, we'll use intellisense to set the values. Intellisense makes this step much easier than having to manually type the values.
127+
128+
To define a resource, use the `resource` keyword. Below your virtual network, type **resource exampleStorage**:
129+
130+
```bicep
131+
resource exampleStorage
132+
```
133+
134+
**exampleStorage** is a symbolic name for the resource you're deploying. You can use this name to reference the resource in other parts of your Bicep file.
135+
136+
When you add a space after the symbolic name, a list of resource types is displayed. Continue typing **storage** until you can select it from the available options.
137+
138+
:::image type="content" source="./media/quickstart-create-bicep-use-visual-studio/select-resource-type.png" alt-text="Screenshot of selecting storage accounts for resource type":::
139+
140+
After selecting **Microsoft.Storage/storageAccounts**, you're presented with the available API versions. Select **2021-09-01** or the latest API version. We recommend using the latest API version.
141+
142+
:::image type="content" source="./media/quickstart-create-bicep-use-visual-studio/select-api-version.png" alt-text="Screenshot of selecting API version for resource type":::
143+
144+
After the single quote for the resource type, add `=` and a space. You're presented with options for adding properties to the resource. Select **required-properties**.
145+
146+
:::image type="content" source="./media/quickstart-create-bicep-use-visual-studio/select-required-properties.png" alt-text="Screenshot of adding required properties":::
147+
148+
This option adds all of the properties for the resource type that are required for deployment. After selecting this option, your storage account has the following properties:
149+
150+
```bicep
151+
resource exampleStorage 'Microsoft.Storage/storageAccounts@2021-09-01' = {
152+
name: 1
153+
location: 2
154+
sku: {
155+
name: 3
156+
}
157+
kind: 4
158+
}
159+
```
160+
161+
There are four placeholders in the code. Use [TAB] to go through them and enter the values. Again, intellisense helps you. Set `name` to **storageName**, which is the parameter that contains a name for the storage account. For `location`, set it to `location`. When adding SKU name and kind, intellisense presents the valid options.
162+
163+
When you've finished, you have:
164+
165+
```bicep
166+
@minLength(3)
167+
@maxLength(24)
168+
@description('Provide a name for the storage account. Use only lower case letters and numbers. The name must be unique across Azure.')
169+
param storageName string
170+
param location string = resourceGroup().location
171+
172+
resource virtualNetwork 'Microsoft.Network/virtualNetworks@2019-11-01' = {
173+
name: storageName
174+
location: location
175+
properties: {
176+
addressSpace: {
177+
addressPrefixes: [
178+
'10.0.0.0/16'
179+
]
180+
}
181+
subnets: [
182+
{
183+
name: 'Subnet-1'
184+
properties: {
185+
addressPrefix: '10.0.0.0/24'
186+
}
187+
}
188+
{
189+
name: 'Subnet-2'
190+
properties: {
191+
addressPrefix: '10.0.1.0/24'
192+
}
193+
}
194+
]
195+
}
196+
}
197+
198+
resource exampleStorage 'Microsoft.Storage/storageAccounts@2021-09-01' = {
199+
name: storageName
200+
location: location
201+
sku: {
202+
name: 'Standard_LRS'
203+
}
204+
kind: 'StorageV2'
205+
}
206+
```
207+
208+
For more information about the Bicep syntax, see [Bicep structure](./file.md).
209+
210+
## Deploy the Bicep file
211+
212+
Bicep file deployment can't be done from Visual Studio yet. You can deploy the Bicep file by using Azure CLI or Azure PowerShell:
213+
214+
# [CLI](#tab/CLI)
215+
216+
```azurecli
217+
az group create --name exampleRG --location eastus
218+
219+
az deployment group create --resource-group exampleRG --template-file main.bicep --parameters storageName=uniquename
220+
```
221+
222+
# [PowerShell](#tab/PowerShell)
223+
224+
```azurepowershell
225+
New-AzResourceGroup -Name exampleRG -Location eastus
226+
227+
New-AzResourceGroupDeployment -ResourceGroupName exampleRG -TemplateFile ./main.bicep -storageName "uniquename"
228+
```
229+
230+
---
231+
232+
When the deployment finishes, you should see a message indicating the deployment succeeded.
233+
234+
## Clean up resources
235+
236+
When the Azure resources are no longer needed, use the Azure CLI or Azure PowerShell module to delete the quickstart resource group.
237+
238+
# [CLI](#tab/CLI)
239+
240+
```azurecli
241+
az group delete --name exampleRG
242+
```
243+
244+
# [PowerShell](#tab/PowerShell)
245+
246+
```azurepowershell
247+
Remove-AzResourceGroup -Name exampleRG
248+
```
249+
250+
---
251+
252+
## Next steps
253+
254+
> [!div class="nextstepaction"]
255+
> [Bicep in Microsoft Learn](learn-bicep.md)

articles/azure-resource-manager/bicep/toc.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
- name: Create Bicep templates - VS Code
2222
displayname: vscode
2323
href: quickstart-create-bicep-use-visual-studio-code.md
24+
- name: Create Bicep templates - Visual Studio
25+
href: quickstart-create-bicep-use-visual-studio.md
2426
- name: Create template specs
2527
href: quickstart-create-template-specs.md
2628
- name: Use loops

0 commit comments

Comments
 (0)