[go: up one dir, main page]

0% found this document useful (0 votes)
31 views7 pages

Terraform Question

The document provides a comprehensive overview of various Terraform commands and their uses, such as initializing a working directory, previewing changes, validating configurations, and applying or destroying infrastructure. It also discusses the tfstate file, its storage, locking mechanisms, dynamic blocks, variables, modules, outputs, provisioners, workspaces, and sensitive parameters. Additionally, it includes examples of Terraform templates for creating AWS resources like VPCs, subnets, and EC2 instances.
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)
31 views7 pages

Terraform Question

The document provides a comprehensive overview of various Terraform commands and their uses, such as initializing a working directory, previewing changes, validating configurations, and applying or destroying infrastructure. It also discusses the tfstate file, its storage, locking mechanisms, dynamic blocks, variables, modules, outputs, provisioners, workspaces, and sensitive parameters. Additionally, it includes examples of Terraform templates for creating AWS resources like VPCs, subnets, and EC2 instances.
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

write down the use of each command in terraform

terraform init :- initializes a working directory and downloads the necessary provider plugins and
modules and setting up the backend for storing your infrastructure's state.

terraform plan:- It is used to preview changes that Terraform will make to your infrastructure
before applying them

terraform validate:- To verify the correctness of Terraform configuration files. It checks the syntax
of the Terraform files, ensures the correct usage of attributes and values, and validates the
configuration based on the core syntax of Terraform and also by checking all the providers in the
code.

terraform apply:- It is used to create, update, or delete infrastructure resources based on the
changes specified in an Infrastructure-as-Code (IaC) configuration

terraform destroy:-It is used to remove the infrastructure that has been provisioned using
Terraform Infrastructure-as-Code(IaC) configuration

terraform state list:-It is used to list resources within a terraform state.

terraform fmt :- It is used to rewrite Terraform configuration files to a canonical format and style.

terraform import:-It is used to import existing infrastructure into Terraform state.

terraform replace or terraform taint:-It is used to informs Terraform that a particular object has
become degraded or damaged.

terraform destroy --target <resource name>:-

terraform refresh:-It is used to update the state file in Terraform to reflect the current state of
infrastructure that Terraform manages

## Questions, you should be able to do all the questions practically. then write it down with
examples.
how to check detail logs in terraform ?
Terraform has detailed logs that you can enable by setting the TF_LOG environment variable to
any value.There are 5 log levels. TRACE,DEBUG,INFO,WARN,ERROR.

what is mean by tfstate file ?


tfstate file contains information about the provisioned infrastructure which terraform manage.
Whenever we change the configuration file, it automatically determines which part of your
configuration is already created and which needs to be changed with help of state file
where do you store the tfstate file in your company ?
tfstate is stored by default in a local file named "terraform. tfstate". After running “terraform init”
command terraform tfstate file stored in local file.To check state file, we can use “terraform state
list” command.

explain terraform lifecycle ?

how do you store your tfstate file in S3 bucket ?


1. Go to the AWS Console
2. Go to S3
3. Create Bucket

With our bucket created let's head to our personal machine and create a new folder, we will cd into this

and create some files.

mkdir save-state-to-s

cd save-state-to-s3

touch main.tf
Insert the code below and save it. Make sure to add your bucket name to the backend. Also, this

is a very simple Terraform configuration file for this project, so no modules to keep it simple

Make sure to put in your bucket name and your table name here

terraform {

backend "s3" {

bucket = "terraform-s3-state-0223"

key = "my-terraform-project"

region = "us-east-1"

shared_credentials_file = "~/.aws/credentials"

provider "aws" {

region = "us-east-1"

shared_credentials_file = "~/.aws/credentials"

Terraform init
Terraform –auto-approve

explain me the concept of tfstate file locking ? how you will do it ?

Terraform will lock your state for all operations that could write state. This prevents others from

acquiring the lock and potentially corrupting your state.” This is helpful so that two people

cannot perform the same action at the same time and can prevent issues where two resources

of the same type may be deployed and then the state would be messed up.

explain dynamic block configuration , where you will use it ?

Dynamic Blocks in Terraform let you repeat configuration blocks inside a resource, provider,
provisioned, or data source based on a variable, local, or expression you use inside them.

what is use of count and count index ?


Count is used to create multiple instances of a resource or module, using a single configuration
block.
The count. index is a special variable that Terraform creates inside a resource block using count
argument. This helps you uniquely identify each instance of the resource. Indices start from 0
and grow by increments of 1 for each resource instance created.

what is use of variables in terraform ?


In Terraform, variables are used to make your infrastructure code more flexible, reusable, and
customizable. By defining variables, you allow the values for specific parameters of your
infrastructure (e.g., instance types, regions, or AMI IDs) to be defined outside the code, making
it easier to modify the configuration without changing the actual Terraform files.

have you used the concept of modules in your company ? how and why ?

Modules in Terraform are a collection of .tf files stored in a separate directory within the overall
configuration.

explain the use of mapping and list concept in variables ?


List and map variables in Terraform empower users to create dynamic, reusable configurations
for managing infrastructure.

what does the concept of outputs in terraform ?


Terraform outputs are structured data about resources that can be exported and used to
configure other parts of infrastructure. They are similar to return values in programming
languages.
what are the different type of provisioners in terraform ? explain with example ?
Terraform providers are plugins used to authenticate with cloud platforms, services or other
tools, allowing users to create, modify, and delete resources declared in the Terraform
configurations. Provisioners are used only for copying files or executing local or remote
operations.
There are three types of provisioners in Terraform
Local-exec provisioners
Remote-exec provisioners
File provisioners.

explain me the concept of workspace with example ?


A Terraform workspace is a way to create multiple instances of the same infrastructure in
different environments. For example, you may have a development environment , production
environment. Each environment may require the same infrastructure but with different
configurations.

what does .tfvars file means ?


A .tfvars file is a Terraform file that stores input variables for configurations. The variables in a
.tfvars file can be used to parameterize Terraform code and provide dynamic values.

can we have multiple provides in same template ?


Yes.

how does terraform comes to know that which version of aws provider needs to be used ?
Open the terraform.tf file. And then find a terraform block which specifies the required provider
version and required Terraform version for this configuration.

what happens in backend when you execute terrafrom init command ?


When you execute the terraform init command in Terraform, it initializes the working directory
containing Terraform configuration files and prepares it to interact with the specified backend.
This process involves several important steps, including backend initialization, provider
installation, and module initialization.

suppose you have launch vpc and subnet using template. now you delete the resource details
from tfstate file ? what impact will happen on those vpc and subnet ? will they be deleted or not
?
Yes they will be deleted.

suppose you have launch vpc , subnet & ec2 using template , now your manager says that
please change the instance type of the ec2 which you have created earlier using template. now
what
will happen if you update the template and say terraform apply ? new ec2 will be created or it
will update the same server ?
It will destroy the existing one and create a new ec2.
what is the purpose of using sensitive parameter in terraform ? explain with example ?
The sensitive parameter in Terraform is used to mark values as sensitive so that Terraform will
redact them in console and log output. This helps prevent accidental disclosure of sensitive
values.

Write one production ready terraform template to - create vpc, subnet,IGW,R.T,Security


group,EC2 .
Main.tf
# Creating a AWS EC2 Instance
resource "aws_instance" "server-instance" {
# Define number of instance
instance_count = var.number_of_instances

# Instance Configuration
ami = var.ami
instance_type = var.instance_type
subnet_id = var.subnet_id
vpc_security_group_ids = var.security_group

# Instance Tagsid
tags = {
Name = "${var.instance_name}"
}
}

Variable.tf
# Server Module Variables
variable "number_of_instances" {
description = "Number of Instances to Create"
type = number
default = 1
}

variable "instance_name" {
description = "Instance Name"
}

variable "ami" {
description = "AMI ID"
default = "ami-xxxx"
}

variable "instance_type" {
description = "Instance Type"
}

variable "subnet_id" {
description = "Subnet ID"
}

variable "security_group" {
description = "Security Group"
type = list(any)
}

# Server Module Output


output "server_id" {
description = "Server ID"
value = aws_instance.server-instance.id
}

You might also like