Terraform Full Notes
Terraform Full Notes
State Management: Terraform maintains a state file that keeps track of the current state of
the infrastructure. This file is used to plan and apply changes, ensuring that Terraform can
update resources accurately.
Plan and Apply Workflow: Before making changes, Terraform generates an execution plan,
showing what actions it will take. Users review the plan and then apply it to make the changes
to the infrastructure.
Version Control Integration: Terraform configurations can be versioned using version control
systems like Git. This allows for collaboration, code review, and tracking changes over time.
Community and Ecosystem: Terraform has a vibrant community and a rich ecosystem of
modules and providers contributed by the community, making it easier to leverage pre-built
solutions for common infrastructure components.
Immutable Infrastructure: Terraform encourages the concept of immutable infrastructure,
where changes to infrastructure are made by replacing existing resources rather than
modifying them in place.
WHAT IS IAAC:
ALTERNATIVES OF TERRAFORM:
wget https://releases.hashicorp.com/terraform/1.1.3/terraform_1.1.3_linux_amd64.zip
sudo apt-get install zip -y
Unzip terraform
mv terraform /usr/local/bin/
terraform version
TERRAFORM LIFECYCLE:
The Terraform lifecycle refers to the sequence of steps and processes that occur when
working with Terraform to manage infrastructure as code. Here's an overview of the typical
Terraform lifecycle:
Write Configuration:
Initialize:
Run terraform init to initialize a Terraform working directory. This step downloads the
necessary providers and sets up the backend.
Plan:
Run terraform plan to create an execution plan. Terraform compares the desired state
from the configuration with the current state and generates a plan for the changes
required to reach the desired state.
Review Plan:
Examine the output of the plan to understand what changes Terraform intends to
make to the infrastructure. This is an opportunity to verify the planned changes
before applying them.
Apply:
Execute terraform apply to apply the changes outlined in the plan. Terraform makes
the necessary API calls to create, update, or delete resources to align the
infrastructure with the desired state.
Destroy (Optional):
main.tf file using terraform state listso it will gives the list of entire resources
Output Values are like return values for a Terraform module. Local Values are a convenience
feature for assigning a short name to an expression.
TERRAFORM STRING:
It seems like your question might be incomplete or unclear. If you are looking for information
about working with strings in Terraform, I can provide some guidance.
In Terraform, strings are used to represent text data and can be manipulated using various
functions and operators
TERRAFORM NUMBER: The number type can represent both whole numbers and
fractional values .
TERRAFORM BOOLEAN: a boolean represents a binary value indicating either true or
false. Booleans are used to express logical conditions, make decisions, and control the flow of
Terraform configurations. In HashiCorp Configuration Language (HCL), which is used for
writing Terraform configurations, boolean values are written as true or false.
LIST/TUPLE:
MAP/OBJECT:
FOR LOOP:
The for loop is pretty simple and if you have used any programming language before then I
guess you will be pretty much familiar with the for loop.
Only the difference you will notice over here is the syntax in Terraform.
We are going to take the same example by declaring a list(string) and adding three users to it -
user1, user2, user3
FOR EACH:
The for each is a little special in terraforming and you can not use it on any collection
variable.
The reason why for each does not work on list(string) is because a list can contain duplicate
values but if you are using set(string) or map(string) then it does not support duplicate
values.
we need to use count but to use the count first we need to declare collections inside our file.
LAUNCH EC2 INSTANCE WITH SG:
TERRAFORM CLI: to pass values form command line during run time
provider "aws" {
ami = "ami-0715c1897453cabd1"
instance_type = var.instance_type
tags = {
Name = "web-server"
variable "instance_type" {
provider "aws" {
ami = "ami-0715c1897453cabd1"
instance_type = "t2.micro"
tags = {
Name = "web-server"
output "abc" {
provider "aws" {
region = "us-east-1"
}
resource "aws_instance" "one" {
ami = "ami-0715c1897453cabd1"
instance_type = "t2.micro"
tags = {
Name = "web-server"
provider "aws" {
region = "ap-south-1"
alias = "south"
provider = "aws.south"
ami = "ami-0607784b46cbe5816"
instance_type = "t2.micro"
tags = {
Name = "web-server"
TERRAFORM WORKSPACE:
Default Workspace:
When you initialize a Terraform configuration without explicitly creating a workspace, you
are in the default workspace. The default workspace is often used for the main or
production environment.
Create a Workspace:
You can create additional workspaces using the terraform workspace new
List Workspaces:
To see a list of available workspaces, you can use: terraform workspace list
Select a Workspace:
Use the terraform workspace select command to switch between workspaces: terraform
workspace select dev
You can destroy resources for a specific workspace using: terraform workspace select
dev && terraform destroy
bucket = "my-bucket-name"
bucket = aws_s3_bucket.one.id
rule {
object_ownership = "BucketOwnerPreferred"
depends_on = [aws_s3_bucket_ownership_controls.two]
bucket = aws_s3_bucket.one.id
acl = "private"
bucket = aws_s3_bucket.one.id
versioning_configuration {
status = "Enabled"
cidr_block = "10.0.0.0/16"
instance_tenancy = "default"
enable_dns_hostnames = "true"
tags = {
Name = "my-vpc"
vpc_id = aws_vpc.abc.id
cidr_block = "10.0.0.0/16"
availability_zone = "ap-south-1a"
tags = {
Name = "subnet-1"
vpc_id = aws_vpc.abc.id
tags = {
Name = "my-igw"
vpc_id = aws_vpc.abc.id
route {
cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.igw.id
tags = {
Name = "my-route-table"
availability_zone = "us-west-2a"
size = 40
tags = {
Name = "Volume-1"
provider "aws" {
region = "us-east-1"
creation_token = "my-product"
tags = {
Name = "swiggy-efs"
TERRAFORM MODULES:
is a container where you can create multiple resources. Used to create .tf files in the directory
structure.
main.tf
module "my_instance_module" {
source = "./modules/instances"
ami = “ami-0a2457eba250ca23d"
instance_type = "t2.micro"
instance_name = " rahaminstance"
module "s3_module" {
source = "./modules/buckets"
bucket_name = "rahamshaik009988"
provider.tf
provider "aws" {
region = "us-east-1"
modules/instances/main.tf
ami = var.ami
instance_type = var.instance_type
tags = {
Name = var.instance_name
Modules/instances/variable.tf
variable "ami" {
type = string
variable "instance_type" {
type = string
variable "instance_name" {
type = string
Modules/buckets/main.tf
bucket = var.bucket_name
Modules/buckets/variable.tf
variable "bucket_name" {
type = string
TERRAFORM ADVANTAGES:
Readable code.
Dry run.
Importing of Resources is easy.
Creating of multiple resources.
Can create modules for repeatable code.
TERRAFORM DISADVANTAGES:
Currently under development. Each month, we release a beta version.
There is no error handling
There is no way to roll back. As a result, we must delete everything and re-run code.
A few things are prohibited from import.
Bugs