- Install aws cli and terraform
- AWS Account and user created
- AWS user account credentails
- Configure aws cli
Basics of terraform. Knowledge about provider,resources,data and output We created a Ec2 instance in AWS using the configuration file.
provider "aws" {
region = "ap-south-1" #Mumbai
}
resource "aws_instance" "Example" {
ami = "ami-0e670eb768a5fc3d4" #Amazon Linux 2023 AMI 2023.3.20240219.0 x86_64 HVM kernel-6.1
instance_type = "t2.micro"
//subnet_id = "subnet-008698a4ad5513812" #Deafult Subnet for Mumbai -1b
key_name = "Project1"
tags = {
Name = "Example Server"
}
}
Points to remember
- key_name = "Project1" can only be defined and will not be created by terraform if not present.
We are trying to create VPC using ChatGPT on Day2 while using YT as refrence on Day3
Learning and using modules, using modules we can eleminate the need of rewriting code for similiar infrastructures.
Terraform State file and remote backend.
named as - terraform.tfstate Terraform state file act as heart of terraform, it uses the state file to determine what changes need to be applied during the subsequent runs
Terraform state file is created (if not already present), when we we run below command.
terraform init
when we create changes in our terraform configuration (terraform main.tf file), and run below
terraform apply
terraform reads the configuration file and compares the desired state with current state (It is recorded in the state file).
Drawbacks that comes with state file -
- Accidental deletion of the state file can cause issues as terraform will loose track of the current state of the infrastructure.
- Any sensetive info written in state file will be exposed if a vcs like github is being used to store state file.
- It is hard to keep track of state file if github is being used to keep it available between all the users in team. (As terraform state file is updated only post changes are made in the infra and any miss in pushing the updated state file to github can cause state file to loose track of changes made).
Overcoming these using Remote Backend Configuration, we can use cloud, local fs or terraform cloud for this. When an update in state file will happen, the updated file will get stored in the defined backend.
terraform {
backend "s3" {
bucket = "saif-state-file-bucket"
key = "mumbai-state-file/terraform.tfstate" #we can customize the path where we can our state file
region = "ap-south-1"
}
}
Points to remember
- Bucket should be present before using this.
- Creation of bucket manually is recommended while terraform can be used.
- Run terraform init after declaring this.