Streamlining Infrastructure: Installing Backstage Developer Portal and Managing Azure Resources with Terraform (Part-2)
Published: 2024-02-09 03:19:44

 [Part-1](https://dev.to/gittest20202/streamlining-infrastructure-installing-backstage-developer-portal-and-managing-azure-resources-with-terraform-part-1-4gl5)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jcg13wyg4nvmuxecc6ac.png)

**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/
```
_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
```
**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 }}

```
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ahkisxwqhm6ltbylybqh.png)

**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
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/92ujr4aurmok92b3orl9.png)

_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

```

_Login to Dev Portal and verify the Template _


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/04dy9fb1nzeaqpruscc8.png)

_Create a Github Repo with the terraform Code_


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/osmqfylkcggivtqy2yp9.png)

_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 
```

_Create Secrets in terraform-cloud for Azure SPN_

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8fqmtzzheopga3goajrq.png)

_Lets Use the Template to Deploy the Resource group and VNET_

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lz4flrof4no9w6yop0td.png)

**Click on Choose template for Create an Resource Group And VNET**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/07h3e3yt3btb8515cv0c.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/purq3c9p40dduq9j251i.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0m4pwxk0xpjyi5f691nu.png)



![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jb0iwn9ni6d8ckiezn91.png)

**After Verifying Click on Create**


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/377tlckaz1eq0ct6b158.png)

_Verify the Pipeline_


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6dodgvy4vgidg36tx1o1.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zs1xppymjshvleq9jeob.png)


Login to Azure Portal and Verify


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sdmvs480d38z9f3rdbv3.png)

_Verify that the Template Get Deployed._


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xupwwsvdstbdtu4n29bx.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c3munnj5hnwkwcnbz7qm.png)


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