Description:
Docs
Written in HashiCorp Config Language, HCL
Can be repeated
Resources are infrastructure objects such as compute engine, storage, containers, et cetera.
A. Files and directories:
1. Overview:
2. Override files:
3. Dependency lock file:
4. Test files:
B. Syntax:
2. For .tf
Argument:
Assign a value/attribute to a name
Context where the argument appears determines what value types are valid
for example, each resource type has a schema that defines the types of its arguments
can be an expression
Block: ^1945db
Containers for other content
Has a block type
The type defines how many labels must present after type
< type > "<label1>" [ "<label2>" ,...]{
}
Has block body within {}
can have nested block
resource "google_compute_instance" "vm_instance" {
name = "terraform-instance"
machine_type = "e2-micro"
boot_disk {
initialize_params {
image = "debian-cloud/debian-11"
}
}
Identifier: ^612f22
Argument, block type names, and the names of most Terraform-specific constructs like resources, input variables, etc. are all identifiers .
Can contain letters, digits, underscores (_
), and hyphens (-
).
The first character can not be a digit.
Comment:
#
or //
for a single-line
/*
to */
for block
3. For .tf.json
:
4. Style conventions:
C. Resources:
Each resource block describes one or more infrastructure objects
2. Resource block:
Syntax of a block
Resource types:
Provider
A plugin that offers a collection of resource types in modules.
Each resource type is implemented by a provider.
Based on the resource type’s name, Terraform can usually determine which provider to use
Within the block, resource arguments assign value to the object’s attributes
Meta-arguments
Custom condition checks
Operation timeouts:
Some resource types provide a special timeouts
nested block
Allows to customize how long certain operations are allowed to take before consideredfailed
3. Resource behaviour
Identifier for that real infrastructire object is saved in Terraform’s state
Applying a Terraform configuration will:
Create resources that exist in the configuration but are not associated with a real infrastructure object in the state.
Destroy resources that exist in the state but no longer exist in the configuration.
Update in-place resources whose arguments have changed.
Destroy and re-create resources whose arguments have changed but which cannot be updated in-place due to remote API limitations.
Accessing resource attributes:
Expressions can be used to access information about resources in the same module
Data sources are special type of resource used only for looking up information
Resource dependencies:
Some resources must be processed after other specific resources
because infrastructure need
or requires infor generated by another resource
Terraform analyzes the expressions and order them
If there is implicit dependencies, depends on can be explicitlty specified
Local-only resources:
local-only resource types exist for
generating private keys,
issuing self-signed TLS certificates,
and even generating random ids.
The behavior is the same as all other resources, but their result data exists only within the Terraform state.
”Destroying” such a resource means only to remove it from the state, discarding its data.
depends on
count
for each
provider
lifecycle
provisioner
D. Data sources:
E. Providers:
1.
Terraform relies on plugins called providers to interact with cloud providers, SaaS providers, and other APIs.
Terraform configurations must declare which providers they require so that Terraform can install and use them.
Additionally, some providers require configuration (like endpoint URLs or cloud regions) before they can be used.
2. Provider configuration:
3. Provider requirements:
4. Dependency lock file:
F. Variables and Outputs:
1.
3. Output variables:
G. Modules:
1. Overview:
A module:
A container for multiple resources that are used together
A collections of .tf
and/or .tf.json
in a directory
Each must defines a distinct set of config objects
Not in nested directory
The root module :
A Terraform config has atleast 1 module which is the root module
Contains all .tf
files in the working directory
Child module :
A Terraform module can call other modules to include their resources in the configuration
Published modules:
Terraform can load modules from public (Terraform Registry) or private registry
Using modules:
Developing modules
2. Module block:
Calling a child module:
To cal a module means to include the contents of that module into the configuration with specific values for its Input variables
module "<local-name>>" {
source = "./app-cluster"
< input - variable - defined - by - the - module > = 5
}
This block is called the calling module of the child module
The label right after is the local-name
Module call use arguments:
source
:
mandatory
value is:
the path to a local directory containing the module’s configuration files
or a remote module source that Terraform should download and use
3. Module sources
version
:
4. Module meta-arguments
3. Module sources:
5. Module development:
K . Expressions:
Expressions:
Represent a value, either literally or by referencing and combining other values. They appear as values for arguments, or within other expressions.
2. Types and values
7. Conditional expressions
Custom
…
Preconditions and Postconditions
13. Version constraints:
1.