Terraform ile Infrastructure as Code: AWS ve Cloud Yönetimi

z

zafer ak

Yazar

29 December 2025 3 dakika okuma 331 görüntülenme
Terraform ile Infrastructure as Code: AWS ve Cloud Yönetimi
Terraform ile bulut altyapısı yönetimi. AWS, Azure, GCP için kaynak oluşturma, state yönetimi, modüller ve best practices.

Terraform, HashiCorp tarafından geliştirilen, altyapıyı kod olarak tanımlamanızı sağlayan bir IaC (Infrastructure as Code) aracıdır.

Cloud Infrastructure

Terraform Avantajları

  • Multi-cloud: AWS, Azure, GCP ve daha fazlası
  • Declarative: İstenen durumu tanımlarsınız
  • State management: Altyapı durumu takibi
  • Plan özelliği: Değişiklikleri önizleme
  • Modülerlik: Yeniden kullanılabilir bileşenler

Kurulum

# macOS
brew install terraform

# Ubuntu
wget -O- https://apt.releases.hashicorp.com/gpg | sudo gpg --dearmor -o /usr/share/keyrings/hashicorp-archive-keyring.gpg
echo "deb [signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] https://apt.releases.hashicorp.com $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/hashicorp.list
sudo apt update && sudo apt install terraform

# Versiyon kontrolü
terraform version

Temel Yapı

# main.tf
terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 5.0"
    }
  }
  required_version = ">= 1.0"
}

provider "aws" {
  region = var.aws_region
}

# EC2 Instance
resource "aws_instance" "web" {
  ami           = var.ami_id
  instance_type = var.instance_type

  tags = {
    Name        = "WebServer"
    Environment = var.environment
  }
}
AWS Cloud

Variables

# variables.tf
variable "aws_region" {
  description = "AWS region"
  type        = string
  default     = "eu-west-1"
}

variable "instance_type" {
  description = "EC2 instance type"
  type        = string
  default     = "t3.micro"
}

variable "ami_id" {
  description = "AMI ID"
  type        = string
}

variable "environment" {
  description = "Environment name"
  type        = string
  validation {
    condition     = contains(["dev", "staging", "prod"], var.environment)
    error_message = "Environment must be dev, staging, or prod."
  }
}
# terraform.tfvars
aws_region    = "eu-west-1"
instance_type = "t3.small"
ami_id        = "ami-0123456789abcdef0"
environment   = "prod"

Outputs

# outputs.tf
output "instance_id" {
  description = "EC2 Instance ID"
  value       = aws_instance.web.id
}

output "public_ip" {
  description = "Public IP address"
  value       = aws_instance.web.public_ip
}

output "public_dns" {
  description = "Public DNS name"
  value       = aws_instance.web.public_dns
}

Terraform Komutları

# Initialize
terraform init

# Format check
terraform fmt

# Validate
terraform validate

# Plan (preview)
terraform plan

# Apply
terraform apply

# Apply with auto-approve
terraform apply -auto-approve

# Destroy
terraform destroy

# State list
terraform state list

# Show specific resource
terraform state show aws_instance.web
Infrastructure Management

Modüller

# modules/vpc/main.tf
resource "aws_vpc" "main" {
  cidr_block           = var.vpc_cidr
  enable_dns_hostnames = true

  tags = {
    Name = var.vpc_name
  }
}

resource "aws_subnet" "public" {
  count             = length(var.public_subnets)
  vpc_id            = aws_vpc.main.id
  cidr_block        = var.public_subnets[count.index]
  availability_zone = var.azs[count.index]

  tags = {
    Name = "${var.vpc_name}-public-${count.index + 1}"
  }
}
# main.tf - Modül kullanımı
module "vpc" {
  source = "./modules/vpc"

  vpc_name       = "production-vpc"
  vpc_cidr       = "10.0.0.0/16"
  azs            = ["eu-west-1a", "eu-west-1b"]
  public_subnets = ["10.0.1.0/24", "10.0.2.0/24"]
}

# Registry modülü
module "s3_bucket" {
  source  = "terraform-aws-modules/s3-bucket/aws"
  version = "3.15.1"

  bucket = "my-unique-bucket-name"
  acl    = "private"
}

Remote State

# backend.tf
terraform {
  backend "s3" {
    bucket         = "my-terraform-state"
    key            = "prod/terraform.tfstate"
    region         = "eu-west-1"
    encrypt        = true
    dynamodb_table = "terraform-locks"
  }
}

Workspaces

# Workspace oluştur
terraform workspace new staging
terraform workspace new production

# Workspace listele
terraform workspace list

# Workspace değiştir
terraform workspace select production

# Workspace'e göre değişken
locals {
  environment = terraform.workspace
  instance_type = {
    dev     = "t3.micro"
    staging = "t3.small"
    prod    = "t3.large"
  }
}

resource "aws_instance" "web" {
  instance_type = local.instance_type[local.environment]
}

Sonuç

Terraform, bulut altyapısını kod olarak yönetmenin standart aracı haline gelmiştir. Modüler yapısı ve multi-cloud desteği ile büyük ölçekli altyapıları güvenle yönetebilirsiniz.

İlgili Yazılar