{"id":256,"date":"2023-10-23T14:07:30","date_gmt":"2023-10-23T22:07:30","guid":{"rendered":"https:\/\/www.dumpsterfirecomputing.com\/?p=256"},"modified":"2023-10-23T14:07:31","modified_gmt":"2023-10-23T22:07:31","slug":"bicep-basics-up-and-running-in-10-minutes","status":"publish","type":"post","link":"https:\/\/www.dumpsterfirecomputing.com\/?p=256","title":{"rendered":"BICEP Basics &#8211; Up and Running in 10 Minutes"},"content":{"rendered":"\n<p>Just a quick one on getting started leveraging bicep to deploy resources in Azure since it&#8217;s actually significantly easier than it sounds.  Let&#8217;s get right to it&#8230;<\/p>\n\n\n\n<p><a href=\"https:\/\/learn.microsoft.com\/en-us\/azure\/azure-resource-manager\/bicep\/\">Bicep <\/a>is a declarative language used to deploy resources into Azure.  At its core you have one (or more) file(s) with your bicep code and then something to take that and deploy it.  I like to create a separate PowerShell file as the harness for my bicep &#8211; I can store parameters, string deployments together, etc.<\/p>\n\n\n\n<p>This post is organized into the pre-requisites, a sample bicep file for deploying a Resource Group, the harness, and how to put the two together.  I&#8217;ll wrap it up with a summary which includes information on where to go from here.<\/p>\n\n\n\n<p class=\"has-larger-font-size\"><strong>Pre-Requisites<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/code.visualstudio.com\/Download\">Visual Studio Code<\/a> with both the <a href=\"https:\/\/marketplace.visualstudio.com\/items?itemName=ms-vscode.PowerShell\">PowerShell <\/a>and <a href=\"https:\/\/marketplace.visualstudio.com\/items?itemName=ms-azuretools.vscode-bicep\">Bicep <\/a>extensions<\/li>\n\n\n\n<li><a href=\"https:\/\/portal.azure.com\">Azure Subscription<\/a> that you own (or that you have permission to write to)<\/li>\n<\/ul>\n\n\n\n<p class=\"has-larger-font-size\"><strong>The Bicep File<\/strong><\/p>\n\n\n\n<p>Type the following into a file and save it with a <code>.bicep<\/code> extension.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>targetScope = 'subscription'\r\n\r\nresource NewResourcGroup 'Microsoft.Resources\/resourceGroups@2023-07-01' = {\r\n  name: 'rg-myapplication'\r\n  location: 'westus'\r\n}<\/code><\/pre>\n\n\n\n<p>The first line sets the scope.  Because we&#8217;re building a resource group, this is an item that must be targeted at the subscription scope.  With the VS Code extension for bicep, as you start typing that first line you can see the various options available to you (this context sensitive help is quite useful when working with bicep).<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"288\" height=\"108\" src=\"https:\/\/www.dumpsterfirecomputing.com\/wp-content\/uploads\/2023\/10\/image.png\" alt=\"\" class=\"wp-image-257\"\/><\/figure>\n\n\n\n<p>The next block of code defines the type of resource we want to build and the various parameters.  If you don&#8217;t know the exact formatting for a resource type, the context sensitive help can again be useful here.  Below you can see that I start typing &#8220;resourcegroup&#8221; and the context sensitive help provides the full value (you can press &#8220;tab&#8221; to select \/ complete the line).<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"754\" height=\"134\" src=\"https:\/\/www.dumpsterfirecomputing.com\/wp-content\/uploads\/2023\/10\/image-1.png\" alt=\"\" class=\"wp-image-258\" srcset=\"https:\/\/www.dumpsterfirecomputing.com\/wp-content\/uploads\/2023\/10\/image-1.png 754w, https:\/\/www.dumpsterfirecomputing.com\/wp-content\/uploads\/2023\/10\/image-1-300x53.png 300w\" sizes=\"auto, (max-width: 754px) 100vw, 754px\" \/><\/figure>\n\n\n\n<p>The same context help will pop up when selecting the version of the resource to use.  I tend to always pick the latest.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"650\" height=\"173\" src=\"https:\/\/www.dumpsterfirecomputing.com\/wp-content\/uploads\/2023\/10\/image-2.png\" alt=\"\" class=\"wp-image-259\" srcset=\"https:\/\/www.dumpsterfirecomputing.com\/wp-content\/uploads\/2023\/10\/image-2.png 650w, https:\/\/www.dumpsterfirecomputing.com\/wp-content\/uploads\/2023\/10\/image-2-300x80.png 300w\" sizes=\"auto, (max-width: 650px) 100vw, 650px\" \/><\/figure>\n\n\n\n<p> Then as you begin to finalize the first row you have the option for VS Code to prepopulate the resource with the minimum required properties for the resource in question:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"785\" height=\"157\" src=\"https:\/\/www.dumpsterfirecomputing.com\/wp-content\/uploads\/2023\/10\/image-3.png\" alt=\"\" class=\"wp-image-260\" srcset=\"https:\/\/www.dumpsterfirecomputing.com\/wp-content\/uploads\/2023\/10\/image-3.png 785w, https:\/\/www.dumpsterfirecomputing.com\/wp-content\/uploads\/2023\/10\/image-3-300x60.png 300w, https:\/\/www.dumpsterfirecomputing.com\/wp-content\/uploads\/2023\/10\/image-3-768x154.png 768w\" sizes=\"auto, (max-width: 785px) 100vw, 785px\" \/><\/figure>\n\n\n\n<p>This can be helpful, especially when you begin browsing Microsoft&#8217;s documentation on the various resource types, some of which get pretty complex.<\/p>\n\n\n\n<p class=\"has-larger-font-size\"><strong>The Harness<\/strong><\/p>\n\n\n\n<p>I use a separate PowerShell file as the harness for bicep work.  In this case, the harness file contains a single line (change <code>01-ResourceGroup.bicep<\/code> to whatever you named your bicep file):<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>New-AzSubscriptionDeployment -Location 'westus' -TemplateFile '.\\01-ResourceGroup.bicep'<\/code><\/pre>\n\n\n\n<p>With the harness file saved in the same location as the bicep file above (and make sure your terminal window shows you in that same folder), running it will deploy your new resource group.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"618\" height=\"304\" src=\"https:\/\/www.dumpsterfirecomputing.com\/wp-content\/uploads\/2023\/10\/image-4.png\" alt=\"\" class=\"wp-image-261\" srcset=\"https:\/\/www.dumpsterfirecomputing.com\/wp-content\/uploads\/2023\/10\/image-4.png 618w, https:\/\/www.dumpsterfirecomputing.com\/wp-content\/uploads\/2023\/10\/image-4-300x148.png 300w\" sizes=\"auto, (max-width: 618px) 100vw, 618px\" \/><\/figure>\n\n\n\n<p>This shows a successful deployment, and it can be verified by looking at the Azure Portal for the resource group.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"328\" height=\"165\" src=\"https:\/\/www.dumpsterfirecomputing.com\/wp-content\/uploads\/2023\/10\/image-5.png\" alt=\"\" class=\"wp-image-262\" srcset=\"https:\/\/www.dumpsterfirecomputing.com\/wp-content\/uploads\/2023\/10\/image-5.png 328w, https:\/\/www.dumpsterfirecomputing.com\/wp-content\/uploads\/2023\/10\/image-5-300x151.png 300w\" sizes=\"auto, (max-width: 328px) 100vw, 328px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p class=\"has-larger-font-size\"><strong>Where To Go From Here<\/strong><\/p>\n\n\n\n<p>The above is great for deploying a single resource group, but if you want to build virtual networks, VMs, load balancers, app services, etc. you&#8217;re going to need more information.<\/p>\n\n\n\n<p>First place to go get more information is this <a href=\"https:\/\/learn.microsoft.com\/en-us\/azure\/azure-resource-manager\/templates\/deploy-powershell\">overview from Microsoft<\/a> on deploying resources in bicep.  Pay careful attention to the &#8220;Deployment Scope&#8221; section.  In the above, we targeted the subscription for the scope.  If you target a resource group (which is a bit more common), your harness file should have the command <code>New-AzResourceGroupDeployment<\/code> instead of the <code>New-AzSubscriptionDeployment<\/code> used above.<\/p>\n\n\n\n<p>Second is deciding <em>what <\/em>you want to build.  Microsoft has a massive <a href=\"https:\/\/learn.microsoft.com\/en-us\/azure\/templates\/\">reference library<\/a> for all of Azure&#8217;s resource types.  You simply go down this list to find the resource you want to build and can then read through the required and optional parameters.  Here&#8217;s just a sample from the Virtual Network resource:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"740\" height=\"437\" src=\"https:\/\/www.dumpsterfirecomputing.com\/wp-content\/uploads\/2023\/10\/image-6.png\" alt=\"\" class=\"wp-image-263\" srcset=\"https:\/\/www.dumpsterfirecomputing.com\/wp-content\/uploads\/2023\/10\/image-6.png 740w, https:\/\/www.dumpsterfirecomputing.com\/wp-content\/uploads\/2023\/10\/image-6-300x177.png 300w\" sizes=\"auto, (max-width: 740px) 100vw, 740px\" \/><\/figure>\n\n\n\n<p>The code samples tend to display every possible parameter, which is why it&#8217;s important to look at the required parameters (or let VS Code give them to you).<\/p>\n\n\n\n<p>Lastly, you can string together multiple resources into a single bicep file, reference properties of one resource in another, and even <a href=\"https:\/\/learn.microsoft.com\/en-us\/azure\/azure-resource-manager\/bicep\/existing-resource\">reference existing resources<\/a>. <\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Just a quick one on getting started leveraging bicep to deploy resources in Azure since it&#8217;s actually significantly easier than it sounds. Let&#8217;s get right to it&#8230; Bicep is a [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,8,54],"tags":[],"class_list":["post-256","post","type-post","status-publish","format-standard","hentry","category-azure","category-bicep","category-learning"],"_links":{"self":[{"href":"https:\/\/www.dumpsterfirecomputing.com\/index.php?rest_route=\/wp\/v2\/posts\/256","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.dumpsterfirecomputing.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.dumpsterfirecomputing.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.dumpsterfirecomputing.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.dumpsterfirecomputing.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=256"}],"version-history":[{"count":1,"href":"https:\/\/www.dumpsterfirecomputing.com\/index.php?rest_route=\/wp\/v2\/posts\/256\/revisions"}],"predecessor-version":[{"id":264,"href":"https:\/\/www.dumpsterfirecomputing.com\/index.php?rest_route=\/wp\/v2\/posts\/256\/revisions\/264"}],"wp:attachment":[{"href":"https:\/\/www.dumpsterfirecomputing.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=256"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.dumpsterfirecomputing.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=256"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.dumpsterfirecomputing.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=256"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}