Azure - Bicep : Udmy Les : 1 :

 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

 https://github.com/dylanbudgen/udemy-bicep-course/tree/main/lesson-05

Azure All Templated (Bicep | ARMS | Powershell )

https://learn.microsoft.com/en-us/azure/templates/

 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

Installing our Bicep Development Environment :

As per our VS Code installation we are going to use several useful extensions that will help us with

-- Syntax highlighting
-- Linting
-- Validation


1. Install Azure CLI Windows

https://learn.microsoft.com/en-us/cli/azure/install-azure-cli-windows?tabs=azure-cli

.msifile toinstall Azure CLI

check which version of Azure we are running

> az --version

> az bicep version


2. Install Azure Powe Shell Modules on Windows


3. Install VSC 

-- Install Extension Azure Tools Pack  // This isnt really necessary but includes loads of extensions
-- Bicep Extension


Section : 3 : Writing your first Bicep Template


Our very first Bicep templates create two different Storage Accounts


Building Bicep to ARM Template or use the command line utility

if you have installed the extensions : the you can right click the file and convert them into ARM Template by choosing "Build ARM Template"

> az bicep build --file main.bicep

How to convert ARM template into Bicep -- or de-compile them

> az bicep decomplie --file main.bicep

15 :


18: Deploying Azure Bicep with Azure CLI




20: Integrating Azure Bicep Deployment with Azure Pipelines

How to deploy bicep files with Azure Pipelines . Integrating your bicep deployments into CI/CD pipeline is amazing



trigger:
- main

pool:
  vmImage: ubuntu-latest

jobs:

  - job: deploy_infra
    displayName: 'Deploy Infra'
    steps:

    - task: AzureResourceManagerTemplateDeployment@3
      displayName: 'Deploy main.bicep'
      inputs:
        deploymentScope: 'Resource Group'
        azureResourceManagerConnection: <service-connection>
        subscriptionId: <subscription-id>
        action: 'Create Or Update Resource Group'
        resourceGroupName: <resource-group>
        location: 'West Europe'
        templateLocation: 'Linked artifact'
        csmFile: 'main.bicep'
        deploymentMode: 'Incremental'

22. Previewing Azure Deployment Bicep Changes



There are two ways you can inject those parameters


https://github.com/dylanbudgen/udemy-bicep-course/blob/main/lesson-05/main.parameters.json




Mixed array datatype


You can have normal arrays

var myArray = [

   'myFirstValue'
   'mySecondValue'
]

Objects : It is key value pairs





29. Azure Bicep Functions :

String manupulation


Data Manupulation :




Scope functions allow us to access the deployment environments


Listkeys function allows us to list the keys in Bicep function .
listkeys().key -- this is an array of the keys those with an Index.

If I were going to inject in some keyvalte somewhere


Azure Bicep provides scope functions to help you work with resource scopes within your Bicep templates. These functions allow you to access information about the current deployment scope, parent scopes, and more. Here are some of the commonly used scope functions in Bicep:

deployment(): Returns information about the current deployment.

var deploymentName = deployment().name

resourceGroup(): Returns information about the current resource group.

var resourceGroupName = resourceGroup().name

subscription(): Returns information about the current subscription.

var subscriptionId = subscription().subscriptionId

managementGroup(): Returns information about the current management group (if applicable).

var managementGroupId = managementGroup().managementGroupId

tenant(): Returns information about the current tenant.

var tenantId = tenant().tenantId

parent(): Returns information about the parent resource (if the current resource is nested).

var parentName = parent().name

extensionResourceId(): Constructs an Azure resource ID for an extension resource in the same scope.

var extensionResource = extensionResourceId('Microsoft.Network/virtualNetworks', 'myVNet', 'subnets', 'mySubnet')


These scope functions provide context-aware information and can be used to reference various attributes of the current scope, which can be particularly useful when you need to construct resource IDs or access properties of resources within a template.

Here's an example of using these scope functions in a Bicep template:

resource myVNet 'Microsoft.Network/virtualNetworks@2021-02-01' = {
  name: 'myVNet'
  location: 'East US'
  properties: {
    addressSpace: {
      addressPrefixes: [
        '10.0.0.0/16'
      ]
    }
  }
}

resource mySubnet 'Microsoft.Network/virtualNetworks/subnets@2021-02-01' = {
  parent: myVNet
  name: 'mySubnet'
  properties: {
    addressPrefix: '10.0.1.0/24'
  }
}

var vnetName = myVNet.name
var subnetName = mySubnet.name
var subnetResourceId = resourceGroup().id + '/providers/Microsoft.Network/virtualNetworks/' + vnetName + '/subnets/' + subnetName




Functions in an Array

length(): Returns the number of elements in an array or the number of characters in a string.

var myArray = [1, 2, 3, 4, 5]
var arrayLength = length(myArray) // arrayLength will be 5

range(): Generates an array of sequential integers within a specified range.

var myRange = range(1, 5) // myRange will be [1, 2, 3, 4, 5]

contains(): Checks if an array or string contains a specific element or substring.

var myArray = ['apple', 'banana', 'cherry']
var containsBanana = contains(myArray, 'banana') // containsBanana will be true

concat(): Concatenates arrays or strings.

var array1 = [1, 2]
var array2 = [3, 4]
var combinedArray = concat(array1, array2) // combinedArray will be [1, 2, 3, 4]

split(): Splits a string into an array of substrings based on a specified delimiter.

var myString = 'apple,banana,cherry'
var stringArray = split(myString, ',') // stringArray will be ['apple', 'banana', 'cherry']


join(): Joins elements of an array into a single string using a specified delimiter.


var myArray = ['apple', 'banana', 'cherry']
var joinedString = join(myArray, ', ') // joinedString will be 'apple, banana, cherry'


unique(): Returns an array containing only unique elements from the input array.


var myArray = [1, 2, 2, 3, 3, 3]
var uniqueArray = unique(myArray) // uniqueArray will be [1, 2, 3]

union(): Combines two or more arrays into a single array with unique elements.


var array1 = [1, 2, 3]
var array2 = [2, 3, 4]
var unionArray = union(array1, array2) // unionArray will be [1, 2, 3, 4]


variables(): Returns a reference to a variable defined in the Bicep template.

var myVar = 'Hello, Bicep!'
var referenceToVar = variables('myVar') // referenceToVar will be 'Hello, Bicep!'


Use Output to display the values of a variable

param myParam string = 'Hello, Bicep!'

var myVariable = 'This is a variable value'

output myOutput string = myParam
output myVariableOutput string = myVariable

-----


To access the keys of an Azure Storage Account in an Azure Bicep template, you would typically use the listKeys function associated with the storage account resource. Here's the correct syntax for retrieving the keys of a storage account in Bicep:

resource storageAccount 'Microsoft.Storage/storageAccounts@2021-08-01' = {
  name: 'mystorageaccount'
  location: 'East US'
  sku: {
    name: 'Standard_LRS'
  }
}

var storageAccountKeys = storageAccount.listKeys('2019-06-01')


In this example:

  1. We declare a storageAccount resource of type 'Microsoft.Storage/storageAccounts@2021-08-01'. You should replace 'mystorageaccount' with the actual name of your storage account and adjust the other properties as needed.

  2. We use the listKeys function to retrieve the keys of the storage account. The argument '2019-06-01' specifies the API version to use when making the request. You can replace this with the appropriate API version if needed.

After this code, the storageAccountKeys variable will contain the keys of the storage account, and you can use them as needed in your Bicep template.



Loading External Files

We can use Bicep to import Yaml files.

We create a JSON file > example.json


The first will parse the file

the second will read the particular content from the example.json file which is the name from teh first element.

We can do the same thing for YAML and TEXT files.


 

Section : 7 : Working with Modules in Biceps


Module called in the main.bicep file .



Creating Multiple storage accounts

module storageAccount1 './storageAccount1.bicep' = {
  params: {
    storageAccountName: 'mystorageaccount1'
    location: 'East US'
    skuName: 'Standard_LRS'
  }
}

module storageAccount2 './storageAccount2.bicep' = {
  params: {
    storageAccountName: 'mystorageaccount2'
    location: 'West US'
    skuName: 'Standard_GRS'
  }
}

PARAMETER.JSON > File for multiple modules

{
  "$schema": "https://schemas.microsoft.com/bicep/parameters-2022-03-01.json",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "storageAccount1Name": {
      "value": "mystorageaccount1"
    },
    "storageAccount1Location": {
      "value": "East US"
    },
    "storageAccount1Sku": {
      "value": "Standard_LRS"
    },
    "storageAccount2Name": {
      "value": "mystorageaccount2"
    },
    "storageAccount2Location": {
      "value": "West US"
    },
    "storageAccount2Sku": {
      "value": "Standard_GRS"
    }
  }
}

name in the above create a deployment ${storageAccountName} what is set in the parameters.json file is taken from the module

When you are using modules 


Section 8: Advanced Bicep Concept :

Understand the Parent and Children resources in Bicep - One example of a Parent and Child resource is a container on a Storage Account . Because a container cannot exist without its parent storage account

We will head over to the module account. 

And we will add a container call "DATA" to all of our deployments , every storage account will have a container created called Data.

And to begin with we need to enable Blob services on our storage account


This will be created after the storage account is created underneath as a child . This blob service will be deployed after the storage account is deployed and set as a child.

We now create a container now.


Communicating with Existing Resources :

It is possible that your one Bicep file is deploying something and the other Bicep file is deploy in some other and they need to communicate.

Perhaps you are performing Role Assignments in another teams resources. Perhaps you need to interact with it to get some ID or the scope of something

-- We are going to add one prameter  Storage Account 

-- User going to add a parameter for AD Group (ID for AD Group for Role Assignment)

We are going to assume that the user wants to set Roles Assignments for a particular AD group

-- Parameter of ID of the Role definition

We are going to create a new module > storage_role_assignment.bicep












Comments

Popular posts from this blog

Overview if Biceps