일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 | 29 | 30 |
- Amazon DynamoDB
- AWS
- Amazon GuardDuty
- staticmethod
- ReplicaSet
- Python
- mariaDB
- Amazon VPC
- Backend
- SSL 인증서
- DaemonSet
- Terraform
- Heartbleed
- Terraform state
- ConfigMap
- classmethod
- ansible
- deployment
- taint
- AWS Security Hub
- docker
- Amazon Route 53
- Cognito
- Kubernetis
- Industry Week 2023
- CI CD
- AWS EC2
- Amazon RDS
- k8s
- DevOps
- Today
- Total
Nubes et Stella
Terraform #04 (Variable/Output/Local) 본문
01. Terraform Variable
테라폼에는 Variable이라는 블록 타입이 존재하는데, 프로그래밍에서의 argutment(인자)라고 생각하면 편할 것같다.
테라폼에서 Variable을 사용하면 모듈의 자체 소스 코드를 변경하지 않고도 Terraform 모듈의 측면을 맞춤 설정 할 수 있다. 또한 이 기능을 사용하면 다양한 Terraform 구성에서 모듈을 공유하여 모듈을 구성하고 재사용할 수 있다고 한다.
테라폼 공식 : https://developer.hashicorp.com/terraform/language/values/variables
Input Variables - Configuration Language | Terraform | HashiCorp Developer
Input variables allow you to customize modules without altering their source code. Learn how to declare, define, and reference variables in configurations.
developer.hashicorp.com
- 아래의 예시 코드를 살펴보면 여러 AWS 리소스들을 모듈화 시켜놓은 코드이다. (모듈에 대해서는 나중에..)
- 아래의 코드는 VPC 생성 모듈, 퍼블릭/프라이빗 서브넷 생성 모듈이 정의되어 있는 코드이다.
- "myvpc" 라는 이름의 vpc를 생성하고, 서브넷 모듈의 메타변수에 ${module.vpc.name} 형태로 불러와서 서브넷 이름을 구성한다.
- 그 외 "CIDR 블록(cidr_block)"과 "가용 영역(availability_zone_id)"을 설정한다.
provider "aws" {
region = "ap-northeast-2"
}
module "vpc" {
source = "tedilabs/network/aws//modules/vpc"
version = "0.27.0"
name = "myvpc"
cidr_block = "10.0.0.0/16"
internet_gateway_enabled = true
dns_hostnames_enabled = true
dns_support_enabled = true
tags = {}
}
module "subnet_group__public" {
source = "tedilabs/network/aws//modules/subnet-group"
version = "0.27.0"
name = "${module.vpc.name}-public"
vpc_id = module.vpc.id
map_public_ip_on_launch = true
subnets = {
"${module.vpc.name}-public-001/az1" = {
cidr_block = "10.0.0.0/27"
availability_zone_id = "apne2-az1"
}
"${module.vpc.name}-public-002/az2" = {
cidr_block = "10.0.1.0/27"
availability_zone_id = "apne2-az2"
}
}
tags = {}
}
module "subnet_group__private" {
source = "tedilabs/network/aws//modules/subnet-group"
version = "0.27.0"
name = "${module.vpc.name}-private"
vpc_id = module.vpc.id
map_public_ip_on_launch = false
subnets = {
"${module.vpc.name}-private-001/az1" = {
cidr_block = "10.0.10.0/27"
availability_zone_id = "apne2-az1"
}
"${module.vpc.name}-private-002/az2" = {
cidr_block = "10.0.11.0/27"
availability_zone_id = "apne2-az2"
}
}
tags = {}
}
- #terraform apply 하여 AWS 웹 콘솔에서 위의 코드대로 생성된 것을 확인 할수 있다.
- VPC이름과 CIDR블록이 코드와 동일하게 생성된 것을 확인할 수 있고, 서브넷도 잘 생성된 것을 볼 수 있다.
02. Terraform Variable 사용법 #1
- variable 블록 선언 후, 해당 변수를 "vpc" 모듈의 "name" 변수에 적용한다. variable 블록은 변수 값이 필요하지 않으며 apply 과정에서 물어본다.
provider "aws" {
region = "ap-northeast-2"
}
variable "vpc_name" {}
module "vpc" {
source = "tedilabs/network/aws//modules/vpc"
version = "0.27.0"
name = var.vpc_name
cidr_block = "10.0.0.0/16"
internet_gateway_enabled = true
dns_hostnames_enabled = true
dns_support_enabled = true
tags = {}
}
03. Terraform Variable 사용법 #2
- variable을 사용할 수 있는 여러 방법이 있는 데 크게 아래 몇가지가 있다.
- 환경 변수 사용
- terraform.tfvars 파일 사용
- terraform.tfvars.json 파일 사용 (거의 사용 안 함)
- *.auto.tfvars 파일 사용
- -var / -var-file 옵션 사용
- 환경 변수 사용
- export TF_VAR_[변수명]=[변수 값]
- "terraform.tfvars" 파일 사용
- terraform.tfvars 파일을 생성 후, 파일 안에 변수명을 작성한다.
- "*.auto.tfvars" 파일 사용
- [파일명].auto.tfvars 파일 생성 후 , 파일 안에 변수명을 작성한다.
- "-var / -var-file" 옵션 사용
- tf plan/apply -var="[변수명]=[변수값]"
- tf plan/apply -var-file=[tfvars 파일]
04. Terraform Ouput
- Output 값은 apply/plan 했을 때 명령줄에서 인프라에 대한 정보를 제공하고 사용할 다른 Terraform 구성에 대한 정보를 출력해 준다. 이것 은 프로그래밍 언어의 return 과 유사하다.
테라폼 공식 : https://developer.hashicorp.com/terraform/language/values/outputs
Output Values - Configuration Language | Terraform | HashiCorp Developer
Output values are the return values of a Terraform module.
developer.hashicorp.com
- output [BLOCK NAME] 형식으로 선언하고 내용으로 value 메타인자에 변수 값을 넣어준다.
output "vpc_name" {
value = module.vpc.name
}
output "vpc_id" {
value = module.vpc.id
}
05. Terraform Local
- Local Values는 표현식에 이름을 할당하므로 표현식을 반복하는 대신 모듈 내에서 이름을 여러 번 사용할 수 있습니다. 이는 프로그래밍에서 지역 변수와 매칭된다.
테라폼 공식 : https://developer.hashicorp.com/terraform/language/values/locals
Local Values - Configuration Language | Terraform | HashiCorp Developer
Local values assign a name to an expression that can be used multiple times within a Terraform module.
developer.hashicorp.com
locals {
common_tags = {
Project = "Network"
Owner = "syhan"
}
}
output "vpc_name" {
value = module.vpc.name
}
output "vpc_id" {
value = module.vpc.id
}
module "vpc" {
source = "tedilabs/network/aws//modules/vpc"
version = "0.27.0"
name = var.vpc_name
cidr_block = "10.0.0.0/16"
internet_gateway_enabled = true
dns_hostnames_enabled = true
dns_support_enabled = true
tags = local.common_tags
}
- END -
'DevOps > Terraform' 카테고리의 다른 글
Terraform #06 (Conditional/For) (0) | 2023.10.19 |
---|---|
Terraform #05 (count/for_each) (1) | 2023.10.18 |
Terraform #03 (0) | 2023.09.27 |
Terraform #02 (0) | 2023.09.26 |
Terraform #01 (0) | 2023.09.25 |