# 테라폼에서 변수 사용법을 알아보자.
# 값을 넣지 않고 변수로 처리해 효율성을 높이자.
1
sudo su -
cd terraform_root/environment/dev
2
# var.tf
# var
variable nhncloud_info {
type =map(string)
default = {
user_name = "topasvga@naver.com"
tenant_id = "0cc8a9fb6ad32d7cf43"
password = "test"
auth_url = "https://api-identity-infrastructure.nhncloudservice.com/v2.0"
}
}
variable passwd {
type = string
default = "test"
sensitive =true
}
variable region {
type = map(string)
default = {
kr1 = "KR1"
kr2 = "KR2"
}
}
# pro.tf
# Define required providers
terraform {
required_version = ">= 1.0.0"
required_providers {
nhncloud = {
source = "nhn-cloud/nhncloud"
version = "1.0.2"
}
}
}
provider "nhncloud" {
user_name = var.nhncloud_info["user_name"]
tenant_id = var.nhncloud_info["tenant_id"]
password = var.passwd
auth_url = var.nhncloud_info["auth_url"]
region = var.region["kr2"]
}
# vpc.tf
#vpc
resource "nhncloud_networking_vpc_v2" "terrform_vpc"{
name="tf-vpc-01"
cidrv4="10.0.0.0/16"
}
#subnet
resource "nhncloud_networking_vpcsubnet_v2" "terrform_v-vpcsubnet-01" {
name="public-subnet"
vpc_id= nhncloud_networking_vpc_v2.terrform_vpc.id
cidr="10.0.1.0/24"
}
3
ls
provider.tf
vpc.tf
variable.tf
# pro.tf
# Define required providers
terraform {
required_version = ">= 1.0.0"
required_providers {
nhncloud = {
source = "nhn-cloud/nhncloud"
version = "1.0.2"
}
}
}
provider "nhncloud" {
user_name = var.nhncloud_info["user_name"]
tenant_id = var.nhncloud_info["tenant_id"]
password = var.passwd
auth_url = var.nhncloud_info["auth_url"]
region = var.region["kr2"]
}
#vpc.tf
resource "nhncloud_networking_vpc_v2" "terrform_vpc"{
name="tf-vpc-01"
cidrv4="10.0.0.0/16"
}
#subnet
resource "nhncloud_networking_vpcsubnet_v2" "terrform_v-vpcsubnet-01" {
name="public-subnet"
vpc_id= nhncloud_networking_vpc_v2.terrform_vpc.id
cidr="10.0.1.0/24"
}
# var.tf
variable nhncloud_info {
type =map(string)
default = {
user_name = "topasvga@naver.com"
tenant_id = "0ccc9863d32d7cf43"
password = "test"
auth_url = "https://api-identity-infrastructure.nhncloudservice.com/v2.0"
}
}
variable passwd {
type = string
default = "test"
sensitive =true
}
variable region {
type = map(string)
default = {
kr1 = "KR1"
kr2 = "KR2"
}
}
# sudo su -
root@control-server:~/terraform_root/environment/dev# terraform validate
Success! The configuration is valid.