0% found this document useful (0 votes)
2 views

Terraform with rules

Terraform is an Infrastructure as Code (IaC) tool that enables users to define and manage infrastructure using a declarative configuration format, supporting multiple cloud providers. Key features include a workflow for initializing, planning, applying, and destroying resources, as well as the use of blocks, variables, and modules for organization and reusability. It also ensures idempotency and dependency management through a state file and provides validation commands to check configuration syntax before applying changes.

Uploaded by

madhav
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Terraform with rules

Terraform is an Infrastructure as Code (IaC) tool that enables users to define and manage infrastructure using a declarative configuration format, supporting multiple cloud providers. Key features include a workflow for initializing, planning, applying, and destroying resources, as well as the use of blocks, variables, and modules for organization and reusability. It also ensures idempotency and dependency management through a state file and provides validation commands to check configuration syntax before applying changes.

Uploaded by

madhav
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Terraform

update, or delete, verifies syntax, resolves


Terraform is an Infrastructure as Code (IaC) tool
dependencies, and ensures safe modifications by
that lets you define and manage infrastructure in a
allowing a review before applying.
declarative configuration format.
It supports provisioning for various cloud providers
like AWS, Azure, GCP, and on-premises systems.
Terraform apply
Key features
It applies the changes required to reach the desired
• It uses declarative language i.e. infrastructure state. It asks prompts for
HCL(HashiCorp Configuration Language). confirmation before execution.
• It has multi-cloud support
• It has consistent infrastructure automation

Terraform Workflow Terraform destroy


The basic Terraform workflow includes init, plan, It deletes all resources defined in the Terraform
apply, and destroy commands. configuration.
These steps enable a complete lifecycle for
provisioning, managing, and de-provisioning
resources.
Blocks
Terraform init
Blocks are the core structural elements of a
Initializes the working directory and downloads Terraform configuration. They define resources,
provider plugins. providers, variables, outputs, and other
components. Like -Provider block ,resource block,
Prepares the environment for further Terraform
variable block ,output block,module , block,data
commands.
block.

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 Resource example AWS Example

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

Variable Precedence Azure Example

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

b) Explicit Dependency Azure Example


o Explicit dependencies are defined using the
depends_on argument to ensure that
certain resources are created or modified in
a specific order.
o Use this when Terraform cannot
automatically infer dependencies.
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.

Modules are reusable components that


encapsulate multiple Terraform resources. They
help in organizing configurations and avoiding code
duplication.

ANUPAM SHUKLA
AWS Example AWS -Create AWS instance

Azure Example

Azure- Create a resource group

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.

• If the configuration is valid, it will return


success.
• If the configuration is invalid, it will display
an error message indicating what needs to
be fixed.

ANUPAM SHUKLA
Terraform Condition
• Ternary Operator: Used to conditionally
assign values or select between two values.
Syntax

• for_each: Used to apply conditional logic


when creating multiple instances of a
resource.

Terraform code for creating github


repository

Write terraform code to execute a


command that prints your name in your
local system
Define null_resource to execute local command.

ANUPAM SHUKLA

You might also like