Introduction To Terraform Cloud
Tags: aws, devops, tutorial, beginners
Published: 2020-07-22 05:45:52

Infrastructure-as-Code (IAC) is a Thing now. CI’s now are not only limited to Code. ‘I’ in CI was missing a critical meaning : "Infrastructure". 

There was a time when Infrastructure either meant Large Server Rooms or all components deployed in the public cloud before even testing the requirements for the software.

With the Introduction of IAC, infrastructure management is now just a Push away. The automation of infrastructure deployment has now become an integral part of DevOps which creates insanely faster release cycles.

> The use of continuous delivery pipelines for infrastructure to orchestrate the release cycle is now an essential part of DevOps.
IAC Pipeline is advantageous to find errors even before the changes are applied to environments.

Before proceeding further,

> If you are here, I assume you have the basic knowledge of Public Clouds like Azure, AWS, GCP. If not, I recommend a five-minute read [here](https://aws.amazon.com/). I will be using AWS for the sake of this article. Create a free account [here](https://aws.amazon.com/).

The purpose of this article is to make you understand and appreciate the problem that is being solved with the IAC pipeline. You’ll need to Signup to use terraform [here](https://www.terraform.io/). Make sure you get some ideas by reading the intro article [here](https://www.terraform.io/intro/index.html), this will accelerate you in no time.

Like any other programming language, terraform also has Variable declarations and Best Practice recommends that Terraform script should have different files for variables e.g. variable.tf or .tfvars. But for now, we don’t need to worry about that.

Terraform can be used either by downloading the packages locally or by using Terraform Cloud, an online dashboard (GUI Version). For this article, we will be using Terraform Cloud. It’s better to get a basic understanding of terraform before downloading and using it as CLI.
Let’s code a basic script to get things started:

```
#For AWS
provider "aws" {
  region     = "us-east-1"
  access_key = ""
  secret_key = ""
}

#For GCP
provider "google" {
  project = "YOUR GCP PROJECT"
  region  = "us-central1"
  zone    = "us-central1-c"
```

> The above code is for the authentication, so that terraform can access your cloud platform.

> **_Note : Never Expose Your Authentication Keys._**

###Steps To Configure IAC Pipeline

**Step 1 :** Create a Repository on Github with Authentication Script above respective to your provider.

**Step 2 :** Log-in to your Terraform cloud dashboard and create an Organization.

>Organizations are a shared space for teams to collaborate on workspaces in Terraform Cloud

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/olmvr7ahwxqymvetmz3o.png)

**Step 3 :** Creating a workspace —
Workspaces are how Terraform Cloud organizes infrastructure. A workspace consists of:

* A collection of Terraform configurations (retrieved from a VCS repo).
* Values for any variables those configurations require.
* Persistent stored state for the resources it manages.
* Historical state and run logs.

_Step 3.1 :_ Configure VCS (Github.com in this case)

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/olmvr7ahwxqymvetmz3o.png)

_Step 3.2 :_ Select the Repo created in Step 1

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/tyxwso1hopyh0cz33vuq.png)

**Step 4 :** Configure Variables

>**_Note: Your keys will be used here as secret variables, Terraform will treat these variables as secrets._**

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/lkybzaztjm2tk8ke1yeg.png)

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/lv12iqo3cpwm1bwwltta.png)

**Step 5 :** Update and commit anything like, add comment or change the location in the provider. After the Commit terraform plan will be triggered and will automatically perform the tasks as written in the terraform scripts, in this case, Authenticate to your cloud provider.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/0kgbzfeo4tdokpyk6vi2.png)

>If your planning was successful, Congratulations you have successfully authenticated to your cloud provider.

Just add the code to the repository and commit:

```
resource "aws_vpc" "main" {
  cidr_block       = "10.0.0.0/16"
  instance_tenancy = "dedicated"

  tags = {
    Name = "mainINF"
  }
}
resource "aws_subnet" "new"{
  cidr_block="10.0.1.0/24"
  vpc_id     = aws_vpc.main.id
  }
```
I hope you enjoyed building this IAC Pipeline.It’s a very Powerful environment provisioning tool and can manage very complex architectures easily.