πŸ“ main.tf

.
└── main.tf
    β”œβ”€β”€ terraform {}   β†’ write providers in it
    β”œβ”€β”€ provider {}    β†’ auth / region
    β”œβ”€β”€ resource {}    β†’ resources to create (S3, VPC, EC2)
    β”œβ”€β”€ data {}        β†’ Read-only info from cloud e.g ami_id
    β”œβ”€β”€ variable {}    β†’ inputs
    └── output {}      β†’ outputs

🟧 Data Source

β†’ Read-only lookups β†’ Fetch info, don’t create

Example:

data "aws_ami" "latest" {
  most_recent = true
  owners      = ["amazon"]
}

🟦 Lambda Packaging (archive_file)

Structure

.
β”œβ”€β”€ main.tf
└── lambda/
    └── handler.py

Package Code β†’ ZIP

data "archive_file" "lambda_zip" {
  type        = "zip"
  source_dir  = "${path.module}/lambda"
  output_path = "${path.module}/lambda.zip"
}

Use ZIP in Lambda

resource "aws_lambda_function" "my_lambda" {
  function_name = "demo-lambda"
  role          = aws_iam_role.lambda_role.arn
  handler       = "handler.lambda_handler"
  runtime       = "python3.12"
 
  filename         = data.archive_file.lambda_zip.output_path
  source_code_hash = data.archive_file.lambda_zip.output_base64sha256
}

🟨 State File (terraform.tfstate)

β†’ Terraform’s memory
β†’ Stores IDs, attributes

Must be:

  • Protected
  • Not edited manually
  • Stored remotely (S3 + DynamoDB lock recommended)

πŸŸͺ Variables

β†’ Avoid hardcoding

variable "region" {
  default = "us-east-1"
}

🟫 Outputs

β†’ Print useful info

output "vpc_id" {
  value = aws_vpc.main.id
}