All technological notes.
Infrastructure as code
version control system like GITvs other automation tools
Ansible, Chef, Puppet, Saltstack have a focus on automating the installation and conguration of software
Terraform can automate provisioning of the infrastructure itself
AWS, DigitalOcean, Azure APIWorks well with automation software like ansible to install software after the infrastructure is provisioned先创建基础设施,再自动安装。
Website: https://www.terraform.io/
terraform.exe in a path.PATH.
Path and click EditNew and paste the path.terraform versionterraform console
terraform console
HCL
HashiCorp Conguration LanguageTerraform file extension:
.tfTerraform language
Terraform configuration
<BLOCK TYPE> "<BLOCK LABEL>" "<BLOCK LABEL>" {
# Block body
<IDENTIFIER> = <EXPRESSION> # Argument
}
Blocks
Blocks have a block type, can have zero or more labels, and have a body that contains any number of arguments and nested blocks.Arguments
Expressions
arguments, or within other expressions.File Extension
.tf file extension..tf.json file extension.Configuration files
Text Encoding
Configuration files must always use UTF-8 encodingmodule
.tf and/or .tf.json files kept together in a directory.Terraform module
configuration files in a module, effectively treating the entire module as a single document.
child modules can come from local directories (nested in the parent module’s directory, or anywhere else on disk), or from external sources like the Terraform Registry.Root Module
root module and the tree of child modules (which includes the modules called by the root module, any modules called by those modules, etc.).In Terraform CLI, the root module is the working directory where Terraform is invoked.
Workspace
terraform apply in new workspace will recreate all resources, whose state will be managed in this workspace.Varaible terraform.workspace
# avoid naming collision
resource "aws_ssm_parameter" "my-parameter"{
name = "/myapp/myname-${terraform.workspace}"
}
# conditional
resource "aws_instance" "myinstance" {
count = terraform.workspace == "default" ? 1 :0
}
Not Fit
Common Commands
| CMD | DESC |
|---|---|
terraform workspace new wsp_name |
Create a new workspace |
terraform workspace select wsp_name |
Switch to a workspace |
Use Case
Real world Practises
TF_LOG environment variable to enable more loggin.# Linux
terraform apply -var TF_LOG=DEBUG
# windows Powershell
$Env:TF_LOG="DEBUG"
TRACEDEBUGINFOWARNERROR