Terraform ile Infrastructure as Code: AWS Altyapı Otomasyonu

z

zafer ak

Yazar

03 November 2025 13 dakika okuma 693 görüntülenme
Terraform ile Infrastructure as Code: AWS Altyapı Otomasyonu
Altyapınızı kod olarak yönetin. Terraform modülleri, state yönetimi ve AWS provider kullanımı.

Infrastructure as Code (IaC) Nedir?

IaC, altyapı kaynaklarını kod ile tanımlama ve yönetme yaklaşımıdır. Terraform, HashiCorp'un açık kaynak IaC aracıdır ve multi-cloud desteği sunar.

Terraform Avantajları

  • Deklaratif Syntax: Ne istediğinizi tanımlayın, nasıl yapılacağını Terraform halletsin
  • Multi-Cloud: AWS, Azure, GCP, Kubernetes ve daha fazlası
  • State Management: Mevcut durumu takip eder
  • Plan & Apply: Değişiklikleri önizleme imkanı

Kurulum ve Başlangıç

# macOS
brew install terraform

# Windows (Chocolatey)
choco install terraform

# Versiyon kontrolü
terraform version

İlk Terraform Projesi

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

provider "aws" {
  region = "eu-west-1"
}

# VPC
resource "aws_vpc" "main" {
  cidr_block           = "10.0.0.0/16"
  enable_dns_hostnames = true
  enable_dns_support   = true

  tags = {
    Name        = "main-vpc"
    Environment = "production"
  }
}

# Subnet
resource "aws_subnet" "public" {
  vpc_id                  = aws_vpc.main.id
  cidr_block              = "10.0.1.0/24"
  availability_zone       = "eu-west-1a"
  map_public_ip_on_launch = true

  tags = {
    Name = "public-subnet"
  }
}

# EC2 Instance
resource "aws_instance" "web" {
  ami           = "ami-0c55b159cbfafe1f0"
  instance_type = "t3.micro"
  subnet_id     = aws_subnet.public.id

  tags = {
    Name = "web-server"
  }
}

Temel Komutlar

# Provider'ları indir
terraform init

# Değişiklikleri planla
terraform plan

# Değişiklikleri uygula
terraform apply

# Kaynakları yok et
terraform destroy

# State'i görüntüle
terraform state list
terraform state show aws_instance.web

Variables ve Outputs

# variables.tf
variable "environment" {
  description = "Deployment environment"
  type        = string
  default     = "development"
}

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

# outputs.tf
output "instance_ip" {
  description = "Public IP of the instance"
  value       = aws_instance.web.public_ip
}

output "vpc_id" {
  description = "VPC ID"
  value       = aws_vpc.main.id
}

Modüller

# modules/vpc/main.tf
resource "aws_vpc" "this" {
  cidr_block = var.cidr_block
  # ...
}

# Kullanım
module "vpc" {
  source     = "./modules/vpc"
  cidr_block = "10.0.0.0/16"
}

Sonuç

Terraform, altyapı yönetimini kod haline getirerek tekrarlanabilirlik, versiyon kontrolü ve otomasyon sağlar.

İlgili Yazılar