Terraform

From Logic Wiki
Jump to: navigation, search


Installation

Install Terraform

Windows

choco install terraform

Mac

  • First, install the HashiCorp tap, a repository of all our Homebrew packages.
$ brew tap hashicorp/tap
  • Now, install Terraform with hashicorp/tap/terraform.
$ brew install hashicorp/tap/terraform

Linux

sudo apt-get update && sudo apt-get install -y gnupg software-properties-common

Install the HashiCorp GPG key.

 
$ wget -O- https://apt.releases.hashicorp.com/gpg | \
gpg --dearmor | \
sudo tee /usr/share/keyrings/hashicorp-archive-keyring.gpg > /dev/null

Verify the key's fingerprint.

$ gpg --no-default-keyring \
--keyring /usr/share/keyrings/hashicorp-archive-keyring.gpg \
--fingerprint

The gpg command will report the key fingerprint:

/usr/share/keyrings/hashicorp-archive-keyring.gpg
-------------------------------------------------
pub   rsa4096 XXXX-XX-XX [SC]
AAAA AAAA AAAA AAAA
uid           [ unknown] HashiCorp Security (HashiCorp Package Signing) <security+packaging@hashicorp.com>
sub   rsa4096 XXXX-XX-XX [E]

Download the package information from HashiCorp.

$ sudo apt update

Install Terraform from the new repository.

$ sudo apt-get install terraform

install AWS CLI

https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html

Some Commands

terraform init

  • Downloads the provider in terraform file into working directory (under .terraform/providers)
  • Downloads modules into working directory (under .terraform/modules)

terraform plan

Compares terraform config with actual state (Terraform State) - Do not change configuration outside once it's created with Terraform

terraform apply

Apply differences in cloud

terraform destroy

Destroy everything created

State File

Terraform's representation of the world

it's a json file containing information about every resource and data object. It contains sensitive info (database passwords etc)

Can be stored locally or remotely (Terraform cloud, Amazon s3)

Remote Backend

It's free up to 5 users https://app.terraform.io/

aliiybar - C1+
terraform {
  backend "remote" {
    organization = "devops-directive"
 
    workspaces {
      name = "terraform-course"
    }
  }
}