728x90
반응형
Terraform으로 NCP 서버 이미지,스펙 code 검색하기
- server_image_product_code => 서버이미지
- server_product_code => 서버스펙
[variables.tf]
variable "server_image_product_code" { # centos-7.3-64
default = "SPSW0LINUX000046"
}
variable "server_product_code" { # vCPU 2EA, Memory 4GB, Disk 50GB
default = "SPSVRSTAND000004" #SPSVRSTAND000056
}
=> 위와 같이 variables.tf (변수파일)에 변수선언하고 terraform apply를 하면 선언한 code를 읽어서 서버이미지, 스펙을 code대로 생성하게 된다.
1) 서버 이미지 code 확인 (ex. centos-7.3-64)
image //디렉토리
├── main.tf
├── variables.tf // 변수 선언 파일
├── outputs.tf //실행 끝에 표시되는 내용 정의 파일
└── version.tf //현재 설정을 맞게 versions.tf 파일을 생성된다.
[main.tf]
provider "ncloud" {
access_key = var.access_key
secret_key = var.secret_key
region = var.region
}
data "ncloud_server_images" "images" {
filter {
name = "product_name"
values = ["centos-7.3-64"]
}
output_file = "image.json"
}
output "list_image" {
value = {
for image in data.ncloud_server_images.images.server_images:
image.id => image.product_name
}
}
[variables.tf]
variable "access_key" { # export TF_VAR_access_key=...
default = ""
}
variable "secret_key" { # export TF_VAR_secret_key=...
default = ""
}
variable "region" {
default = "KR"
}
[versions.tf]
terraform {
required_version = ">= 0.13"
required_providers {
ncloud = {
source = "navercloudplatform/ncloud"
}
}
}
결과
terraform init
terraform plan
terraform apply
2) 서버 스펙 code 확인 (ex. vCPU 1EA, Memory 2GB, Disk 50GB)
[main.tf]
provider "ncloud" {
access_key = var.access_key
secret_key = var.secret_key
region = var.region
}
data "ncloud_server_products" "products" {
server_image_product_code = "SPSW0LINUX000032" #classic_CentOS 7.3
filter {
name = "cpu_count"
values = ["1"]
}
filter {
name = "memory_size"
values = ["2GB"]
}
filter {
name = "base_block_storage_size"
values = ["50GB"]
}
output_file = "product.json"
}
output "products" {
value = {
for product in data.ncloud_server_products.products.server_products:
product.id => product.product_name
}
}
[variables.tf]
variable "access_key" { # export TF_VAR_access_key=...
default = ""
}
variable "secret_key" { # export TF_VAR_secret_key=...
default = ""
}
variable "region" {
default = "KR"
}
[versions.tf]
terraform {
required_version = ">= 0.13"
required_providers {
ncloud = {
source = "navercloudplatform/ncloud"
}
}
}
결과
terraform init
terraform plan
terraform apply
*참고사이트[Terraform 사이트]
https://registry.terraform.io/
[Terraform - NAVER CLOUD]
https://github.com/NaverCloudPlatform/terraform-provider-ncloud
[Terraform Language 참고]
https://www.terraform.io/docs/language/index.html
728x90
728x90
'DevOps > Terraform' 카테고리의 다른 글
[NCP] Terraform을 활용한 NCP (Classic) - Server (2), LB(1) 구성 (0) | 2021.08.31 |
---|---|
[NCP] Terraform에서 apply 할 시, Status: 500 Internal Server Error 오류 (1) | 2021.08.30 |
Terraform의 provisioner(프로비저너)란? (1) | 2021.08.30 |
Terraform 이란 (프로비저닝 도구와 코드형 인프라)? (1) | 2021.08.30 |
[NCP] Terraform 0.13.0 설치 + git으로 provider 가져오기 (2) | 2021.08.26 |