Skip to content

Commit 8b8b1c8

Browse files
authored
Merge pull request #210912 from mumian/0912-vs-extension
0912 vs extension
2 parents 154b152 + 7e8666d commit 8b8b1c8

File tree

8 files changed

+254
-0
lines changed

8 files changed

+254
-0
lines changed
33.6 KB
Loading
36.9 KB
Loading
12.7 KB
Loading
21.2 KB
Loading
Loading
66 KB
Loading
Lines changed: 252 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,252 @@
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: 09/12/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+
- Bicep file deployment requires either the latest [Azure CLI](/cli/azure/) or the latest [Azure PowerShell module](/powershell/azure/new-azureps-module-az).
20+
21+
## Add resource snippet
22+
23+
Launch Visual Studio and create a new file named **main.bicep**.
24+
25+
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.
26+
27+
In *main.bicep*, type **vnet**. Select **res-vnet** from the list, and then press **[TAB]** or **[ENTER]**.
28+
29+
:::image type="content" source="./media/quickstart-create-bicep-use-visual-studio/add-snippet.png" alt-text="Screenshot of adding snippet for virtual network.":::
30+
31+
> [!TIP]
32+
> 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.
33+
34+
Your Bicep file now contains the following code:
35+
36+
```bicep
37+
resource virtualNetwork 'Microsoft.Network/virtualNetworks@2019-11-01' = {
38+
name: 'name'
39+
location: location
40+
properties: {
41+
addressSpace: {
42+
addressPrefixes: [
43+
'10.0.0.0/16'
44+
]
45+
}
46+
subnets: [
47+
{
48+
name: 'Subnet-1'
49+
properties: {
50+
addressPrefix: '10.0.0.0/24'
51+
}
52+
}
53+
{
54+
name: 'Subnet-2'
55+
properties: {
56+
addressPrefix: '10.0.1.0/24'
57+
}
58+
}
59+
]
60+
}
61+
}
62+
```
63+
64+
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`.
65+
66+
```bicep
67+
name: 'exampleVnet'
68+
```
69+
70+
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.
71+
72+
## Add parameters
73+
74+
Now, we'll add two parameters for the storage account name and the location. At the top of file, add:
75+
76+
```bicep
77+
param storageName
78+
```
79+
80+
When you add a space after **storageName**, notice that intellisense offers the data types that are available for the parameter. Select **string**.
81+
82+
:::image type="content" source="./media/quickstart-create-bicep-use-visual-studio/add-param.png" alt-text="Screenshot of adding string type to parameter.":::
83+
84+
You have the following parameter:
85+
86+
```bicep
87+
param storageName string
88+
```
89+
90+
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.
91+
92+
Add a line above the parameter, and type **@**. You see the available decorators. Notice there are decorators for both **minLength** and **maxLength**.
93+
94+
:::image type="content" source="./media/quickstart-create-bicep-use-visual-studio/add-decorators.png" alt-text="Screenshot of adding decorators to parameter.":::
95+
96+
Add both decorators and specify the character limits, as shown below:
97+
98+
```bicep
99+
@minLength(3)
100+
@maxLength(24)
101+
param storageName string
102+
```
103+
104+
You can also add a description for the parameter. Include information that helps people deploying the Bicep file understand the value to provide.
105+
106+
```bicep
107+
@minLength(3)
108+
@maxLength(24)
109+
@description('Provide a name for the storage account. Use only lower case letters and numbers. The name must be unique across Azure.')
110+
param storageName string
111+
```
112+
113+
The storage account name parameter is ready to use.
114+
115+
Add another location parameter:
116+
117+
```bicep
118+
param location string = resourceGroup().location
119+
```
120+
121+
## Add resource
122+
123+
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.
124+
125+
To define a resource, use the `resource` keyword. Below your virtual network, type **resource exampleStorage**:
126+
127+
```bicep
128+
resource exampleStorage
129+
```
130+
131+
**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.
132+
133+
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.
134+
135+
:::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.":::
136+
137+
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.
138+
139+
:::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.":::
140+
141+
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**.
142+
143+
:::image type="content" source="./media/quickstart-create-bicep-use-visual-studio/select-required-properties.png" alt-text="Screenshot of adding required properties.":::
144+
145+
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:
146+
147+
```bicep
148+
resource exampleStorage 'Microsoft.Storage/storageAccounts@2021-09-01' = {
149+
name: 1
150+
location: 2
151+
sku: {
152+
name: 3
153+
}
154+
kind: 4
155+
}
156+
```
157+
158+
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.
159+
160+
When you've finished, you have:
161+
162+
```bicep
163+
@minLength(3)
164+
@maxLength(24)
165+
@description('Provide a name for the storage account. Use only lower case letters and numbers. The name must be unique across Azure.')
166+
param storageName string
167+
param location string = resourceGroup().location
168+
169+
resource virtualNetwork 'Microsoft.Network/virtualNetworks@2019-11-01' = {
170+
name: storageName
171+
location: location
172+
properties: {
173+
addressSpace: {
174+
addressPrefixes: [
175+
'10.0.0.0/16'
176+
]
177+
}
178+
subnets: [
179+
{
180+
name: 'Subnet-1'
181+
properties: {
182+
addressPrefix: '10.0.0.0/24'
183+
}
184+
}
185+
{
186+
name: 'Subnet-2'
187+
properties: {
188+
addressPrefix: '10.0.1.0/24'
189+
}
190+
}
191+
]
192+
}
193+
}
194+
195+
resource exampleStorage 'Microsoft.Storage/storageAccounts@2021-09-01' = {
196+
name: storageName
197+
location: location
198+
sku: {
199+
name: 'Standard_LRS'
200+
}
201+
kind: 'StorageV2'
202+
}
203+
```
204+
205+
For more information about the Bicep syntax, see [Bicep structure](./file.md).
206+
207+
## Deploy the Bicep file
208+
209+
Bicep file deployment can't be done from Visual Studio yet. You can deploy the Bicep file by using Azure CLI or Azure PowerShell:
210+
211+
# [CLI](#tab/CLI)
212+
213+
```azurecli
214+
az group create --name exampleRG --location eastus
215+
216+
az deployment group create --resource-group exampleRG --template-file main.bicep --parameters storageName=uniquename
217+
```
218+
219+
# [PowerShell](#tab/PowerShell)
220+
221+
```azurepowershell
222+
New-AzResourceGroup -Name exampleRG -Location eastus
223+
224+
New-AzResourceGroupDeployment -ResourceGroupName exampleRG -TemplateFile ./main.bicep -storageName "uniquename"
225+
```
226+
227+
---
228+
229+
When the deployment finishes, you should see a message indicating the deployment succeeded.
230+
231+
## Clean up resources
232+
233+
When the Azure resources are no longer needed, use the Azure CLI or Azure PowerShell module to delete the quickstart resource group.
234+
235+
# [CLI](#tab/CLI)
236+
237+
```azurecli
238+
az group delete --name exampleRG
239+
```
240+
241+
# [PowerShell](#tab/PowerShell)
242+
243+
```azurepowershell
244+
Remove-AzResourceGroup -Name exampleRG
245+
```
246+
247+
---
248+
249+
## Next steps
250+
251+
> [!div class="nextstepaction"]
252+
> [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)