HCL: Hashicorp Configuration Language
DSL: Domain Specific Language
Workflow
terraform 설치
https://www.terraform.io/downloads
sudo yum install -y yum-utils
sudo yum-config-manager --add-repo https://rpm.releases.hashicorp.com/RHEL/hashicorp.repo
sudo yum -y install terraform
terraform --version
aws-cli 설치
https://docs.aws.amazon.com/ko_kr/cli/latest/userguide/getting-started-install.html
cd ~
curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
sudo yum install -y unzip
unzip awscliv2.zip
sudo ./aws/install
aws --version
aws configure
aws sts get-caller-identity
sudo vi /etc/chrony.conf
(다른 서버 지우고 붙여넣기)
server 3.kr.pool.ntp.org iburst
server 2.asia.pool.ntp.org iburst
server 3.asia.pool.ntp.org iburst
sudo systemctl restart chronyd
timedatectl set-ntp true
timedatectl set-timezone Asia/Seoul
timedatectl
(만약 세팅이 안될 시 수동으로 하는 법)
sudo timedatectl set-ntp false
sudo timedatectl set-time "2022-04-20 10:00:00:00"
sudo timedatectl set-ntp true
Terraform Configure File
.tf
.tf.json
: Json 형식인코딩
Unicode
디렉토리
현재 작업 디렉토리 위치에 따라서 해당 디렉토리의 .tf
, .tf.json
모두 읽어서 실행
<BLOCK TYPE> <BLOCK LABEL> ... {
ARGUMENT
KEY = VALUE
}
{ } 블록
aws 리소스 참고링크 :
https://registry.terraform.io/providers/hashicorp/aws/latest/docs
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 3.27"
}
}
required_version = ">= 0.14.9"
}
provider "aws" {
profile = "default"
region = "us-west-2"
}
참고
테라폼 리소스 = 앤서블 모듈
테라폼 모듈 = 앤서블 역할
resource "RESOURCE_TYPE" "NAME" {
ARGUMENT = VALUE
}
terraform init
프로바이더 플러그인 설치
언제?
init 변경 시
ls -a
terraform.lock.hcl 을 삭제 후 재 init
.terraform 파일은 git 업로드 하면 안된다.
terraform fmt
언제?
terraform.tfstate , terraform.tfstate.backup
파일은 수정 및 git push하면 안된다.
terraform validate
terraform plan
terraform apply
terraform destroy
terraform.tfstate
: 현재상태 terraform.tfstate.backup
: 직전 상태terraform show
terraform state list
terraform state show aws_instance.app_server
terraform refresh
resource "aws_instance" "app_server" {
depends_on = [
aws_s3_bucket.app_bucket
]
}
variable "image_id" {
type = string
}
variable "availability_zone_names" {
type = list(string)
default = ["us-west-1a"]
}
[a, b, c]
{a = abc, b = xyz}
variable abc {
type = list(string)
# ["a", "b"]
type = list(number)
# [1, 2]
}
variable.tf에 default를 설정하지 않았을때
terraform plan -var "instance_name=xyz"
terraform.tfvars
instance_name = "xyz"
variable "aws_availability_zone"{
description = "aws AZs"
type = map(string)
default = {
ap-northeast-1 = "ap-northeast-1a"
ap-northeast-2 = "ap-northeast-2a"
}
}
tags = {
AZ = var.aws_availability_zone["ap-northeast-1"]
}