DEV Community

AMIT CHATURVEDI
AMIT CHATURVEDI

Posted on

Streamlining Infrastructure: Installing Backstage Developer Portal and Managing Azure Resources with Terraform (Part-2)

Part-1
Image description

Continuing to Create Template in Portal and deploy Resource Group In Azure.

In last blog we have created a project named Chicco. Traverse to project Dir.



root@devmaster:~# cd chicco/


Enter fullscreen mode Exit fullscreen mode

Create a directory rg-creation with structure as below



root@devmaster:~/chicco# mkdir -p rg-creation/template/content
root@devmaster:~/chicco# tree rg-creation/
rg-creation/
└── template
    ├── content
    │   └── component-info.yaml
    └── template.yaml

3 directories, 2 files


Enter fullscreen mode Exit fullscreen mode

Template
In Backstage, templates are used to define the structure and layout of various entities, such as services, documentation, components, and more. Templates allow you to standardize how different types of entities are created and managed within your organization.

Here's how templates work in Backstage:

Definition: Templates are defined using YAML files, typically located in the templates directory of your Backstage repository. These YAML files specify the properties, fields, and configurations for each type of entity that can be created using the template.

Customization: Templates can be customized to match your organization's specific needs and preferences. You can modify the YAML files to add or remove fields, change default values, specify validation rules, and more.

Usage: Once templates are defined, they can be used to create new instances of entities within Backstage. Users can select a template from the available options and fill in the required fields to create a new entity based on that template.

Validation: When creating a new entity using a template, Backstage validates the input against the template's specifications to ensure that all required fields are provided and that the data entered is valid.

Consistency: Templates help enforce consistency and standardization across your organization by providing a predefined structure for creating entities. This ensures that entities are created and managed in a consistent manner, making it easier to understand and navigate your Backstage instance.

Lets Create A template which will create Resource Group and VNET in Azure.



root@devmaster:~/chicco# vim rg-creation/template/template.yaml

apiVersion: scaffolder.backstage.io/v1beta3
kind: Template
metadata:
  name: create-rg
  title: Create an Resource Group And VNET
  description: Create an Resource Group and VNET in Azure Subscription
  tags: ['recommended']
spec:
  owner: guests
  type: service
  parameters:
    - title: Resource Group And VNET Creation
      required:
         - ResourceGroupName
         - Location
         - VirtualNetworkName
      properties:
        ResourceGroupName:
          title: Resource Group Name
          type: string
          description: Name of the Resource Group.
          ui:autofocus: true
          ui:options:
            rows: 5

        Location:
          title:  Location
          type: string
          description: Location name where you want to create Resource Group and Virtual Network  eg:- eastus.

        VirtualNetworkName:
          title: Virtual Network Name
          type: string
          description: Name of the Virtual Network
        action:
          title: action
          type: string
          description: What action do you want to perform? Create or delete?
          enum:
            - apply
            - destroy

    - title: Choose a Repository Location
      required:
        - repoUrl
      properties:
        repoUrl:
          title: Location of the repository
          type: string
          ui:field: RepoUrlPicker
          ui:options:
            allowedHosts:
              - github.com

  steps:
# Getting the all the files/details from the template
    - id: fetch-base
      name: Fetching Details from content folder
      action: fetch:template
      input:
        url: ./content  # Location of the content directory where catlog-info.yaml file is present for the template
        values:
          name: ${{ parameters.ResourceGroupName}}

# Publish the content of the current working directory to our github directory
    - id: publish
      name: Publishing Details
      action: publish:github
      input:
        allowedHosts: ['github.com']
        description: This repo is to create Resource Group Name ${{ parameters.ResourceGroupName }} using backstage.
        repoUrl: ${{ parameters.repoUrl }}
        repoVisibility: public # or 'internal' or 'private'
        # defaultBranch: master

# Triggering CI-CD to create resource in our case github action.
    - id: github-action
      name: Starting GitHub action
      action: github:actions:dispatch
      input:
        workflowId: terraform-build.yaml  # Name of the github pipeline created into .github/workflows 
        repoUrl: 'github.com?repo=terraform-cloud&owner=gittest20202' # Repo location where the github action build pipeline is created
        branchOrTagName: 'main'
        workflowInputs:
          ResourceGroupName: ${{ parameters.ResourceGroupName }}
          Location: ${{ parameters.Location }}
          VirtualNetworkName: ${{ parameters.VirtualNetworkName }}
          action: ${{ parameters.action }}

# Registering new component in the catalog of backstage.
    - id: register
      name: Registering the new Component
      action: catalog:register
      input:
        repoContentsUrl: ${{steps['publish'].output.repoContentsUrl }}
        catalogInfoPath: '/component-info.yaml' # where the info of the catalog is stored.

# Output links  are displayed to the user after the template execution.
  output:
    links:
      - title: Repository
        url: ${{ steps['publish'].output.remoteUrl }}
      - title: Open in catalog
        icon: catalog
        entityRef: ${{ steps['register'].output.entityRef }}



Enter fullscreen mode Exit fullscreen mode

Image description

Let's Create Template



root@devmaster:~/chicco# vim app-config.yaml
 - type: file
      target: ../../rg-creation/template/template.yaml
      rules:
        - allow: [Template]

root@devmaster:~/chicco# yarn dev


Enter fullscreen mode Exit fullscreen mode

Image description

In Backstage, component-info metadata is typically stored in YAML files or in a database, and it's used to provide context and information about the components/services managed within the platform. This metadata helps users understand the purpose, usage, and dependencies of each component/service, and it's used to facilitate various features such as search, discovery, documentation, and collaboration.



root@devmaster:~/chicco# cat rg-creation/template/content/component-info.yaml
apiVersion: backstage.io/v1alpha1
kind: Component
metadata:
  name: ${{ values.name | dump }}
  annotations:
    github.com/project-slug: gittest20202/terraform-cloud/terraform/dev # Change the value to your github repo which contains terraform code.
    backstage.io/techdocs-ref: dir:.
  title: 'Infrastructure Automation' # you can give any title
  description: An example of a Resource Group. # Change Description accordingly
spec:
  type: service
  owner: user:guest
  lifecycle: experimental



Enter fullscreen mode Exit fullscreen mode

_Login to Dev Portal and verify the Template _

Image description

Create a Github Repo with the terraform Code

Image description

Create a github action pipeline with name terraform-build.yaml



name: rg-connectivity-001
on:
  push:
    paths:
      - '*.tf'
  workflow_dispatch:
     inputs:
      ResourceGroupName:
        description: "Resource Group Name"
        type: "string"
      VirtualNetworkName:
        description: "Virtual Network Name"
        type: "string"
      Location:
        description: "Location"
        type: "string"  
      action:
        description: "Action to perform like apply or destroy"
        type: "string"   
jobs:
  resourcegroups:
    runs-on: ubuntu-latest
    name: 'rg-connectivity-001'
    env:
      ARM_CLIENT_ID: ${{ secrets.AZURE_CLIENT_ID }}
      ARM_CLIENT_SECRET: ${{ secrets.AZURE_CLIENT_SECRET }}
      ARM_SUBSCRIPTION_ID: ${{ secrets.SUBSCRIPTION_ID }}
      ARM_TENANT_ID: ${{ secrets.AZURE_TENANT_ID }}
    steps:
      - uses: actions/checkout@main
      - name: 'Terraform init'
        id: init
        run: |
          terraform init          

      - name: 'Terraform plan'
        id: plan
        run: |
          terraform plan -var rg_name=${{ github.event.inputs.ResourceGroupName }} -var rg_location=${{ github.event.inputs.Location }} -var vnetname=${{ github.event.inputs.VirtualNetworkName }}

      - name: 'Terraform apply'
        id: apply
        run: |
          terraform ${{ github.event.inputs.action }} -var rg_name=${{ github.event.inputs.ResourceGroupName }} -var rg_location=${{ github.event.inputs.Location }} -var vnetname=${{ github.event.inputs.VirtualNetworkName 


Enter fullscreen mode Exit fullscreen mode

Create Secrets in terraform-cloud for Azure SPN

Image description

Lets Use the Template to Deploy the Resource group and VNET

Image description

Click on Choose template for Create an Resource Group And VNET

Image description

Image description

Image description

Image description

After Verifying Click on Create

Image description

Verify the Pipeline

Image description

Image description

Login to Azure Portal and Verify

Image description

Verify that the Template Get Deployed.

Image description

Image description

*Hope you have enjoyed reading the blog and starting using the Backstage for deployment.
*

Top comments (0)