IT/Ansible & Terraform

[Terraform] Terraform 설치 및 개요

깅지수 2022. 4. 21. 11:57

https://www.terraform.io/

 

Terraform by HashiCorp

Terraform is an open-source infrastructure as code software tool that enables you to safely and predictably create, change, and improve infrastructure.

www.terraform.io

 

 

Terraform 

HCL : Hashicorp Configuration Language

DSL : Domain Specific Language

 

Workflow

  1. 코드 작성 write
  2. 계획 plan
  3. 적용 apply

provider : https://registry.terraform.io/browse/providers

 

Terraform Registry

 

registry.terraform.io

 

 

구성파일

Terraform Configure File

  • .tf : HCL
  • .tf.json : Json 형식

인코딩

Unicode

 

디렉토리

현재 작업 디렉토리 위치에 따라서 해당 디렉토리의 .tf, .tf.json 모두 읽어서 실행

 

<BLOCK TYPE> <BLOCK LABEL> ... {
	ARGUMENT
}

 

Terraform BLOCK

terraform {
  required_providers {
    mycloud = {						#provider의 이름
   	  source = "hashicorp/aws"		#provider의 종류
      version = "~> 3.27"			#provider의 버전
    }
  }
  
  required_version ">= 0.14.9"
}

 

Provider BLOCK (필수)

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

 

Resource BLOCK

resource "aws_instance" "app_server" {
  ami           = "ami-02de72c5dc79358c9"
  instance_type = "t2.micro"

  tags = {
    Name = "ExampleAppServerInstance"
  }
}

 

 

예제

terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 3.27"
    }
  }
  required_version = ">= 0.14.9"
}

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

resource "aws_instance" "app_server" {
  ami           = "ami-02de72c5dc79358c9"
  instance_type = "t2.micro"

  tags = {
    Name = "ExampleAppServerInstance"
  }
}]