๐Ÿ“‚ IaC/Terraform

[Terraform] Provider, Resource, Data Source, Variable, Output, Locals, Module

dhyuck 2022. 8. 11. 00:26
๋ฐ˜์‘ํ˜•

Terraform

  • Terraform์€ ํ…Œ๋ผํผ ์ž์ฒด๋ฅผ ๊ตฌ์„ฑํ•˜๋Š” ๋‚ด์šฉ์œผ๋กœ backend, required_version ๋“ฑ์„ ์ง€์ •ํ• ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
terraform {
  required_version = ">= 1.0.0"
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 4.0"
    }
  }
}

Provider

  • Provider๋Š” ํ…Œ๋ผํผ์ด ๋™์ž‘ํ•˜๋Š” ํด๋ผ์šฐ๋“œ ์ œ๊ณต์ž(cloud providers) ๋˜๋Š” API ์ œ๊ณต์ž(API providers)์— ๋Œ€ํ•ด ๊ธฐ์ˆ ํ•ฉ๋‹ˆ๋‹ค.
  • provider๋“ค์ด ํ…Œ๋ผํผ์„ ์ง€์›ํ•˜์ง€ ์•Š๋Š”๋‹ค๋ฉด terraform์€ ์–ด๋–ค ์ข…๋ฅ˜์˜ ์ธํ”„๋ผ๋„ ๊ด€๋ฆฌ ํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.
provider "aws" {
  profile = "aws_hyuckang"
  region  = "ap-northeast-2"
}

Resource

  • resource๋Š” AWS EC2, VPC, Subnet์™€ ๊ฐ™์€ ์ธํ”„๋ผ ๊ฐ์ฒด(infrastructure object)๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.
resource "aws_instance" "hyuckang_EC2" {
  ami           = "ami-0ecb2a61303230c9d"
  instance_type = "t2.micro"

  tags = merge({
    Name = var.hyuckang_EC2_NameTag
    }, local.common_tags
  )
}

Data Source

  • Data Source๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋‹ค๋ฅธ ํ…Œ๋ผํผ ์ž‘์—…๊ณต๊ฐ„(workspace)์ด๋‚˜ API๋ฅผ ํ†ตํ•ด ๊ฐ€์ ธ์˜จ ์ •๋ณด๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ ํ…Œ๋ผํผ ๊ตฌ์„ฑ(configuration)์„ ์œ ์—ฐํ•˜๊ฒŒ ๋งŒ๋“ค์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
data "aws_vpc" "default_vpc" {
  default = true
}

data "aws_subnets" "default" {
  filter {
    name   = "vpc-id"
    values = [data.aws_vpc.default_vpc.id]
  }
}

Input Variable

  • Input Variable์€ ์ž…๋ ฅ ๋ณ€์ˆ˜๋กœ ํ…Œ๋ผํผ์˜ ๋ชจ๋“ˆ์„ ์žฌ์‚ฌ์šฉํ•˜๊ฑฐ๋‚˜ ํ…Œ๋ผํผ์„ ์ข€ ๋” ์œ ์—ฐํ•˜๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ค๋‹ˆ๋‹ค.
variable "hyuckang_EC2_NameTag" {
  default = "hyuckang_EC2"
  type    = string
}

Output Values

  • Output์€ ์ธํ”„๋ผ ๊ตฌ์กฐ์— ๋Œ€ํ•œ ์ •๋ณด(Infra structured data)๋ฅผ export ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

    ์ด๋ฅผ ํ†ตํ•ด ๋‹ค๋ฅธ ์ธํ”„๋ผ๋ฅผ ๊ตฌ์„ฑํ•˜๊ฑฐ๋‚˜ ๋‹ค๋ฅธ ํ…Œ๋ผํผ ์ž‘์—… ๊ณต๊ฐ„์—์„œ data source๋กœ ์‚ฌ์šฉ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

  • Output Value๋Š” ํ”„๋กœ๊ทธ๋ž˜๋ฐ์˜ ๋ฐ˜ํ™˜ ๊ฐ’(return value)๊ณผ ์œ ์‚ฌํ•œ ๊ฐœ๋…์ž…๋‹ˆ๋‹ค.

output "default_vpc_cidr" {
  value       = data.aws_vpc.default_vpc.cidr_block
  description = "default vpc cidr_block"
}

Local Values

  • Local Value๋Š” ๋™์ผํ•œ ๊ฐ’์ด๋‚˜ ํ‘œํ˜„์‹์„ ์—ฌ๋Ÿฌ๋ฒˆ ๋ฐ˜๋ณตํ•ด์•ผ ํ•  ๋•Œ ์‚ฌ์šฉ ํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ ์„ ์–ธ๋œ ๋ชจ๋“ˆ ๋‚ด์˜ ํ‘œํ˜„์‹์—์„œ๋งŒ ์•ก์„ธ์Šคํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  • Local Value๋Š” Input Variable๊ณผ ๋‹ฌ๋ฆฌ ์‚ฌ์šฉ์ž๊ฐ€ ์ง์ ‘ ์„ค์ • ํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.
locals {
  common_tags = {
    Using = "terraform_test"
    Owner = "hyuckang"
  }
}

Module

  • Module์„ ์‚ฌ์šฉํ•˜๋ฉด ๋ฆฌ์†Œ์Šค ๊ตฌ์„ฑ์„ ์บก์Šํ™”ํ•˜๊ณ  ๊ตฌ์„ฑ ํŒŒ์ผ์„ ์žฌ์‚ฌ์šฉ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  • Module์€ ๋‹จ์ผ ๋””๋ ‰ํ„ฐ๋ฆฌ์— ์žˆ๋Š” ํ•˜๋‚˜ ์ด์ƒ์˜ ๊ตฌ์„ฑํŒŒ์ผ๋“ค๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ์Šต๋‹ˆ๋‹ค.
  • ํ˜„์žฌ ์ž‘์—… ๋””๋ ‰ํ„ฐ๋ฆฌ์˜ ๋ชจ๋“ˆ์„ ๋ฃจํŠธ ๋ชจ๋“ˆ(root module)์ด๋ผ ํ•ฉ๋‹ˆ๋‹ค.
  • A ๋ชจ๋“ˆ์—์„œ B ๋ชจ๋“ˆ์„ ํ˜ธ์ถœํ•ด์„œ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ B๋Š” A์˜ ํ•˜์œ„๋ชจ๋“ˆ(child module)์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค.
module "servers" {
  source = "./app-cluster"
}

์ฐธ๊ณ 

Overview - Configuration Language | Terraform by HashiCorp

๋ฐ˜์‘ํ˜•