Terraform usage: Difference between revisions

From wikinotes
No edit summary
 
(4 intermediate revisions by the same user not shown)
Line 14: Line 14:
<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
terraform fmt  # autoformat terraform files
terraform fmt  # autoformat terraform files
terraform init    # install dependencies
terraform plan    # show what is required to deploy
terraform apply    # deploy instance
terraform destroy  # delete instance
</syntaxhighlight>
</syntaxhighlight>
</blockquote><!-- Usage -->
</blockquote><!-- Usage -->
Line 24: Line 29:
# ${PROJECT}/main.tf
# ${PROJECT}/main.tf


terraform {
variable "vultr_api_key" {
   required_providers {
   type = string
    vultr = {
      source = "vultr/vultr"
      version = ">=2"
    }
  }
}
}


provider "vultr" {
provider "vultr" {
   api_key = "VULTR_API_KEY"
   api_key = (var.vultr_api_key)
}
}


resource "vultr_instance" "web" {
# ...
  # ...
}
</syntaxhighlight>
</syntaxhighlight>


Line 45: Line 43:
# ${PROJECT}/.envrc
# ${PROJECT}/.envrc


export TF_VAR_api_key=abcdefg
export TF_VAR_vultr_api_key="$(pass foo/bar/api_key)"
</syntaxhighlight>
</syntaxhighlight>



Latest revision as of 21:38, 25 September 2022

Documentation

terraform cli https://www.terraform.io/cli
debugging https://www.terraform.io/internals/debugging

Usage

terraform fmt  # autoformat terraform files

terraform init     # install dependencies
terraform plan     # show what is required to deploy
terraform apply    # deploy instance
terraform destroy  # delete instance

Secrets Management

The simplest way is probably a combination of direnv and pass.

# ${PROJECT}/main.tf

variable "vultr_api_key" {
  type = string
}

provider "vultr" {
  api_key = (var.vultr_api_key)
}

# ...
# ${PROJECT}/.envrc

export TF_VAR_vultr_api_key="$(pass foo/bar/api_key)"
cd ${PROJECT}
direnv allow
terraform init