Terrraform Flashcards
Terraform Installation
Note for using Binary Installation(GUI) method as against package manager(CLI).
Terraform must be installed into the /usr/local/bin/terraform folder.
Terraform Binary File Location
command:
$ which terraform
Expected result:
/usr/local/bin/terraform
VM Configurations Musts
CPU
Memory
Storage
OS
Minimal requirement for Instance creation
resource “RESOURCE NAME” “INSTANCE NAME” {
ami = “ami-0230bdFD60aa48260c6”
Instance_Type = “t2.micro”
Required Block
Provider block is the only required block. All other blocks can be changed
Terraform init
Downloads the provider plugins defined in the provider block of your code
A single Terraform Directory can contain morethan one provider plugin
Required_provider block
This specification must be explicitly defined in the provider block to enable terraform plugin daemon to look for the plugins required for that provider. For providers that are not maintained by HashiCorp.
Hashicorp maintained provider
This can be in the direct form: provider: “aws”
However if it is not a HashiCorp maintained(a community based or individual ) provider, the the required_proider block must be used.
Terraform destroy in a multicloud directory
use the -target flag
Syntax
- target resource_type + local resource name
terraform apply on a blank code
is equal to terraform destroy, however this can be used to remove specific resources from a deployment
terraform state file
a metadata file that stores the details of the physical result of a running physical deployment. However whenever a resource is destroyed, the statefile is also destroyed.
Terraform command and directory files
terraform commands work on all deployment codes in its directory. ie, it can deploy to multiple platforms with a single terraform plan command.
Statefile Warning
DO NOT EDIT A STATEFILE
STORE A BACKUP COPY OF YOUR
Terraform code is also known as Desire at State.
While the state file can be called the current state
Terraform refresh
Terraform refresh synchronizes the current state and statefile, giving an opportunity for Terraform plan to update you on what you are about to destroy or create.
Note:
terraform refresh functionality is intrinsic, does not requre a manual declaration
Provider version
Terraform init will download the version of provider specified in the provider block. This specification is very necessary in order to avoid infrastructure mismatch.
> = 1.0
~> 3.0 - any version in the 3x range.
=2, <=3.5
Terraform version best practice
best to define a specific provider version, a vague version(eg ~>x) , leaves room for terraform downloading even beta versions, and thereby affecting your resource.
.terraform.lock.hcl
The terraform dependency lock file in our terraform directory restricts terraform to the chosen or preffered plugin version it downloaded during terraform init
terraform init -upgrade
this flag added to the terraform init command bypasses and downloads the newer dependency/plugin version within the specified version range.
Also note that the upgrade can either be an upward or downward upgrade.
terraform apply -auto-approve
this tag bypasses the need for an approval before deployment
Credentials and security concerns
It is a dangerous practice to declare configuration files or their path in a tf document/code.
If these lines are not added to the provider block, Terraform will locate these files at HOME/.aws/config
and HOME/.aws/credentials on Linux and macOS. And USERPROFILE%.aws\config,
you have a USERPROFILE%.aws\credentials for windows
aws configure (command on aws CLI)
This command applies and creates the relevant directories to store aws credentials; ie,
attribute
characteristics/contents of values generated in a deployment process
Outputs
this statement fetches and presents artifactes generated from a deployment to the user for further use
Syntax
output “StringName” {
value = resourcename.attributeName
}
*eg. for ec2 instance deployment
output “public_ip” {
value = aws_instance.web.public_ip
}
variables.tf
values are stored in this file and called upon within the terraform code using the var.variableName function.
eg.
In Variable.tf
variable “vpn_ip” {
default = “116.50.30.50/32”
}
in file.tf value_of_x = [var.vpn_ip]
the result of the above will be the value of default. ie,
value_of_x = 116.50.30.50/32
note that terraform compiler takes note of every file within the working directory.