IaC
Infrastructure as Code
A method of defining and managing infrastructure as code. Uses Terraform, Pulumi, and CloudFormation.
Infrastructure as Code
A method of defining and managing infrastructure as code. Uses Terraform, Pulumi, and CloudFormation.
IaC (Infrastructure as Code) is an approach to defining and managing infrastructure as code. Instead of manually clicking through a console, you provision infrastructure such as servers, networks, and databases with declarative or imperative code. Version control, code review, and automated testing become possible, so infrastructure management becomes like software development.
# main.tf - AWS VPC์ EC2 ์ธ์คํด์ค
terraform {
required_version = ">= 1.0"
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 5.0"
}
}
backend "s3" {
bucket = "my-terraform-state"
key = "prod/terraform.tfstate"
region = "ap-northeast-2"
dynamodb_table = "terraform-locks"
encrypt = true
}
}
provider "aws" {
region = var.region
default_tags {
tags = {
Environment = var.environment
ManagedBy = "Terraform"
}
}
}
# VPC
module "vpc" {
source = "terraform-aws-modules/vpc/aws"
version = "5.1.0"
name = "${var.project}-vpc"
cidr = "10.0.0.0/16"
azs = ["ap-northeast-2a", "ap-northeast-2c"]
private_subnets = ["10.0.1.0/24", "10.0.2.0/24"]
public_subnets = ["10.0.101.0/24", "10.0.102.0/24"]
enable_nat_gateway = true
single_nat_gateway = var.environment != "production"
tags = {
Project = var.project
}
}
# Security Group
resource "aws_security_group" "web" {
name = "${var.project}-web-sg"
description = "Security group for web servers"
vpc_id = module.vpc.vpc_id
ingress {
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
from_port = 443
to_port = 443
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
tags = {
Name = "${var.project}-web-sg"
}
}
# EC2 Instance
resource "aws_instance" "web" {
count = var.instance_count
ami = data.aws_ami.amazon_linux.id
instance_type = var.instance_type
subnet_id = module.vpc.private_subnets[count.index % length(module.vpc.private_subnets)]
vpc_security_group_ids = [aws_security_group.web.id]
root_block_device {
volume_size = 20
volume_type = "gp3"
encrypted = true
}
tags = {
Name = "${var.project}-web-${count.index + 1}"
}
}
# Data source
data "aws_ami" "amazon_linux" {
most_recent = true
owners = ["amazon"]
filter {
name = "name"
values = ["al2023-ami-*-x86_64"]
}
}
# variables.tf
variable "region" {
description = "AWS region"
type = string
default = "ap-northeast-2"
}
variable "project" {
description = "Project name"
type = string
}
variable "environment" {
description = "Environment (development, staging, production)"
type = string
validation {
condition = contains(["development", "staging", "production"], var.environment)
error_message = "Environment must be development, staging, or production."
}
}
variable "instance_type" {
description = "EC2 instance type"
type = string
default = "t3.micro"
}
variable "instance_count" {
description = "Number of EC2 instances"
type = number
default = 2
}
# environments/prod.tfvars
region = "ap-northeast-2"
project = "myapp"
environment = "production"
instance_type = "t3.medium"
instance_count = 4
# ์ด๊ธฐํ - provider ๋ค์ด๋ก๋
terraform init
# ๊ณํ - ๋ณ๊ฒฝ์ฌํญ ๋ฏธ๋ฆฌ ํ์ธ
terraform plan -var-file=environments/prod.tfvars
# ์ ์ฉ - ์ค์ ์ธํ๋ผ ์์ฑ/์์
terraform apply -var-file=environments/prod.tfvars
# ์ํ ํ์ธ
terraform state list
terraform state show aws_instance.web[0]
# ํน์ ๋ฆฌ์์ค๋ง ์ ์ฉ
terraform apply -target=aws_instance.web
# ์ญ์
terraform destroy -var-file=environments/prod.tfvars
# ํฌ๋งทํ
๋ฐ ๊ฒ์ฆ
terraform fmt -recursive
terraform validate
Terraform state files store sensitive information such as passwords and keys in plaintext. Be sure to enable S3 encryption and access restrictions.
Always check the plan output before apply. Review it carefully, especially if it includes `destroy` or `replace`.
If multiple people run terraform apply at the same time, the state can become corrupted. Set up locking with DynamoDB or similar.
Manual changes in the console cause the code and the actual state to diverge. Periodically detect drift with `terraform plan`.