[NCP] Terraform을 활용한 NCP 서버 이미지,서버 스펙 code 검색하기
DevOps/Terraform

[NCP] Terraform을 활용한 NCP 서버 이미지,서버 스펙 code 검색하기

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

terraform plan 만하고 image.json 파일 확인 해도 됨.
image.json 파일이 생성 된 것 을 확인

 

 

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

product.json 파일이 생성 된 것 을 확인

 

 

*참고사이트[Terraform 사이트]

https://registry.terraform.io/

 

https://registry.terraform.io/

 

registry.terraform.io

[Terraform - NAVER CLOUD]

https://github.com/NaverCloudPlatform/terraform-provider-ncloud

 

GitHub - NaverCloudPlatform/terraform-provider-ncloud: Terraform NaverCloud provider

Terraform NaverCloud provider. Contribute to NaverCloudPlatform/terraform-provider-ncloud development by creating an account on GitHub.

github.com

[Terraform Language 참고]

https://www.terraform.io/docs/language/index.html

 

Overview - Configuration Language - Terraform by HashiCorp

An introduction to the Terraform Configuration Language that is used to declare resources in infrastructure as code.

www.terraform.io

 

728x90
728x90