Skip to content

vnatarov-mn/terraform

 
 

Repository files navigation

Terraform usage


terraform-lint|||LatestVer|License

Install Terraform

  • If you're using MacOS, then you can run the next command to install TF:
$ brew install terraform

NOTE: you must install HOMEBREW to your host something like:

$ sudo /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"
  • For Unix/Linux OS you might go to the official Terraform site and download bin-file with software.

Init


First of all, you should clone this repo to your local machine. I provided modules with simple examples. After, go to needed example and run the next command:

$ terrafrom init

This command will init everything to provision module(s).

Plan


When you set terraform init you will be able to see which services are going to create with the next command:

$ terraform plan

If you're using tfvars, you can run the following command:

$ terraform plan -var-file terraform.tfvars

It's a good point to use tfvars for multiple environments, - as example.

Apply


To apply your stack, you can use:

$ terraform apply

Or:

$ terraform apply -var-file terraform.tfvars

Also, you can use -auto-approve command to automatically apply the stack.

Destroy


To terminate the stack, use:

$ terraform destroy

Or:

 $ terraform destroy -var-file terraform.tfvars

Also, you can use -auto-approve command to automatically terminate the stack.

Help


To get help, use:

$ terraform help
Usage: terraform [-version] [-help] <command> [args]

The available commands for execution are listed below.
The most common, useful commands are shown first, followed by
less common or more advanced commands. If you're just getting
started with Terraform, stick with the common commands. For the
other commands, please read the help and docs before usage.

Common commands:
    apply              Builds or changes infrastructure
    console            Interactive console for Terraform interpolations
    destroy            Destroy Terraform-managed infrastructure
    env                Workspace management
    fmt                Rewrites config files to canonical format
    get                Download and install modules for the configuration
    graph              Create a visual graph of Terraform resources
    import             Import existing infrastructure into Terraform
    init               Initialize a Terraform working directory
    login              Obtain and save credentials for a remote host
    logout             Remove locally-stored credentials for a remote host
    output             Read an output from a state file
    plan               Generate and show an execution plan
    providers          Prints a tree of the providers used in the configuration
    refresh            Update local state file against real resources
    show               Inspect Terraform state or plan
    taint              Manually mark a resource for recreation
    untaint            Manually unmark a resource as tainted
    validate           Validates the Terraform files
    version            Prints the Terraform version
    workspace          Workspace management

All other commands:
    0.12upgrade        Rewrites pre-0.12 module source code for v0.12
    0.13upgrade        Rewrites pre-0.13 module source code for v0.13
    debug              Debug output management (experimental)
    force-unlock       Manually unlock the terraform state
    push               Obsolete command for Terraform Enterprise legacy (v1)
    state              Advanced state management``` 

Auto-switching Terraform version


The tfswitch command line tool lets you switch between different versions of terraform. If you do not have a particular version of terraform installed, tfswitch lets you download the version you desire. The installation is minimal and easy. Once installed, simply select the version you require from the dropdown and start using terraform.

To install this tool for macOS:

$ brew install warrensbox/tap/tfswitch

Linux:

 $ curl -L https://raw.githubusercontent.com/warrensbox/terraform-switcher/release/install.sh | bash

Snapcraft for CentOS, Ubuntu, Linux Mint, RHEL, Debian, Fedora:

$ sudo snap install tfswitch

Automatically switch with bash

Add the following to the end of your ~/.bashrc file: (Use either .tfswitchrc or .tfswitch.toml or .terraform-version):

cdtfswitch(){
  builtin cd "$@";
  cdir=$PWD;
  if [ -e "$cdir/.tfswitchrc" ]; then
    tfswitch
  fi
}
alias cd='cdtfswitch'

Automatically switch with zsh

Add the following to the end of your ~/.zshrc file:

load-tfswitch() {
  local tfswitchrc_path=".tfswitchrc"

  if [ -f "$tfswitchrc_path" ]; then
    tfswitch
  fi
}
add-zsh-hook chpwd load-tfswitch
load-tfswitch

To get list of the supported version of the software, run the next command:

$ tfswitch -l

Or:

$ tfswitch --list-all

Scroll up or down to see versions. Click on needed to use it.

If a *.tf file with the terraform constrain is included in the current directory, it should automatically download or switch to that terraform version. For example, the following should automatically switch terraform to the lastest version:

terraform {
  required_version = ">= 0.13.9"

  required_providers {
    aws        = ">= 2.52.0"
    kubernetes = ">= 1.11.1"
  }
}

I really like this tool and it can be used for your locally run as well as for CI/CD.

Auto-generate documentation for Terraform modules


I have a python script to generate the README.md file-based on Terraform modules and examples that I'm supporting. The code can be found here: generate-tf-docs.

Authors

Created and maintained by Vitaliy Natarov. An email: vitaliy.natarov@yahoo.com.

License

Apache 2 Licensed. See LICENSE for full details.

About

work with terraform

Resources

License

Code of conduct

Stars

Watchers

Forks

Packages

No packages published

Languages

  • HCL 99.2%
  • Other 0.8%