■ Terraform 명령어
terraform init : 테라폼을 수행하기 위한 공급자의 플러그인들을 초기 설정하는 명령어
terraform fmt : terraform 표준 스타일로 파일 구성 포매팅
terraform validate : 파일의 구성이 유효한지 문법 검사
provider : 공급자
local : Type (리소스의 종류)
foo : 테라폼에서 사용할이름
구성요소 : filename, content
path.module : 현재 디렉토리 반환
${path.module}
${}는 프로그래밍 언어에서 문자열 보간String Interpolation이라고 부르는 문법입니다.
중괄호 사이에 있는 코드를 실행한 반환값을 문자열 중간에 삽입해줍니다.
하지만 0.12부터 단순히 다른 리소스의 속성값을 참조하는 경우 문자열 보간을 사용하지 않아도 됩니다.
■ git hub
cd 01-terraform
ls -al
.git , .gitignore 확인
vim .gitignore
.terraform
.tfstate
.tfstate.backup
이렇게 저장되있어야함
깃허브 홈페이지 접속해서 01-terraform 레포지토리 만듬
우분투와서
echo "# 01-terraform" >> README.md
ls 치면 README.md 추가되있어야함
git config --global user.email
git config --global user.name //확인해보고 없으면 추가
git config --global user.name "Ryu-dongwoo"
git config --global user.email "cjstkehddn11@naver.com"
git remote add origin git@github.com/Ryu-dongwoo/01-terraform.git
git remote -v // remote 추가됐는지 확인
git add .
git commit -m "first commit" //
git branch -M main
git push -u origin +main
curl http://13.125.105.235:8080 // 확인 하는법
tf destroy -auto-approve // 삭제하는법
==================================================
■ 타임서버 (우분투 시간설정)
sudo apt install ntp -y
sudo vim /etc/ntp.conf
server 1.kr.pool.ntp.org
server 1.asia.pool.ntp.org
server time.bora.net
맨아래에 추가입력
pool로시작하는거 모두 주석처리
저장하고나옴
sudo systemctl start ntp
ntpq -p
date치면 현재시각
==================================================
■ syntax
폴더안에 디렉토리 모두 삭제하고
vim main.tf
provider "local" {
}
-# 변수지정
variable "number_example" {
type = number
default = 100
}
변수값 출력
output "number_output" {
value = var.number_example
}
tf plan
tf apply
tf plan -var "number_example=200" // 변수에 값변경해보기
■ main.tf에 string 추가
provider "local" {
}
-# 변수지정
variable "number_example" {
type = number
default = 100
}
variable "string_example" {
type = string
default = "terraform-example-instance"
}
-# 변수값 출력
output "number_output" {
value = var.number_example
}
output "string_output" {
value = var.string_example
}
tf plan
tf apply
cp main.tf outputs.tf
cp main.tf variables.tf
ls
vim main.tf 에 들어가서 provider빼고 다 삭제
:e output.tf 들어가서 output빼고 다 삭제
:e variables.tf 들어가서 variable빼고 다 삭제
tf plan
tf apply
■ list 추가
vim variables.tf
-# 변수지정
variable "number_example" {
type = number
default = 100
}
variable "string_example" {
type = string
default = "terraform-example-instance"
}
variable "list_example" {
description = "An example of a list in Terraform"
type = list(any)
default = ["a", "b", "c"]
}
vim output.tf
-# 변수값 출력
output "number_output" {
value = var.number_example
}
output "string_output" {
value = var.string_example
}
output "list_output" {
value = var.list_example
}
tf apply
■ one-webserver에 포트
cd one-webserver
vim variabe.tf
variable "server_port" {
type = number
default = 8080
}
vim main.tf
user_data = <<-EOF
#!/bin/bash
echo "Hello, World" > index.html
nohup busybox httpd -f -p ${var.server_port} &
EOF
ingress {
from_port = var.server_port # 출발 포트
to_port = var.server_port # 도착 포트
tf apply
cd .. // 01-terraform으로와서
git add . // 변경이력있는것은 추적
git commit -m "20221220" // 로컬저장소에만 관리
git status
git push -u origin +main // 깃허브에 올림
■ count 반복문
cd 01-terraform 에서
mkdir count
vim main.tf
provider "aws" {
region = "ap-northeast-2"
}
resource "aws_iam_user" "example" {
name = "aws08-neo"
}
tf apply
■ count 매개변수를 이용한 반복
provider "aws" {
region = "ap-northeast-2"
}
resource "aws_iam_user" "example" {
count = 3
name = "aws08-neo.${count.index}"
}
tf apply
■ 3개 이름 번호붙혀서 나누기
provider "aws" {
region = "ap-northeast-2"
}
variable "user_name" {
description = "Create IAM users with these names"
type = list(string)
default = ["aws08-neo", "aws08-trinity", "aws08-morpheus"]
}
resource "aws_iam_user" "example" {
count = 3
name = "aws08-neo.${count.index}"
}
■ 3개 이름 붙혀서 나누기
provider "aws" {
region = "ap-northeast-2"
}
variable "user_name" {
description = "Create IAM users with these names"
type = list(string)
default = ["aws08-neo", "aws08-trinity", "aws08-morpheus"]
}
resource "aws_iam_user" "example" {
count = length(var.user_names)
name = var.user_names[count.index]
}
■ output추가
provider "aws" {
region = "ap-northeast-2"
}
variable "user_names" {
description = "Create IAM users with these names"
type = list(string)
default = ["aws08-neo", "aws08-trinity", "aws08-morpheus"]
}
resource "aws_iam_user" "example" {
count = length(var.user_names)
name = var.user_names[count.index]
}
output "neo_arn" {
value = aws_iam_user.example[*].arn
}