Terraform + AWS Quick Notes
1️⃣ Basic Terraform Workflow
Step Command What Happens?
✅ Downloads provider plugins (e.g., AWS), sets up backend. No
1️⃣ Initialize Terraform terraform init
resources created.
2️⃣ View Execution Plan terraform plan ✅ Shows what changes Terraform will make. No changes yet.
3️⃣ Apply Changes
terraform apply ✅ Creates resources and generates terraform.tfstate.
(Create Infra)
4️⃣ Destroy Infra terraform
❌ Removes all resources Terraform created.
(Optional) destroy
2️⃣ Terraform Important Files
File Purpose
main.tf Defines infrastructure (EC2, VPC, S3, etc.).
variables.tf Stores variables (like AMI ID, instance type).
outputs.tf Defines outputs (e.g., EC2 instance IP after creation).
terraform.tfstate Tracks current infra state (auto-generated after apply).
.terraform/ Stores provider plugins and settings (created after init).
📌 Remember: terraform.tfstate is NOT created during terraform init. It appears only after terraform
apply.
3️⃣ AWS Basics for Terraform
🟢 AWS IAM (Identity and Access Management)
IAM is used to manage permissions and security in AWS.
You can create IAM Users & Roles.
To use Terraform, create an IAM User with Programmatic Access (Access Key & Secret Key).
🔑Setting Up AWS Credentials
1️⃣ Login to AWS as IAM User → Create Access Key. 2️⃣ Configure AWS CLI in VS Code:
aws configure
Enter Access Key & Secret Key.
Choose AWS Region (e.g., us-east-1).
💿 Finding a Valid AMI ID (For EC2 Instances)
Run this command in AWS CLI to get the latest Amazon Linux 2 AMI:
aws ec2 describe-images --owners amazon --filters "Name=name,Values=amzn2-ami-hvm-*-
x86_64-gp2" --query "Images | sort_by(@, &CreationDate) [-1].ImageId"
4️⃣ Terraform Providers & Resources
📌 What is a Provider?
A provider is what Terraform uses to interact with cloud services (AWS, Azure, GCP, etc.).
Example AWS Provider in main.tf:
provider "aws" {
region = "us-east-1"
}
📌 What is a Resource?
A resource is an actual infrastructure component (EC2, S3, VPC, etc.).
Example EC2 Instance:
resource "aws_instance" "my_instance" {
ami = "ami-XXXXXXXXXX" # Replace with valid AMI ID
instance_type = "t2.micro"
}
5️⃣ Terraform State File (terraform.tfstate)
Action What Happens?
First terraform apply Creates terraform.tfstate.
Second terraform plan Compares main.tf with terraform.tfstate.
Manually Deleting Terraform will think the resources don’t exist and recreate them. ⚠️
terraform.tfstate Avoid deleting manually!
6️⃣ Terraform Tips & Tricks
📝 Easy Mapping:
Init → Set up Terraform.
Plan → Check what will change.
Apply → Create infra.
Destroy → Delete infra.
⚠️Common Errors & Fixes:
InvalidAMIID.NotFound: AMI is not available in your region. Find a new AMI.
403 InvalidClientTokenId: Wrong AWS credentials. Run aws configure again.
✅ Best Practices:
Always use version control (Git) for .tf files.
Don’t edit terraform.tfstate manually!
Use terraform destroy before changing regions (to avoid orphaned resources).
provider "aws" {
region = "us-east-1"
resource "aws_instance" "example" {
ami = "ami-XXXXXXXXXXXXXXX" # Correct AMI ID for your region
instance_type = "t2.micro"