[terraform] terraform 이란?

Mr.Song·2022년 7월 13일
0

IaC

목록 보기
1/3

테라폼이란

Hashicorp(하시코프)에서 Go언어로 개발한 오픈소스 IaC 도구로, 인프라 서버를 구축/운영할 수 있게 도와주는 오픈소스 소프트웨어이다

테라폼 워크 플로우

테라폼은 API를 통해 클라우드는 온프레미스에 리소스를 생성하고 관리한다.

테라폼 핵심 워크플로우는 세 단계로 구성된다

  1. Write : 리소스 정의, 어떤 자원(서버,네트워크)에 어떻게 만들겠다를 정의한 '.tf' 확장자의 소스코드 작성
  2. Plan : 위에서 작성한 소스코드가 통해 생성 및 업데이트 될 인프라를 확인할 수 있는 실행계획 생성
  3. apply : Plan에서 확인한 리소스를 적용하는 단계

설치

wget -O- https://apt.releases.hashicorp.com/gpg | gpg --dearmor | sudo tee /usr/share/keyrings/hashicorp-archive-keyring.gp
echo "deb [signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] https://apt.releases.hashicorp.com $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/hashicorp.list
sudo apt update && sudo apt install terraform

# terraform -version
on linux_amd6

-- 자동완성 활성화
terraform -install-autocomplete

기본 사용 명령어

  1. 디렉터리(프로젝트) 초기화
# terraform init

Initializing the backend...

Initializing provider plugins...
- Reusing previous version of hashicorp/aws from the dependency lock file
- Using previously-installed hashicorp/aws v4.22.0

Terraform has been successfully initialized!

You may now begin working with Terraform. Try running "terraform plan" to see
any changes that are required for your infrastructure. All Terraform commands
should now work.

If you ever set or change modules or backend configuration for Terraform,
rerun this command to reinitialize your working directory. If you forget, other
commands will detect it and remind you to do so if necessary.
  1. main.tf 작성 (nginx 컨테이너 실행)
# vi main.tf
terraform {
  required_providers {
    docker = {
      source  = "kreuzwerker/docker"
      version = "~> 2.13.0"
    }
  }
}

provider "docker" {}

resource "docker_image" "nginx" {
  name         = "nginx:1.22.0"
  keep_locally = false
}

resource "docker_container" "nginx" {
  image = docker_image.nginx.latest
  name  = "tutorial"
  ports {
    internal = 80
    external = 8000
  }
}
  1. .tf 유효성 체크
# terrafrom validate
Success! The configuration is valid.
  1. plan
# terraform plan
docker_image.nginx: Refreshing state... [id=sha256:b3c5c59017fbb3fdcebe18b977da09d1e448218354d73ecd036283ddb81b3c35nginx:1.22.0]
docker_container.nginx: Refreshing state... [id=41a476861cf301835c65c615dff188d15466f025f4327bd41fb24955a8469da9]

Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
  + create

Terraform will perform the following actions:

  # docker_container.nginx will be created
  + resource "docker_container" "nginx" {


`
`
`
(중략)
`
`
`


Plan: 1 to add, 0 to change, 0 to destroy.
  1. apply
# terraform apply
Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
  + create

Terraform will perform the following actions:

  # docker_container.nginx will be created
  + resource "docker_container" "nginx" {

`
`
`
(중략)
`
`
`


Plan: 2 to add, 0 to change, 0 to destroy.

Do you want to perform these actions?
  Terraform will perform the actions described above.
  Only 'yes' will be accepted to approve.

  Enter a value: yes

docker_image.nginx: Creating...

Message from syslogd@user at Jul 13 15:00:11 ...
docker_image.nginx: Still creating... [44s elapsed]
docker_image.nginx: Creation complete after 44s [id=sha256:88e754207f36628c574c1a0cbcde92147eff93b32c497831c75a9d3d2cd0d0a3nginx:1.22.0]
docker_container.nginx: Creating...
docker_container.nginx: Creation complete after 5s [id=3f1978b2cea5ea36dc464a0c9476f5509c7708f0a1553b674c49a6b82d87370d]

Apply complete! Resources: 2 added, 0 changed, 0 destroyed.
  1. destroy
# terraform destroy
docker_image.nginx: Refreshing state... [id=sha256:b3c5c59017fbb3fdcebe18b977da09d1e448218354d73ecd036283ddb81b3c35nginx:1.22.0]
docker_container.nginx: Refreshing state... [id=41a476861cf301835c65c615dff188d15466f025f4327bd41fb24955a8469da9]

Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
  - destroy

Terraform will perform the following actions:

  # docker_image.nginx will be destroyed
  - resource "docker_image" "nginx" {
      - id           = "sha256:b3c5c59017fbb3fdcebe18b977da09d1e448218354d73ecd036283ddb81b3c35nginx:1.22.0" -> null
      - keep_locally = false -> null
      - latest       = "sha256:b3c5c59017fbb3fdcebe18b977da09d1e448218354d73ecd036283ddb81b3c35" -> null
      - name         = "nginx:1.22.0" -> null
      - repo_digest  = "nginx@sha256:d4fe835ca959ff06f3f0d6fff47dc9d480d21179cefea413f07e1be6b1de16c3" -> null
    }

Plan: 0 to add, 0 to change, 1 to destroy.

Do you really want to destroy all resources?
  Terraform will destroy all your managed infrastructure, as shown above.
  There is no undo. Only 'yes' will be accepted to confirm.

  Enter a value: yes

docker_image.nginx: Destroying... [id=sha256:b3c5c59017fbb3fdcebe18b977da09d1e448218354d73ecd036283ddb81b3c35nginx:1.22.0]
docker_image.nginx: Destruction complete after 0s

Destroy complete! Resources: 1 destroyed.
profile
정리를 못하면 기록이라도 하자!!

0개의 댓글