Part 1: Managing Github with Terraform - Automation

Automation can be defined as the technology that reduce human intervention in any processes. Automation can come in different forms depending upon various use cases. Some run automation scripts locally to ease daily tasks and some take it further to help organizations and infrastructure.

You might know about running automation scripts locally, including unit test written to perform code checks every time you make changes in your code. This might be the simplest example of automation, but this can grow as you move from working locally to working in a team. To make automation scripts for an organization we have Infrastructure as Code aka 'IaC' tools such as Terraform to make our life easier. IaC tools helps us to manage our organization infrastructure using code rather then make manual efforts.

This blog covers you for things that should be considered while managing Infrastructure like Github using Terraform, we will understand "How to automate repetitive task, such as add/replace the file in Github repository and generate a pull request in Github" by taking simpler examples. To accomplish this we will first need to cover how Github and Terraform works and to understand this I have divided this blog into two parts. The first part will teach you about the basics of Github and Terraform and then we will move forward with "How to automate repetitive task, such as add/replace the file in Github repository and generate a pull request in Github" in the second part. Lets get started ;)

Understanding Github and Infrastructure as Code

Github is the most popular platform for software development version control system using Git. And we might use is for personal or organizational use, but Ever have you imagined to automate use of Github to save ample amount of time rather than making manual changes in multiple repository you have?

Automating Github management can be achieved by Infrastructure as Code concept, the managing and provisioning of infrastructure such as Github through code instead of through manual processes. Most IaC tools use a declarative approach which means to define the desired state of the system (i.e. Github) including resources (i.e. Github repository) and properties they should have. IaC tools will configure desired state for us in any infrastructure we want.

IaC process can simply be understood as to create/change infrastructure as we desire (what the end result we want), configure it and also store the current state of our system so that we can revert back whenever we want.

Terraform for beginners

Terraform is an infrastructure as code tool that lets you build, change, and version cloud and on-prem resources safely and efficiently. - terraform.io terraform allows you to manage and automate infrastructure and platform (system) and services that run on that platform (i.e Github). Terraform is open-source and use the declarative model as explained above.

Terraform is tool for infrastructure provisioning meaning prepare the environment (i.e. Create a repository on Github to push files in that repository) and terraform does all this in the correct order because one task may depend on other.

Installing Terraform in your PC

Terraform in pretty easy to install irrespective of whichever operating system you use. Here I will show you how to install terraform in windows based OS and you can find links of other OS terraform installation below.

If you have Chocolatey installed in your windows OS you can directly download it by running below command

choco install terraform

If you don't have Chocolatey installed, you can use windows binary download and download binary zip by clicking on following link. Windows Binary

After downloading Terraform, unzip the package. This is what you will see.

image.png

Now to run terraform globally in your system, set path to terraform.exe on windows.

  • You can do that by searching Environment Variables in windows start menu.
  • Then scroll down in the system variable until you find PATH.
  • Click on edit and enter the path of terraform.exe which we see earlier.

Now your terraform is up and running, you can check that by running the following command

terraform --version

Here is the link for downloading Terraform for other OS Click here.

Core Terraform Command

The command line interface to terraform is through the terraform command. To view a list of the commands available in your current Terraform version, run terraform with no additional arguments.

1. terraform init

Prepare your working directory for other commands.

Try out this terraform init tutorial to get hands on Click here

2. terraform validate

Check whether the configuration is valid

3. terraform plan

Show changes required by the current configuration.

Try out this terraform plan tutorial to get hands on Click here

4. terraform apply

Create or update infrastructure.

Try out this terraform applytutorial to get hands on Click here

5. terraform destroy

Destroy previously-created infrastructure

Conclusion

Here we understand how Github helps us with version controlling, managing repositories and how terraform is installed into our system. To get better understanding I highly recommend to try using different command in Terraform. See you in the next part :)