# macOS
brew install awscli
# Linux
sudo apt-get install -y awscli
aws configure
입력 예시:
AWS Access Key ID [None]: AKIA...
AWS Secret Access Key [None]: wJalrXUtnFEMI/K7MDENG/bPxRfiCY
Default region name [None]: ap-northeast-2
Default output format [None]: json
⚙️ 설정 파일은
~/.aws/credentials에 저장됩니다.
# macOS
brew tap hashicorp/tap
brew install hashicorp/tap/terraform
# Linux
sudo apt-get install -y terraform
terraform -v
작업용 디렉토리 생성:
mkdir terraform-aws-demo
cd terraform-aws-demo
main.tf 파일 작성:
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 5.0"
}
}
required_version = ">= 1.6.0"
}
provider "aws" {
region = "ap-northeast-2"
}
Terraform 초기화:
terraform init
예를 들어 이미 존재하는 S3 버킷을 불러오는 경우:
main.tf에 리소스 정의:
resource "aws_s3_bucket" "example" {
bucket = "my-existing-bucket"
}
Import 실행:
terraform import aws_s3_bucket.example my-existing-bucket
현재 상태 확인:
terraform state list
terraform state show aws_s3_bucket.example
💡 import는 state 파일만 갱신합니다.
코드(HCL)는 직접 맞춰줘야 하며,terraform plan으로 diff를 확인하면서 수정합니다.
변경사항 시뮬레이션:
terraform plan
실제 반영:
terraform apply
모든 리소스 삭제:
terraform destroy
로컬 terraform.tfstate 대신 S3를 백엔드로 사용하면 협업에 유리합니다.
terraform {
backend "s3" {
bucket = "my-tfstate-bucket"
key = "terraform/state.tfstate"
region = "ap-northeast-2"
}
}
variables.tf
variable "bucket_name" {
type = string
}
outputs.tf
output "bucket_name" {
value = aws_s3_bucket.example.bucket
}
명령:
terraform apply -var "bucket_name=my-demo-bucket"