Create, Read, Configure TF Flashcards
What is the purpose of a resource block in Terraform?
A resource block in Terraform is used to define and provision a specific resource in your infrastructure, such as an AWS EC2 instance or an S3 bucket. It specifies the resource type
resource "aws_instance" "example" { ami = "ami-0c94855ba95c71c99" instance_type = "t2.micro" subnet_id = "subnet-12345678" }
What is the purpose of a data block in Terraform?
It is used to fetch data outside of your config that can be used for referencing, decision-making, or configuration purposes, without creating or modifying any resources.
data "aws_s3_bucket" "example" { bucket = "example-bucket" }
What is the purpose of the variable block in Terraform?
The variable block in Terraform is like a placeholder for values that can be used in your Terraform configuration. It lets you pass in different values when you run Terraform, so you can customize how your infrastructure gets created.
It allows you to pass values dynamically during the execution, making your configurations more flexible and reusable.
What is the purpose of the module block in Terraform?
In Terraform, a module is a collection of reusable code. It encapsulates a specific set of resources, configurations, and logic that represent a well-defined component, such as an AWS VPC or an application stack.
Modules allow you to easily include pre-built infrastructure components into your Terraform configuration, making it quicker and more efficient to set up and manage complex infrastructure environments.
For example, you can use a module block to include a pre-built module for provisioning an AWS VPC.
module "vpc" { source = "terraform-aws-modules/vpc/aws" version = "2.0.0" # Module-specific input variables vpc_cidr = "10.0.0.0/16" }
What is the purpose of the output block in Terraform?
The output block is used to define values that will be exposed as outputs after the Terraform configuration is applied. It allows you to retrieve and reference these values from other Terraform configurations or as part of the Terraform output.
For example, you can define an output block to expose the DNS name or the ARN of an AWS resource.
~~~
output “bucket_arn” {
value = aws_s3_bucket.example.arn
}
~~~
What is the purpose of the terraform block in Terraform?
The terraform block is used to configure various settings and behaviors of the Terraform execution. It allows you to set the required Terraform version, configure backend settings for remote state storage, define provider requirements, and enable/disable certain features.
terraform { required_version = ">= 1.0.0" backend "s3" { bucket = "terraform-state" key = "project/state.tfstate" region = "us-west-2" } }
What is the purpose of the terraform init command?
The terraform init command is used to initialize a Terraform project in a directory.
What happens when you run the terraform init
cmd?
It downloads the necessary provider plugins, and initializes the backend for state storage.
What is the purpose of the terraform workspace command?
Is used to manage Terraform workspaces. Workspaces allow you to maintain multiple instances of your infrastructure, such as development, staging, and production, within a single Terraform configuration. The command helps in creating, switching, or deleting workspaces.
What are the three types of Terraform providers?
“Core Providers,” “Community Providers,” and “Partner Providers.”
What is the purpose of the terraform refresh command?
A:
The terraform refresh command is used to reconcile the Terraform state with the actual resources in the cloud provider. It retrieves the latest state of the resources by querying the provider’s API and updates the Terraform state accordingly. It does not make any changes to the infrastructure, but ensures that the state is up to date for subsequent operations.
What is the purpose of the terraform taint command?
Tainting a resource means considering it as potentially corrupted or unreliable. When a resource is tainted, Terraform treats it as if it were destroyed and will recreate it during the next terraform apply run.
You may need to run terraform init
a second time in the following scenarios:
Adding or updating provider plugins:
Switching backend configurations:
Resolving issues with the initialization process:
A resource block in Terraform consists of the following components:
Resource Type: The type of resource being created or managed.
Example: aws_instance, which represents an Amazon EC2 instance in AWS.
Resource Name: A unique identifier for the resource within the Terraform configuration.
Example: “example_instance”, a custom name given to the EC2 instance.
Resource Arguments: The configuration settings and properties specific to the resource type.
Example: ami = “ami-0123456789” and instance_type = “t2.micro”, which define the Amazon Machine Image (AMI) and instance type for the EC2 instance.
resource "aws_instance" "example_instance" { ami = "ami-0123456789" instance_type = "t2.micro" }
What are the components of an output block?
Components:
Output Name: A unique identifier for the output within the Terraform configuration.
Output Value: The value or attribute that you want to output
output "instance_id" { value = aws_instance.example.id }