DEV Community

Cover image for Az Managed Grafana with Terraform Azapi & Devops
Arindam Mitra
Arindam Mitra

Posted on • Edited on

Az Managed Grafana with Terraform Azapi & Devops

Greetings my fellow Technology Advocates and Specialists.

In this Session, I will demonstrate, How to deploy Azure Managed Grafana with Terraform AzAPI and DevOps

LIVE RECORDED SESSION:-
LIVE DEMO was Recorded as part of my Presentation in JOURNEY TO THE CLOUD: WRAP UP - 2022 Forum/Platform
Duration of My Demo = 37 Mins 27 Secs
WHAT IS TERRAFORM AZAPI PROVIDER:-
As Per the official documentation: The AzAPI provider is a very thin layer on top of the Azure ARM REST APIs. This provider compliments the AzureRM provider by enabling the management of Azure resources that are not yet or may never be supported in the AzureRM provider such as private/public preview services and features.
For more information, please refer AzAPI Documentation
USE CASE:-
How to deploy Azure Managed Grafana using Terraform when the required AzureRM Provider is NOT available ?
REQUIREMENTS:-
  1. Azure Subscription.
  2. Azure DevOps Organisation and Project.
  3. Service Principal with Required RBAC ( Contributor) applied on Subscription or Resource Group(s).
  4. Azure Resource Manager Service Connection in Azure DevOps.
  5. Microsoft DevLabs Terraform Extension Installed in Azure DevOps and in Local System (VS Code Extension).
OUT OF SCOPE:-
Azure DevOps Pipeline Code Snippet Explanation.
CODE REPOSITORY:-

AZ MANAGED GRAFANA WITH TERRAFORM AZAPI & DEVOPS

Greetings my fellow Technology Advocates and Specialists.

In this Session, I will demonstrate, How to deploy Azure Managed Grafana with Terraform AzAPI and DevOps

LIVE RECORDED SESSION:-
LIVE DEMO was Recorded as part of my Presentation in JOURNEY TO THE CLOUD: WRAP UP - 2022 Forum/Platform
Duration of My Demo = 37 Mins 27 Secs
IMAGE ALT TEXT HERE
WHAT IS TERRAFORM AZAPI PROVIDER:-
As Per the official documentation: The AzAPI provider is a very thin layer on top of the Azure ARM REST APIs. This provider compliments the AzureRM provider by enabling the management of Azure resources that are not yet or may never be supported in the AzureRM provider such as private/public preview services and features.
For more information, please refer AzAPI Documentation
USE CASE:-
How to deploy Azure Managed Grafana using Terraform when the required AzureRM Provider is NOT available ?
REQUIREMENTS:-
  1. Azure…
HOW DOES MY CODE PLACEHOLDER LOOKS LIKE:-
Image description
PIPELINE CODE SNIPPET:-
AZURE DEVOPS YAML PIPELINE (azure-pipelines-azapi-az-managed-grafana-v1.0):-


trigger:
  none

######################
#DECLARE PARAMETERS:-
######################
parameters:
- name: SubscriptionID
  displayName: Subscription ID Details Follow Below:-
  default: 210e66cb-55cf-424e-8daa-6cad804ab604
  values:
  -  210e66cb-55cf-424e-8daa-6cad804ab604

- name: ServiceConnection
  displayName: Service Connection Name Follows Below:-
  default: amcloud-cicd-service-connection
  values:
  -  amcloud-cicd-service-connection

######################
#DECLARE VARIABLES:-
######################
variables:
  ResourceGroup: tfpipeline-rg
  StorageAccount: tfpipelinesa
  Container: terraform
  TfstateFile: AMG/Grafana.tfstate
  BuildAgent: windows-latest
  WorkingDir: $(System.DefaultWorkingDirectory)/AzAPI-Az-Managed-Grafana
  Target: $(build.artifactstagingdirectory)/AMTF
  Environment: NonProd
  Artifact: AM

#########################
# Declare Build Agents:-
#########################
pool:
  vmImage: $(BuildAgent)

###################
# Declare Stages:-
###################
stages:

- stage: PLAN
  jobs:
  - job: PLAN
    displayName: PLAN
    steps:
# Install Terraform Installer in the Build Agent:-
    - task: ms-devlabs.custom-terraform-tasks.custom-terraform-installer-task.TerraformInstaller@0
      displayName: INSTALL TERRAFORM VERSION - LATEST
      inputs:
        terraformVersion: 'latest'
# Terraform Init:-
    - task: TerraformTaskV2@2
      displayName: TERRAFORM INIT
      inputs:
        provider: 'azurerm'
        command: 'init'
        workingDirectory: '$(workingDir)' # Az DevOps can find the required Terraform code
        backendServiceArm: '${{ parameters.ServiceConnection }}' 
        backendAzureRmResourceGroupName: '$(ResourceGroup)' 
        backendAzureRmStorageAccountName: '$(StorageAccount)'
        backendAzureRmContainerName: '$(Container)'
        backendAzureRmKey: '$(TfstateFile)'
# Terraform Validate:-
    - task: TerraformTaskV2@2
      displayName: TERRAFORM VALIDATE
      inputs:
        provider: 'azurerm'
        command: 'validate'
        workingDirectory: '$(workingDir)'
        environmentServiceNameAzureRM: '${{ parameters.ServiceConnection }}'
# Terraform Plan:-
    - task: TerraformTaskV2@2
      displayName: TERRAFORM PLAN
      inputs:
        provider: 'azurerm'
        command: 'plan'
        workingDirectory: '$(workingDir)'
        commandOptions: "--var-file=az-managed-grafana.tfvars --out=tfplan"
        environmentServiceNameAzureRM: '${{ parameters.ServiceConnection }}'

# Copy Files to Artifacts Staging Directory:-
    - task: CopyFiles@2
      displayName: COPY FILES ARTIFACTS STAGING DIRECTORY
      inputs:
        SourceFolder: '$(workingDir)'
        Contents: |
          **/*.tf
          **/*.tfvars
          **/*tfplan*
        TargetFolder: '$(Target)'
# Publish Artifacts:-
    - task: PublishBuildArtifacts@1
      displayName: PUBLISH ARTIFACTS
      inputs:
        targetPath: '$(Target)'
        artifactName: '$(Artifact)' 

- stage: DEPLOY
  condition: succeeded()
  dependsOn: PLAN
  jobs:
  - deployment: 
    displayName: Deploy
    environment: $(Environment)
    pool:
      vmImage: '$(BuildAgent)'
    strategy:
      runOnce:
        deploy:
          steps:
# Download Artifacts:-
          - task: DownloadBuildArtifacts@0
            displayName: DOWNLOAD ARTIFACTS
            inputs:
              buildType: 'current'
              downloadType: 'single'
              artifactName: '$(Artifact)'
              downloadPath: '$(System.ArtifactsDirectory)' 
# Install Terraform Installer in the Build Agent:-
          - task: ms-devlabs.custom-terraform-tasks.custom-terraform-installer-task.TerraformInstaller@0
            displayName: INSTALL TERRAFORM VERSION - LATEST
            inputs:
              terraformVersion: 'latest'
# Terraform Init:-
          - task: TerraformTaskV2@2 
            displayName: TERRAFORM INIT
            inputs:
              provider: 'azurerm'
              command: 'init'
              workingDirectory: '$(System.ArtifactsDirectory)/$(Artifact)/AMTF/' # Az DevOps can find the required Terraform code
              backendServiceArm: '${{ parameters.ServiceConnection }}' 
              backendAzureRmResourceGroupName: '$(ResourceGroup)' 
              backendAzureRmStorageAccountName: '$(StorageAccount)'
              backendAzureRmContainerName: '$(Container)'
              backendAzureRmKey: '$(TfstateFile)'
# Terraform Apply:-
          - task: TerraformTaskV2@2
            displayName: TERRAFORM APPLY # The terraform Plan stored earlier is used here to apply only the changes.
            inputs:
              provider: 'azurerm'
              command: 'apply'
              workingDirectory: '$(System.ArtifactsDirectory)/$(Artifact)/AMTF'
              commandOptions: '--var-file=az-managed-grafana.tfvars' # The terraform Plan stored earlier is used here to apply. 
              environmentServiceNameAzureRM: '${{ parameters.ServiceConnection }}'



Enter fullscreen mode Exit fullscreen mode
TERRAFORM CODE SNIPPET:-
TERRAFORM (main.tf):-


terraform {

  required_version = ">= 1.3.3"

  backend "azurerm" {
    resource_group_name  = "tfpipeline-rg"
    storage_account_name = "tfpipelinesa"
    container_name       = "terraform"
    key                  = "AMG/Grafana.tfstate"
  }

  required_providers {
    azurerm = {
      source  = "hashicorp/azurerm"
      version = "~> 3.27"
    }
    azapi = {
      source = "Azure/azapi"
      version = "1.0.0"
    }

  }
}
provider "azurerm" {
  features {}
  skip_provider_registration = true
}

provider "azapi" {
}



Enter fullscreen mode Exit fullscreen mode
EXPLANATION:-
Browse to the provided LINK in order to know, as how to use the AzAPI Provider.
Below is how it looks:-
Image description
TERRAFORM (az-managed-grafana.tf):-


#####################
## Resource Group:-
#####################

resource "azurerm_resource_group" "azrg" {
  name        = var.rg-name
  location    = var.rg-location
}

##############################
## Azure Managed Grafana:-
##############################
resource "azapi_resource" "azgrafana" {
  type        = "Microsoft.Dashboard/grafana@2022-08-01" 
  name        = var.az-grafana-name
  parent_id   = azurerm_resource_group.azrg.id
  location    = azurerm_resource_group.azrg.location

  identity {
    type      = "SystemAssigned"
  }

  body = jsonencode({
    sku = {
      name = "Standard"
    }
    properties = {
      publicNetworkAccess = "Enabled",
      zoneRedundancy = "Enabled",
      apiKey = "Enabled",
      deterministicOutboundIP = "Enabled"
    }
  })

}



Enter fullscreen mode Exit fullscreen mode
EXPLANATION:-
The Reference Example of Terraform Resource "azapi_resource" can be found HERE
The JSONENCODE Body definition was build using Azure REST API Reference
From the above Documentation, browse for "Managed Grafana" to find all details to set the "azapi_resource" correctly for Azure Managed Grafana. Below is how it looks:-
Image description
Image description
Image description
TERRAFORM (variables.tf):-


variable "rg-name" {
  type        = string
  description = "Name of the Resource Group"
}
variable "rg-location" {
  type        = string
  description = "Location of the Resource Group"
}
variable "az-grafana-name" {
  type        = string
  description = "Name of the Azure Managed Grafana"
}



Enter fullscreen mode Exit fullscreen mode
NOTE:-
This is self-explanatory.
TERRAFORM (az-managed-grafana.tf):-


rg-name              = "GrafanaRG"
rg-location          = "West Europe"
az-grafana-name      = "AMGrafanaTest"



Enter fullscreen mode Exit fullscreen mode
NOTE:-
This is self-explanatory.
TEST RESULTS:-
Pipeline Execution:-
Image description
Resource Deployment:-
Image description
Important to Note:-
Please add the required RBAC "Grafana Admin" to user identity who will access Azure Managed Grafana URL.
Below is how Azure Managed Grafana looks like:-
Image description

Hope You Enjoyed the Session!!!

Stay Safe | Keep Learning | Spread Knowledge

Top comments (0)