Terraform with rules
Terraform with rules
Providers
Terraform plan Providers are plugins that allow Terraform to
interact with different cloud platforms and services.
The terraform plan command previews changes
that Terraform will make to align infrastructure with
the configuration, identifies resources to add,
ANUPAM SHUKLA
AWS example AWS Example
Azure example
Azure Example
Resources
Resources are the primary building blocks in
Terraform to manage services and infrastructure.
Each resource block defines a specific resource type
and its configuration.
AWS Resource example
Outputs
Outputs provide information about the resources,
such as IDs or IP addresses. They are useful for
exposing data for use in other configurations or CLI
output.
Azure Example
Variables
Variables allow you to parameterize your
configuration files for reusability. They can have
default values, accept values from external files, or Setting Variable Values
be required. Terraform provides multiple ways to set variable
Terraform supports several types, including string, values:
number, bool, list, map, set, object, tuple, and any. Default Values- Defined within the variable block
during declaration.
Command Line- Use the -var flag to pass variables
during Terraform commands.
ANUPAM SHUKLA
Data source
Environment Variables- Use environment variables Data sources allow you to retrieve existing
with the TF_VAR_ prefix. information about infrastructure resources. It is
useful for referencing resources that are not
managed by Terraform.
Variable File AWS Example
Define values in .tfvars files and pass the file using
the -var-file flag
Provisioners
Provisioners execute scripts or commands on a
resource after it is created.
a) Local Exec
• Executes a local command on the machine
running Terraform after a resource is
created or updated.
• Useful for running scripts or other local
setup tasks.
AWS Example
ANUPAM SHUKLA
Azure Example c) File
• The file provisioner copies files or
directories from the machine running
terraform to the newly created resource.
b) Remote Exec
• Executes a script or command on the
remote resource after it is created.
• Useful for remote configuration tasks
like installing software.
AWS Example
Dependencies
Resource dependencies arise when one resource
depends on the existence of other resources.
Terraform must be able to understand these
dependencies to ensure that resources are created
in the correct order. For example, if resource A has
a dependency on resource B, resource B is created
before resource A.
a) Implicit Dependency
Azure Example • Terraform automatically detects
dependencies between resources based on
resource references in their configurations.
• No manual intervention is required;
dependencies are inferred by referencing
attributes of one resource in another.
AWS Example
ANUPAM SHUKLA
Azure Example AWS Example
Terraform state
The state file (terraform.tfstate) stores information
about the resources Terraform manages. It is
essential for detecting changes, maintaining
idempotency, and managing dependencies.
Azure Example
Lookup
The Terraform lookup function is a built-in function
that retrieves the values from a map variable or a
list of objects. It considers the map variable's name
to retrieve the key and default values. It returns the
key value only when a matching key is found.
Modules Otherwise, it returns the default value.
ANUPAM SHUKLA
AWS Example AWS -Create AWS instance
Azure Example
Idempotency
It ensures that applying a configuration multiple
times produces the same result. Terraform tracks
resource changes using the state file.
Terraform Validations
Main advantage of terraform -Multi-cloud
Terraform provides a terraform validate command
support
to check if your configuration files are syntactically
Terraform can manage resources across multiple valid. It helps ensure that your configuration files
clouds in a single configuration. are correctly formatted and logically sound before
applying them.
It is typically run before applying changes with
terraform apply.
ANUPAM SHUKLA
Terraform Condition
• Ternary Operator: Used to conditionally
assign values or select between two values.
Syntax
ANUPAM SHUKLA