GCP

Terraform 시작하기

김모우 2023. 2. 12. 12:18
728x90
반응형

Terraform 이란?

IaC 코드의 종류 중 하나로 HashiCorp 에서 오픈 소스롤 개발 중인 툴입니다. Go 언어로 작성되어 있으며, Puppet, Chef, Ansible 과 같은 코드형 인프라를 제공하는 툴 중 하나이며, 주로 현재 AWS, GCP, Azure 등 클라우드 플랫폼의 Infra Structure를 코드 형태로 관리하기 위한 툴로 많이 사용 되고 있습니다.

 

Terrafrom 설치하기

https://www.terraform.io/downloads

 

Install | Terraform | HashiCorp Developer

Explore Terraform product documentation, tutorials, and examples.

developer.hashicorp.com

 

자신의 OS 버전에 맞는 메뉴얼을 선택하여 설치 진행

GCP Skillboost Lab을 통해 진행하여 제공된 인스턴스에 기 설치되어 있었음

- 버전 확인

terraform --version

 

Terrafrom 시작하기

- Terraform 환경 구성

mkdir /tfinfra
cd /tfinfra
echo provider "google" {} >> provider.tf
terraform init

 

Network 및 리소스 생성

cat << EOT >> mynetwork.tf
# Create the mynetwork network
resource "google_compute_network" "mynetwork" {
name = "mynetwork"
# RESOURCE properties go here
auto_create_subnetworks = "true"
}
# Add a firewall rule to allow HTTP, SSH, RDP and ICMP traffic on mynetwork
resource "google_compute_firewall" "mynetwork-allow-http-ssh-rdp-icmp" {
name = "mynetwork-allow-http-ssh-rdp-icmp"
# RESOURCE properties go here
network = google_compute_network.mynetwork.self_link
allow {
    protocol = "tcp"
    ports    = ["22", "80", "3389"]
    }
allow {
    protocol = "icmp"
    }
source_ranges = ["0.0.0.0/0"]
}
EOT

인스턴스 설정

mkdir instance
cd instnace
cat << EOT >> main.tf
resource "google_compute_instance" "vm_instance" {
  name         = "${var.instance_name}"
  zone         = "${var.instance_zone}"
  machine_type = "${var.instance_type}"
  boot_disk {
    initialize_params {
      image = "debian-cloud/debian-11"
      }
  }
  network_interface {
    network = "${var.instance_network}"
    access_config {
      # Allocate a one-to-one NAT IP to the instance
    }
  }
}
EOT

변수 설정

main.tf 에서 사용한 변수들의 실제 데이터를 입력해 줍니다.

cat << EOT >> variables.tf
variable "instance_name" {}
variable "instance_zone" {}
variable "instance_type" {
  default = "e2-micro"
  }
variable "instance_network" {}
EOT

Instance VPC 정보 추가

cd ~/tfinfra
cat << EOT >> mynetwork.tf
# Create the mynetwork network
resource "google_compute_network" "mynetwork" {
name = "mynetwork"
# RESOURCE properties go here
auto_create_subnetworks = "true"
}
# Add a firewall rule to allow HTTP, SSH, RDP and ICMP traffic on mynetwork
resource "google_compute_firewall" "mynetwork-allow-http-ssh-rdp-icmp" {
name = "mynetwork-allow-http-ssh-rdp-icmp"
# RESOURCE properties go here
network = google_compute_network.mynetwork.self_link
allow {
    protocol = "tcp"
    ports    = ["22", "80", "3389"]
    }
allow {
    protocol = "icmp"
    }
source_ranges = ["0.0.0.0/0"]
}
# Create the mynet-us-vm instance
module "mynet-us-vm" {
  source           = "./instance"
  instance_name    = "mynet-us-vm"
  instance_zone    = "us-east5-a"
  instance_network = google_compute_network.mynetwork.self_link
}
# Create the mynet-eu-vm" instance
module "mynet-eu-vm" {
  source           = "./instance"
  instance_name    = "mynet-eu-vm"
  instance_zone    = "europe-west1-d"
  instance_network = google_compute_network.mynetwork.self_link
}
EOT

실행하기

terraform fmt
terraform plan
terraform apply

- Instance, VPC, Firewall 정상 생성 확인

728x90
반응형

'GCP' 카테고리의 다른 글

Terraform Variables  (0) 2023.03.14
GCP Cloud Run  (0) 2023.02.06
Google Cloud Platform for AWS Professionals Labs  (0) 2021.04.15