Terraform 기초 과정 (2)
Terraform 기초 과정 (2)
03
리소스가 생성될때 다른 사용자가 사용하지 못하도록 막아주는 역할
테라폼 state/lock 관리 -> S3(S3 bucket) => terraform.tfstate -> Dynamo DB => .terraform.lock.hcl
https://developer.hashicorp.com/terraform/language/state/backends
1
2
3
4
5
6
7
8
9
# 다중 사용자들이 상태 정보를 공유하는 형식
terraform {
backend "s3" {
bucket = "mybucket-bsc-7979"
key = "global/s3/terraform.tfstate"
region = "us-east-2"
dynamodb_table = "myDyDB_remote_state"
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
data "terraform_remote_state" "my_remote_state" {
backend = "s3"
config = {
bucket = "mybucket-bsc-7979"
key = "global/s3/terraform.tfstate"
region = "us-east-2"
}
}
output "port" {
....
}
resource "aws_launch_configuration" "myLC" {
name_prefix = "myLC-"
image_id = data.aws_ami.ubuntu.id
instance_type = "t2.micro"
security_groups = [aws_security_group.mySG.id]
# user_data
user_data = templatefile("userdata.sh", {
db_address = data.terraform_remote_state.my_remote_state.outputs.address
db_port = data.terraform_remote_state.my_remote_state.outputs.port
})
....
}
테라폼 모듈(로컬 or 원격)은 단일 디렉토리에 있는 테라폼 구성 파일의 모음
- 로컬 : ./module/*
- 원격 : registry.terraform.io
meta argument = 아무데서나 어디서든 쓸 수 있는 공통 argument
모듈의 출력결과는 많이 적어주는 게 좋음
모듈에 들어있는 내용들을 아웃풋으로 끌어다가 쓸 수 있음.
module.servers.instance_ids
terraform state list terraform plan|apply -replace=module.example…. or terraform plan|apply -replace module.example….
고정으로 쓰는 거는 변수로 써줄 필요가 없음.
태그는 대소문자 구분함.
모듈은 되도록이면 유연해야함
README.md에 정의된 변수나 함수를 모두 문서화해야함.
모듈화
This post is licensed under CC BY 4.0 by the author.