『비전공자도 이해할 수 있는 챗GPT』,
『비전공자도 이해할 수 있는 AI 지식』
10만 베스트셀러를 기록한
세상에서 가장 이해하기 쉬운 챗GPT 교양서

HOME » WIKI » Terraform

Terraform

인증

서비스 계정을 만들고 JSON 인증으로 공통으로 사용할 수 있지만 혼자서 쓸때는 credentials를 설정하지 않으면 gcloud 개인 인증으로 사용한다.

provider에 따라 aws와 gcp를 자동으로 인식한다.

tf import

실제 서버 구성을 가져와서 tf.state에 저장한다. 이 내용을 기준으로 main.tf를 만들 수 있다.

AWS

// main.tf
terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 3.27"
    }
  }

  required_version = ">= 0.14.9"
}

provider "aws" {
  profile = "user1"
  region  = "ap-northeast-2"
}

locals {
  serverconfig = [
    for server in var.configuration : [
      for i in range(1, server.no_of_instances + 1) : {
        instance_name = server.application_name == "skpark-login" || server.application_name == "skpark-install" ? "${server.application_name}" : "${server.application_name}${i}"
        instance_type = server.instance_type
        volume_size = server.volume_size
      }
    ]
  ]
}
// We need to Flatten it before using it
locals {
  instances = flatten(local.serverconfig)
}

resource "aws_instance" "infra1" {
  for_each = { for server in local.instances : server.instance_name => server }

  ami                         = "ami-0ded4aeabdfcffac4" // Ubuntu - Deep Learning AMI
  associate_public_ip_address = true
  instance_type               = each.value.instance_type
  key_name                    = "key1"
  security_groups = [
    "sg-XXXX"
  ]
  subnet_id = "subnet-XXXX"
  tags = {
    Name = each.value.instance_name
  }
  root_block_device {
    volume_size = each.value.volume_size
  }
}

// variables.tf
variable "configuration" {
  description = "The total configuration, List of Objects/Dictionary"
  default     = [{}]
}

// terraform.tfvars
configuration = [
  {
    "application_name" : "skpark-login",
    "instance_type" : "i3en.xlarge",
    "no_of_instances" : "1",
    "volume_size": 2000 // 2TB
  },
  {
    "application_name" : "skpark-install",
    "instance_type" : "t3.xlarge",
    "no_of_instances" : "1",
    "volume_size": 200 
  },
  {
    "application_name" : "skpark-gpu-node",
    "instance_type" : "g4dn.xlarge", # GPU(T4 1ea) nodes
    "no_of_instances" : "2"
    "volume_size": 200
  }
]

provider 정보에 따라 해당 profile 인증을 사용하고, $ terraform init으로 현재 인프라 구성 상태 저장, $ terraform apply -auto-approve로 인프라를 provisioning 한다.

현재 상태 조회는 $ terraform state list, 삭제는 $ terraform destroy

Last Modified: 2022/05/16 17:52:10
자바 알고리즘 인터뷰 파이썬 알고리즘 인터뷰

카카오 코딩 테스트 출제위원이 직접 집필한,
리트코드(LeetCode) 문제로 풀어보는,
구글, 마이크로소프트, 네이버, 카카오
코딩 테스트 완벽 가이드
『자바 알고리즘 인터뷰』,
『파이썬 알고리즘 인터뷰』

이 사이트의 운영 비용을 후원할 수 있으며, 후원자에게 혜택을 제공할 예정입니다.

© 2000 - Sang Park Except where otherwise noted, content on this site is licensed under a CC BY-NC 4.0.
This site design was brought from Distill. Logo and wiki background image was brought from Bear.