DEV Community

Cover image for Introduction To Terraform Cloud
Shivam
Shivam

Posted on • Edited on

Introduction To Terraform Cloud

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. I will be using AWS for the sake of this article. Create a free account here.

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. Make sure you get some ideas by reading the intro article here, 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"
Enter fullscreen mode Exit fullscreen mode

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

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

Step 3.2 : Select the Repo created in Step 1

Alt Text

Step 4 : Configure Variables

Note: Your keys will be used here as secret variables, Terraform will treat these variables as secrets.

Alt Text

Alt Text

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

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
  }
Enter fullscreen mode Exit fullscreen mode

I hope you enjoyed building this IAC Pipeline.It’s a very Powerful environment provisioning tool and can manage very complex architectures easily.

Top comments (0)