Creating Infra Using Backstage Templates, Terraform and GitHub actions.
Tags: platformenginnering, backstage, aws, terraform
Published: 2024-01-14 09:58:05


![Platform Engineering Using Backstage](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/epsrwfmlner8bjvfq1mn.png)
## Introduction

Backstage is an open-source platform for constructing Internal Developer Portals (IDPs). Internal Developer Portals serve as a one-stop shop, that provides a unified view of all our resources. IDPs enable us to seamlessly create, manage, monitor, and document our software resources from a single location. The primary goal of the IDP is to eliminate the reliance on the connection between DevOps and developers.

## Prerequisites

 1. Backstage is up and running. If you need assistance with deploying Backstage, please follow this [getting started guide](https://backstage.io/docs/features/software-templates/).

 2. Backstage integration with Github.

 3. A working Terraform code is uploaded on the GitHub repo.

 4. GitHub action file is created to execute the terraform code.

For the sake of this blog, we are using GitHub and AWS, but you can use any CI/CD tool and cloud provider.

## **Templates**

Backstage provides a software catalog which is used to manage all of our software resources, which include CI-CD, Docs, API, K8s, Websites, microservices etc. To create a new component(entity) in the software catalog we need Templates.

The structure of the template file is almost similar to the kubernetes manifests. This similarity in structure makes it easier for users familiar with Kubernetes to work with Backstage templates.

Below is a backstage template that you can use to create an EC2 instance on AWS.

```
apiVersion: scaffolder.backstage.io/v1beta3
    kind: Template
    metadata:
      name: sagar
      title: Backstage automation
      description: creating ec2 using backstage and terraform.
    spec:
      owner: guest
      type: service
    
      parameters:
        - title: backstage demo template
          required:
            - name
          properties:
            name:
              type: string
    
      steps:
        - id: test
          name: backstage-blog
          action: debug:log
          input:
            message: 'Hello, ${{ parameters.name }}!'
    
      output:
        links:
          - title: Open in catalog
            icon: catalog
            entityRef: ${{ steps['register'].output.entityRef
```

 1. ***apiVersion: -*** is the required field and the latest value of the API version is `scaffolder.backstage.io/v1beta3`.

 2. ***Kind: - ***template is the entity in the backstage and to configure it, set the value of kind to Template.

 3. ***metadata: - ***Information you will add in metadata will appear on the template card.

 4. ***spec: -*** It contains the name of the owner/group of the template and the type of template it could be anything for example:- service/ website/app etc.

 5. ***parameters: -*** These are the list of options you can add to the template to get the information from the user. Parameters must contain these components: -
**5.1.** ***title***: - contains the title of the template
**5.2.** ***required***: - You can add the parameters which are mandatory for the template so that a user cannot skip it.
**5.3.** ***properties***: - It is used to take input from the user.

 6. ***steps***: - Here we define actions taken by the template while executing the template.

 7. *** Output ***: - This is the final section in the template file. After completing all the actions specified in the ‘steps,’ this part is executed. It is used to display the result of the template execution, although it is not mandatory.

You can find out more about the template format [here](https://backstage.io/docs/features/software-catalog/descriptor-format).

## Deploying Custom Template

Now let's see the files which we have created for the demo. In my backstage code, I have created a directory ec2-demo/template inside the template directory I have created a template.yaml file. Also, I have created a content directory where I have created 2 files component-info.yaml, index.json and package.json.

![Screenshot of the template directory.](https://cdn-images-1.medium.com/max/2000/1*vlPtr8j957Noy02ZXp0vsw.png)

I have added component-info.yaml, index.json and package.json files below. In the component-info.yaml file I have given the reference of the GitHub repo where my Terraform workflow is present.
{% gist https://gist.github.com/sagar0419/dfddfee7c0e9c67142ea584473ac7335.js %}
After adding all these files, navigate to your app-config.yaml file and insert the template path under the catalogue section, as illustrated in the screenshot below. Adjust the target path according to your requirements.

![screenshot of app-config.yaml](https://cdn-images-1.medium.com/max/2000/1*RLf5TAFIqxkhgU99pJth9Q.png)

After adding the path, execute the yarn dev command. Now, on the Backstage portal, under the Create option, you will notice the newly created template.

![](https://cdn-images-1.medium.com/max/2000/1*Kh1qBiYM9oAGXXYI81jKGg.png)

In the template.yaml file, Under the parameter I have added instanceName, region and instanceType. Once you open the ec2-template it will ask you to add these values.

![screenshot of the template (Parameter Part)](https://cdn-images-1.medium.com/max/2000/1*B8GNte78-LZaLBSi0jDfcw.png)

After adding the values on the next page it will ask you to add a repository name and owner. You can give any name to the repo, whatever name you add here the template will create a repo on GitHub with that name. The owner can be a backstage group or users.

![Screenshot of the repository location](https://cdn-images-1.medium.com/max/2000/1*kPaKM_MS-_B5ibJCXZ2qIg.png)

Once all the details are filled, the template will ask you to verify the details which you have added.

![Screenshot of review page](https://cdn-images-1.medium.com/max/3258/1*GvK4jnEinN3YNsweIRx2tQ.png)

After reviewing, click on ‘Create.’ It will take a couple of seconds to generate your catalog. In the background, Backstage will trigger the GitHub workflow. This workflow will execute the ‘terraform apply’ command, which will then create an EC2 instance. Below is the screenshot of the GitHub action.

![Screenshot of GitHub workflow file.](https://cdn-images-1.medium.com/max/3680/1*u58Ni9eiFFDSBeWNLir1Yw.png)

If you check the status of the GitHub action Job you will see the status of the current job and its progress.

![Screenshot of the GitHub Action](https://cdn-images-1.medium.com/max/2000/1*Ie5DB6yZmIhKIxilyirSBQ.png)

Coming back to backstage UI, here you will see that your catalog has been created.

![Screenshot of the output window.](https://cdn-images-1.medium.com/max/3074/1*gf1j1bkEPGeJsSeNCSETgA.png)

Once done you can click on the catalog link. It will take you to the newly created catalog window.

![Screenshot of the Catalog](https://cdn-images-1.medium.com/max/2000/1*a88LF58Tue6SrUj0ZYWj8w.png)

Here you will see a CI-CD option under that option you can check the status/progress of the GitHub workflow job.

![Screenshot of the CI-CD option.](https://cdn-images-1.medium.com/max/2270/1*a1MssDTrU66f18AS2DpddQ.png)

Once the pipeline is completed your ec2 instance will be up and running.

## **Summary**

In this blog post, we have seen how we can use the backstage template to create an AWS EC2 instance. You can also use the template to spin Infra anywhere, to create a CI-CD Pipeline to deploy your application etc.

I hope you found this post informative and engaging. I would love to hear your thoughts on this post, so do start a conversation on [Twitter](https://twitter.com/sagarrajput27)** or [LinkedIn](https://www.linkedin.com/in/sagar-parmar-834403a6/).

[**Integration of Opentelemetry (Auto Instrumentation) with Jaeger**](https://blog.devgenius.io/integration-of-opentelemetry-auto-instrumentation-with-jaeger-4de147a64f38)

[**Vcluster — Architecture Overview and Installation**](https://blog.devgenius.io/vcluster-architecture-overview-and-installation-d41b6262b2f8)

[**Backup, Restore and Migrate Kubernetes Cluster resources using Velero.**](https://blog.devgenius.io/backup-restore-and-migrate-kubernetes-cluster-resources-using-velero-a9b6997e4b54)

[**Sealed Secret in Kubernetes**](https://blog.devgenius.io/sealed-secret-in-kubernetes-d10fed2da964)