Terraform Interview QA
Terraform Interview QA
1. What is Terraform?
Terraform is an open-source infrastructure as code (IaC) tool developed by HashiCorp that allows
users to define and provision data center infrastructure using a high-level configuration language
called HCL (HashiCorp Configuration Language) or JSON.
Example:
provider "aws" {
region = "us-west-2"
}
Terraform's main features include Infrastructure as Code (IaC), execution plans, resource graphs,
change automation, and state management.
Example:
terraform {
backend "s3" {
bucket = "my-terraform-state"
key = "global/s3/terraform.tfstate"
region = "us-west-2"
}
}
3. What is the difference between Terraform and other IaC tools like
Ansible, Puppet, and Chef?
Terraform focuses on infrastructure provisioning, is declarative, and uses HCL. Tools like Ansible,
Puppet, and Chef focus on configuration management and are procedural.
Example:
A provider is a plugin that Terraform uses to manage an external API. Providers define the
resources and data sources available.
Example:
provider "aws" {
region = "us-west-2"
}
Example:
A state file is a file that Terraform uses to keep track of the current state of the infrastructure. It
maps the resources defined in the configuration to the real-world resources.
Example:
terraform show
Example:
terraform init
State files can be secured by storing them in remote backends with proper access controls and
encryption, such as AWS S3 with server-side encryption and access control policies.
Example:
terraform {
backend "s3" {
bucket = "my-terraform-state"
key = "global/s3/terraform.tfstate"
region = "us-west-2"
encrypt = true
}
}
Modules are reusable packages of Terraform configurations that can be shared and composed to
manage resources efficiently.
Example:
module "vpc" {
source = "./modules/vpc"
}
Example:
terraform init
terraform plan creates an execution plan, showing what actions Terraform will take to achieve
the desired state defined in the configuration.
Example:
terraform plan
12. What is the terraform apply command used for?
terraform apply applies the changes required to reach the desired state of the configuration.
It executes the plan created by terraform plan.
Example:
terraform apply
terraform destroy is used to destroy the infrastructure managed by Terraform. It removes all
the resources defined in the configuration.
Example:
terraform destroy
Variables in Terraform are defined using the variable block and can be used by referring to
them with var.<variable_name>.
Example:
variable "instance_type" {
description = "Type of EC2 instance"
default = "t2.micro"
}
15. What are output values in Terraform and how are they used?
Output values are used to extract information from the resources and make it accessible after
the apply phase. They can be used to output resource attributes.
Example:
output "instance_id" {
value = aws_instance.example.id
}
Example:
Remote state allows Terraform to store the state file in a remote storage backend, enabling team
collaboration and secure storage.
Example:
terraform {
backend "s3" {
bucket = "my-terraform-state"
key = "global/s3/terraform.tfstate"
region = "us-west-2"
}
}
Existing resources can be imported using the terraform import command, which maps the
existing resource to a Terraform resource in the state file.
Example:
Data sources allow Terraform to fetch data from existing infrastructure or services to use in
resource definitions.
Example:
filter {
name = "name"
values = ["amzn-ami-hvm-*"]
}
}
Example:
provisioner "local-exec" {
command = "echo ${self.public_ip} > ip_address.txt"
}
}
Secrets can be managed using environment variables, secure secret management services (e.g.,
AWS Secrets Manager), or Terraform's sensitive attribute.
Example:
A backend in Terraform defines where and how state is loaded and stored. It can be local or
remote (e.g., S3, Consul, etc.).
Example:
terraform {
backend "s3" {
bucket = "my-terraform-state"
key = "global/s3/terraform.tfstate"
region = "us-west-2"
}
}
Conditional expressions in Terraform are used to assign values based on conditions using the
ternary operator condition ? true_value : false_value.
Example:
variable "environment" {
default = "dev"
}
terraform validate is used to validate the syntax and configuration of the Terraform files
without creating any resources.
Example:
terraform validate
Terraform configuration files can be formatted using the terraform fmt command, which
formats the files according to the
Example:
terraform fmt
count is used to create multiple instances of a resource, while for_each is used to iterate over a
map or set of values to create multiple instances.
Example (count):
Loops in Terraform can be implemented using the count and for_each meta-arguments, as
well as the for expression in variable assignments.
Example:
variable "instance_names" {
type = list(string)
default = ["instance1", "instance2"]
}
28. What are locals in Terraform and how do you use them?
Locals in Terraform are used to define local values that can be reused within a module. They help
avoid repetition and make configurations more readable.
Example:
locals {
instance_type = "t2.micro"
ami_id = "ami-0c55b159cbfafe1f0"
}
terraform taint marks a resource for recreation on the next terraform apply. It is useful
when a resource needs to be replaced due to a manual change or corruption.
Example:
Example:
module "vpc" {
source = "terraform-aws-modules/vpc/aws"
version = "2.0.0"
}
The Terraform Registry is a public repository of Terraform modules and providers that can be
used to discover and use pre-built modules and providers.
Example:
module "vpc" {
source = "terraform-aws-modules/vpc/aws"
version = "2.0.0"
}
A dry run in Terraform can be performed using the terraform plan command, which shows
the execution plan without making any changes.
Example:
terraform plan
The terraform state command is used to manage and manipulate the state file. It provides
subcommands to move, remove, list, and inspect resources in the state file.
Example:
A resource can be renamed in the state file using the terraform state mv command, which
moves the state of a resource to a new address.
Example:
The terraform workspace command is used to manage multiple workspaces, allowing for
different states to be associated with the same configuration.
Example:
terraform workspace new dev
terraform workspace select dev
Debugging Terraform configurations can be done using the TF_LOG environment variable to set
the log level and the terraform console command to interact with the configuration.
Example:
export TF_LOG=DEBUG
terraform apply
Local backends store the state file on the local filesystem, while remote backends store the state
file in a remote storage service (e.g., S3, Consul).
terraform {
backend "local" {
path = "terraform.tfstate"
}
}
Example (remote backend):
terraform {
backend "s3" {
bucket = "my-terraform-state"
key = "global/s3/terraform.tfstate"
region = "us-west-2"
}
}
Example:
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 3.0"
}
}
}
Example:
terraform refresh
A resource graph can be generated using the terraform graph command and can be viewed
using tools like Graphviz.
Example:
lifecycle blocks in Terraform are used to customize the lifecycle of a resource, such as creating
before destroying, ignoring changes, and preventing deletion.
Example:
lifecycle {
create_before_destroy = true
}
}
Example:
lifecycle {
ignore_changes = [ami]
}
}
terraform import is used to import existing infrastructure into Terraform's state file, mapping
it to resources defined in the configuration.
Example:
44. How do you use output values across different modules in Terraform?
Output values from one module can be referenced in another module by using the module's
output attributes.
Example:
module "vpc" {
source = "./modules/vpc"
}
output "vpc_id" {
value = module.vpc.vpc_id
}
45. What is the difference between terraform output and output values in
configuration?
terraform output is a command that displays the output values of a Terraform configuration,
while output values in configuration are defined using the output block.
Example (command):
terraform output
Example (configuration):
output "instance_id" {
value = aws_instance.example.id
}
Dynamic blocks in Terraform are used to generate multiple nested blocks within a resource or
module based on dynamic content.
Example:
dynamic "ingress" {
for_each = var.ingress_rules
content {
from_port = ingress.value.from_port
to_port = ingress.value.to_port
protocol = ingress.value.protocol
cidr_blocks = ingress.value.cidr_blocks
}
}
}
Maps in Terraform are defined using the map type and can be used to store key-value pairs. They
are accessed using the key.
Example:
variable "ami_ids" {
type = map(string)
default = {
us-east-1 = "ami-0c55b159cbfafe1f0"
us-west-2 = "ami-0d5eff06f840b45e9"
}
}
The
Example:
Terraform Cloud and Terraform Enterprise are commercial versions of Terraform that provide
collaboration, governance, and automation features.
Example:
terraform {
backend "remote" {
organization = "my-org"
workspaces {
name = "my-workspace"
}
}
}
50. How do you use a Terraform backend?
A backend is configured using the terraform block in the configuration file, specifying the
backend type and its configuration.
Example:
terraform {
backend "s3" {
bucket = "my-terraform-state"
key = "global/s3/terraform.tfstate"
region = "us-west-2"
}
}
terraform fmt formats the configuration files to follow the Terraform style guide, making the
code consistent and readable.
Example:
terraform fmt
Example:
terraform init
The terraform workspace command is used to create, select, and manage multiple
workspaces, allowing different states to be associated with the same configuration.
Example:
Secrets can be managed using environment variables, secure secret management services (e.g.,
AWS Secrets Manager), or Terraform's sensitive attribute.
Example:
resource "aws_secretsmanager_secret" "example" {
name = "example"
description = "An example secret"
}
Example:
terraform console
Data sources are referenced using the data block and can be used to fetch information about
existing infrastructure or services.
Example:
filter {
name = "name"
values = ["amzn-ami-hvm-*"]
}
}
terraform state mv moves a resource in the state file to a new address, useful for renaming
resources without recreating them.
Example:
Example:
variable "environment" {
default = "dev"
}
terraform taint marks a resource for recreation on the next terraform apply. It is useful
when a resource needs to be replaced due to a manual change or corruption.
Example:
Maps in Terraform are defined using the map type and can be used to store key-value pairs. They
are accessed using the key.
Example:
variable "ami_ids" {
type = map(string)
default = {
us-east-1 = "ami-0c55b159cbfafe1f0"
us-west-2 = "ami-0d5eff06f840b45e9"
}
}
Example:
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 3.0"
}
}
}
terraform refresh updates the state file with the current state of the infrastructure without
making any changes to the configuration.
Example:
terraform refresh
lifecycle blocks in Terraform are used to customize the lifecycle of a resource, such as creating
before destroying, ignoring changes, and preventing deletion.
Example:
lifecycle {
create_before_destroy = true
}
}
Loops in Terraform can be implemented using the count and for_each meta-arguments, as
well as the for expression in variable assignments.
Example:
variable "instance_names" {
type = list(string)
default = ["instance1", "instance2"]
}
Example:
66. How do you use output values across different modules in Terraform?
Output values from one module can be referenced in another module by using the module's
output attributes.
Example:
module "vpc" {
source = "./modules/vpc"
}
output "vpc_id" {
value = module.vpc.vpc_id
}
67. What is the difference between terraform output and output values in
configuration?
terraform output is a command that displays the output values of a Terraform configuration,
while output values in configuration are defined using the output block.
Example (command):
terraform output
Example (configuration):
output "instance_id" {
value = aws_instance.example.id
}
Dynamic blocks in Terraform are used to generate multiple nested blocks within a resource or
module based on dynamic content.
Example:
dynamic "ingress" {
for_each = var.ingress_rules
content {
from_port = ingress.value.from_port
to_port = ingress.value.to_port
protocol = ingress.value.protocol
cidr_blocks = ingress.value.cidr_blocks
}
}
}
Different environments can be managed using workspaces or separate directories with different
variable files and state files.
Example:
Secrets can be managed using environment variables, secure secret management services (e.g.,
AWS Secrets Manager), or Terraform's sensitive attribute.
Example:
configurations.
Example:
terraform console
Example:
filter {
name = "name"
values = ["amzn-ami-hvm-*"]
}
}
terraform state mv moves a resource in the state file to a new address, useful for renaming
resources without recreating them.
Example:
A backend in Terraform defines where and how state is loaded and stored. It can be local or
remote (e.g., S3, Consul, etc.).
Example:
terraform {
backend "s3" {
bucket = "my-terraform-state"
key = "global/s3/terraform.tfstate"
region = "us-west-2"
}
}
State files can be secured by storing them in remote backends with proper access controls and
encryption, such as AWS S3 with server-side encryption and access control policies.
Example:
terraform {
backend "s3" {
bucket = "my-terraform-state"
key = "global/s3/terraform.tfstate"
region = "us-west-2"
encrypt = true
}
}
Local backends store the state file on the local filesystem, while remote backends store the state
file in a remote storage service (e.g., S3, Consul).
terraform {
backend "local" {
path = "terraform.tfstate"
}
}
Example (remote backend):
terraform {
backend "s3" {
bucket = "my-terraform-state"
key = "global/s3/terraform.tfstate"
region = "us-west-2"
}
}
Example:
module "vpc" {
source = "terraform-aws-modules/vpc/aws"
version = "2.0.0"
}
The Terraform Registry is a public repository of Terraform modules and providers that can be
used to discover and use pre-built modules and providers.
Example:
module "vpc" {
source = "terraform-aws-modules/vpc/aws"
version = "2.0.0"
}
79. How do you generate and view a resource graph in Terraform?
A resource graph can be generated using the terraform graph command and can be viewed
using tools like Graphviz.
Example:
terraform validate is used to validate the syntax and configuration of the Terraform files
without creating any resources.
Example:
terraform validate
terraform fmt formats the configuration files to follow the Terraform style guide, making the
code consistent and readable.
Example:
terraform fmt
82. What are locals in Terraform and how do you use them?
Locals in Terraform are used to define local values that can be reused within a module. They help
avoid repetition and make configurations more readable.
Example:
locals {
instance_type = "t2.micro"
ami_id = "ami-0c55b159cbfafe1f0"
}
Example:
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 3.0"
}
}
}
terraform apply applies the changes required to reach the desired state of the configuration.
It executes the plan created by terraform plan.
Example:
terraform apply
terraform destroy is used to destroy the infrastructure managed by Terraform. It removes all
the resources defined in the configuration.
Example:
terraform destroy
86. What are output values in Terraform and how are they used?
Output values are used to extract information from the resources and make it accessible after
the apply phase. They can be used to output resource attributes.
Example:
output "instance_id" {
value = aws_instance.example.id
}
Different environments can be managed using workspaces or separate directories with different
variable files and state files.
Example:
Example (count):
Loops in Terraform can be implemented using the count and for_each meta-arguments, as
well as the for expression in variable assignments.
Example:
variable "instance_names" {
type = list(string)
default = ["instance1", "instance2"]
}
The count parameter in Terraform is used to create multiple instances of a resource based on a
specified number.
Example:
Terraform Cloud and Terraform Enterprise are commercial versions of Terraform that provide
collaboration, governance, and automation features.
Example:
terraform {
backend "remote" {
organization = "my-org"
workspaces {
name = "my-workspace"
}
}
}
A backend is configured using the terraform block in the configuration file, specifying the
backend type and its configuration.
Example:
terraform {
backend "s3" {
bucket = "my-terraform-state"
key = "global/s3/terraform.tfstate"
region = "us-west-2"
}
}
terraform fmt formats the configuration files to follow the Terraform style guide, making the
code consistent and readable.
Example:
terraform fmt
Example:
terraform init
Example:
in Terraform?**
Secrets can be managed using environment variables, secure secret management services (e.g.,
AWS Secrets Manager), or Terraform's sensitive attribute.
Example:
Example:
terraform console
Data sources are referenced using the data block and can be used to fetch information about
existing infrastructure or services.
Example:
filter {
name = "name"
values = ["amzn-ami-hvm-*"]
}
}
terraform state mv moves a resource in the state file to a new address, useful for renaming
resources without recreating them.
Example:
A backend in Terraform defines where and how state is loaded and stored. It can be local or
remote (e.g., S3, Consul, etc.).
Example:
terraform {
backend "s3" {
bucket = "my-terraform-state"
key = "global/s3/terraform.tfstate"
region = "us-west-2"
}
}
State files can be secured by storing them in remote backends with proper access controls and
encryption, such as AWS S3 with server-side encryption and access control policies.
Example:
terraform {
backend "s3" {
bucket = "my-terraform-state"
key = "global/s3/terraform.tfstate"
region = "us-west-2"
encrypt = true
}
}
Local backends store the state file on the local filesystem, while remote backends store the state
file in a remote storage service (e.g., S3, Consul).
Example (local backend):
terraform {
backend "local" {
path = "terraform.tfstate"
}
}
Example (remote backend):
terraform {
backend "s3" {
bucket = "my-terraform-state"
key = "global/s3/terraform.tfstate"
region = "us-west-2"
}
}
Example:
module "vpc" {
source = "terraform-aws-modules/vpc/aws"
version = "2.0.0"
}
The Terraform Registry is a public repository of Terraform modules and providers that can be
used to discover and use pre-built modules and providers.
Example:
module "vpc" {
source = "terraform-aws-modules/vpc/aws"
version = "2.0.0"
}
A resource graph can be generated using the terraform graph command and can be viewed
using tools like Graphviz.
Example:
Example:
terraform validate
terraform fmt formats the configuration files to follow the Terraform style guide, making the
code consistent and readable.
Example:
terraform fmt
108. What are locals in Terraform and how do you use them?
Locals in Terraform are used to define local values that can be reused within a module. They help
avoid repetition and make configurations more readable.
Example:
locals {
instance_type = "t2.micro"
ami_id = "ami-0c55b159cbfafe1f0"
}
Example:
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 3.0"
}
}
}
Example:
terraform apply
terraform destroy is used to destroy the infrastructure managed by Terraform. It removes all
the resources defined in the configuration.
Example:
terraform destroy
112. What are output values in Terraform and how are they used?
Output values are used to extract information from the resources and make it accessible after
the apply phase. They can be used to output resource attributes.
Example:
output "instance_id" {
value = aws_instance.example.id
}
Different environments can be managed using workspaces or separate directories with different
variable files and state files.
Example:
count is used to create multiple instances of a resource, while for_each is used to iterate over a
map or set of values to create multiple instances.
Example (count):
Loops in Terraform can be implemented using the count and for_each meta-arguments, as
well as the for expression in variable assignments.
Example:
variable "instance_names" {
type = list(string)
default = ["instance1", "instance2"]
}
The count parameter in Terraform is used to create multiple instances of a resource based on a
specified number.
Example:
Terraform Cloud and Terraform Enterprise are commercial versions of Terraform that provide
collaboration, governance, and automation features.
Example:
terraform {
backend "remote" {
organization = "my-org"
workspaces {
name = "my-workspace"
}
}
}
A backend is configured using the terraform block in the configuration file, specifying the
backend type and its configuration.
Example:
terraform {
backend "s3" {
bucket = "my-terraform-state"
key = "global/s3/terraform.tfstate"
region = "us-west-2"
}
}
to follow the Terraform style guide, making the code consistent and readable.
Example:
terraform fmt
Example:
terraform init
The terraform workspace command is used to create, select, and manage multiple
workspaces, allowing different states to be associated with the same configuration.
Example:
Secrets can be managed using environment variables, secure secret management services (e.g.,
AWS Secrets Manager), or Terraform's sensitive attribute.
Example:
Example:
terraform console
Data sources are referenced using the data block and can be used to fetch information about
existing infrastructure or services.
Example:
filter {
name = "name"
values = ["amzn-ami-hvm-*"]
}
}
Example:
A backend in Terraform defines where and how state is loaded and stored. It can be local or
remote (e.g., S3, Consul, etc.).
Example:
terraform {
backend "s3" {
bucket = "my-terraform-state"
key = "global/s3/terraform.tfstate"
region = "us-west-2"
}
}
State files can be secured by storing them in remote backends with proper access controls and
encryption, such as AWS S3 with server-side encryption and access control policies.
Example:
terraform {
backend "s3" {
bucket = "my-terraform-state"
key = "global/s3/terraform.tfstate"
region = "us-west-2"
encrypt = true
}
}
Local backends store the state file on the local filesystem, while remote backends store the state
file in a remote storage service (e.g., S3, Consul).
terraform {
backend "local" {
path = "terraform.tfstate"
}
}
Example (remote backend):
terraform {
backend "s3" {
bucket = "my-terraform-state"
key = "global/s3/terraform.tfstate"
region = "us-west-2"
}
}
Example:
module "vpc" {
source = "terraform-aws-modules/vpc/aws"
version = "2.0.0"
}
The Terraform Registry is a public repository of Terraform modules and providers that can be
used to discover and use pre-built modules and providers.
Example:
module "vpc" {
source = "terraform-aws-modules/vpc/aws"
version = "2.0.0"
}
A resource graph can be generated using the terraform graph command and can be viewed
using tools like Graphviz.
Example:
terraform validate is used to validate the syntax and configuration of the Terraform files
without creating any resources.
Example:
terraform validate
Example:
terraform fmt
134. What are locals in Terraform and how do you use them?
Locals in Terraform are used to define local values that can be reused within a module. They help
avoid repetition and make configurations more readable.
Example:
locals {
instance_type = "t2.micro"
ami_id = "ami-0c55b159cbfafe1f0"
}
Example:
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 3.0"
}
}
}
terraform apply applies the changes required to reach the desired state of the configuration.
It executes the plan created by terraform plan.
Example:
terraform apply
Example:
terraform destroy
138. What are output values in Terraform and how are they used?
Output values are used to extract information from the resources and make it accessible after
the apply phase. They can be used to output resource attributes.
Example:
output "instance_id" {
value = aws_instance.example.id
}
Different environments can be managed using workspaces or separate directories with different
variable files and state files.
Example:
count is used to create multiple instances of a resource, while for_each is used to iterate over a
map or set of values to create multiple instances.
Example (count):
Terraform Cloud and Terraform Enterprise are commercial versions of Terraform that provide
collaboration, governance, and automation features.
Example:
terraform {
backend "remote" {
organization = "my-org"
workspaces {
name = "my-workspace"
}
}
}
A backend is configured using the terraform block in the configuration file, specifying the
backend type and its configuration.
Example:
terraform {
backend "s3" {
bucket = "my-terraform-state"
key = "global/s3/terraform.tfstate"
region = "us-west-2"
}
}
terraform fmt formats the configuration files to follow the Terraform style guide, making the
code consistent and readable.
Example:
terraform fmt
Example:
terraform init
Example:
Secrets can be managed using environment variables, secure secret management services (e.g.,
AWS Secrets Manager), or Terraform's sensitive attribute.
Example:
Example:
terraform console
Data sources are referenced using the data block and can be used to fetch information about
existing infrastructure or services.
Example:
filter {
name = "name"
values = ["amzn-ami-hvm-*"]
}
}
resource "aws_instance" "example" {
ami = data.aws_ami.example.id
instance_type = "t2.micro"
}
terraform state mv moves a resource in the state file to a new address, useful for renaming
resources without recreating them.
Example:
A backend in Terraform defines where and how state is loaded and stored. It can be local or
remote (e.g., S3, Consul, etc.).
Example:
terraform {
backend "s3" {
bucket = "my-terraform-state"
key = "global/s3/terraform.tfstate"
region = "us-west-2"
}
}
State files can be secured by storing them in remote backends with proper access controls and
encryption, such as AWS S3 with server-side encryption and access control policies.
Example:
terraform {
backend "s3" {
bucket = "my-terraform-state"
key = "global/s3/terraform.tfstate"
region = "us-west-2"
encrypt = true
}
}
Local backends store the state file on the local filesystem, while remote backends store the state
file in a remote storage service (e.g., S3, Consul).
Example (local backend):
terraform {
backend "local" {
path = "terraform.tfstate"
}
}
Example (remote backend):
terraform {
backend "s3" {
bucket = "my-terraform-state"
key = "global/s3/terraform.tfstate"
region = "us-west-2"
}
}
Example:
module "vpc" {
source = "terraform-aws-modules/vpc/aws"
version = "2.0.0"
}
The Terraform Registry is a public repository of Terraform modules and providers that can be
used to discover and use pre-built modules and providers.
Example:
module "vpc" {
source = "terraform-aws-modules/vpc/aws"
version = "2.0.0"
}
A resource graph can be generated using the terraform graph command and can be viewed
using tools like Graphviz.
Example:
Example:
terraform validate
terraform fmt formats the configuration files to follow the Terraform style guide, making the
code consistent and readable.
Example:
terraform fmt
160. What are locals in Terraform and how do you use them?
Locals in Terraform are used to define local values that can be reused within a module. They help
avoid repetition and make configurations more readable.
Example:
locals {
instance_type = "t2.micro"
ami_id = "ami-0c55b159cbfafe1f0"
}
Example:
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 3.0"
}
}
}
Example:
terraform apply
terraform destroy is used to destroy the infrastructure managed by Terraform. It removes all
the resources defined in the configuration.
Example:
terraform destroy
164. What are output values in Terraform and how are they used?
Output values are used to extract information from the resources and make it accessible after
the apply phase. They can be used to output resource attributes.
Example:
output "instance_id" {
value = aws_instance.example.id
}
Different environments can be managed using workspaces or separate directories with different
variable files and state files.
Example:
count is used to create multiple instances of a resource, while for_each is used to iterate over a
map or set of values to create multiple instances.
Example (count):
Loops in Terraform can be implemented using the count and for_each meta-arguments, as
well as the for expression in variable assignments.
Example:
variable "instance_names" {
type = list(string)
default = ["instance1", "instance2"]
}
resource "
aws_instance" "example" {
for_each = toset(var.instance_names)
ami = "ami-0c55b159cbfafe1f0"
instance_type = "t2.micro"
tags = {
Name = each.key
}
}
The count parameter in Terraform is used to create multiple instances of a resource based on a
specified number.
Example:
Terraform Cloud and Terraform Enterprise are commercial versions of Terraform that provide
collaboration, governance, and automation features.
Example:
terraform {
backend "remote" {
organization = "my-org"
workspaces {
name = "my-workspace"
}
}
}
A backend is configured using the terraform block in the configuration file, specifying the
backend type and its configuration.
Example:
terraform {
backend "s3" {
bucket = "my-terraform-state"
key = "global/s3/terraform.tfstate"
region = "us-west-2"
}
}
terraform fmt formats the configuration files to follow the Terraform style guide, making the
code consistent and readable.
Example:
terraform fmt
Example:
terraform init
The terraform workspace command is used to create, select, and manage multiple
workspaces, allowing different states to be associated with the same configuration.
Example:
Secrets can be managed using environment variables, secure secret management services (e.g.,
AWS Secrets Manager), or Terraform's sensitive attribute.
Example:
Example:
terraform console
Data sources are referenced using the data block and can be used to fetch information about
existing infrastructure or services.
Example:
filter {
name = "name"
values = ["amzn-ami-hvm-*"]
}
}
Example:
A backend in Terraform defines where and how state is loaded and stored. It can be local or
remote (e.g., S3, Consul, etc.).
Example:
terraform {
backend "s3" {
bucket = "my-terraform-state"
key = "global/s3/terraform.tfstate"
region = "us-west-2"
}
}
State files can be secured by storing them in remote backends with proper access controls and
encryption, such as AWS S3 with server-side encryption and access control policies.
Example:
terraform {
backend "s3" {
bucket = "my-terraform-state"
key = "global/s3/terraform.tfstate"
region = "us-west-2"
encrypt = true
}
}
Local backends store the state file on the local filesystem, while remote backends store the state
file in a remote storage service (e.g., S3, Consul).
terraform {
backend "local" {
path = "terraform.tfstate"
}
}
Example (remote backend):
terraform {
backend "s3" {
bucket = "my-terraform-state"
key = "global/s3/terraform.tfstate"
region = "us-west-2"
}
}
Example:
module "vpc" {
source = "terraform-aws-modules/vpc/aws"
version = "2.0.0"
}
The Terraform Registry is a public repository of Terraform modules and providers that can be
used to discover and use pre-built modules and providers.
Example:
module "vpc" {
source = "terraform-aws-modules/vpc/aws"
version = "2.0.0"
}
A resource graph can be generated using the terraform graph command and can be viewed
using tools like Graphviz.
Example:
terraform validate is used to validate the syntax and configuration of the Terraform files
without creating any resources.
Example:
terraform validate
Example:
terraform fmt
186. What are locals in Terraform and how do you use them?
Locals in Terraform are used to define local values that can be reused within a module. They help
avoid repetition and make configurations more readable.
Example:
locals {
instance_type = "t2.micro"
ami_id = "ami-0c55b159cbfafe1f0"
}
Example:
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 3.0"
}
}
}
terraform apply applies the changes required to reach the desired state of the configuration.
It executes the plan created by terraform plan.
Example:
terraform apply
Example:
terraform destroy
190. What are output values in Terraform and how are they used?
Output values are used to extract information from the resources and make it accessible after
the apply phase. They can be used to output resource attributes.
Example:
output "instance_id" {
value = aws_instance.example.id
}
Different environments can be managed using workspaces or separate directories with different
variable files and state files.
Example:
count is used to create multiple instances of a resource, while for_each is used to iterate over a
map or set of values to create multiple instances.
Example (count):
_each):
Loops in Terraform can be implemented using the count and for_each meta-arguments, as
well as the for expression in variable assignments.
Example:
variable "instance_names" {
type = list(string)
default = ["instance1", "instance2"]
}
The count parameter in Terraform is used to create multiple instances of a resource based on a
specified number.
Example:
Terraform Cloud and Terraform Enterprise are commercial versions of Terraform that provide
collaboration, governance, and automation features.
Example:
terraform {
backend "remote" {
organization = "my-org"
workspaces {
name = "my-workspace"
}
}
}
Example:
terraform {
backend "s3" {
bucket = "my-terraform-state"
key = "global/s3/terraform.tfstate"
region = "us-west-2"
}
}
terraform fmt formats the configuration files to follow the Terraform style guide, making the
code consistent and readable.
Example:
terraform fmt
Example:
terraform init
The terraform workspace command is used to create, select, and manage multiple
workspaces, allowing different states to be associated with the same configuration.
Example:
Secrets can be managed using environment variables, secure secret management services (e.g.,
AWS Secrets Manager), or Terraform's sensitive attribute.
Example: