본문 바로가기

DevOps/Terraform

[Terraform] 테라폼 완전 기초

* 테라폼 이제 시작하는 사람이 작성하는 테라폼 완전 기초 개념 글입니다.

테라폼 구성요소

provider : AWS, Azure, Google Cloud Platform, Alibaba Cloud 와 같은 클라우드 제공업체(provider 종류)

resource: AWS에서 EC2 인스턴스, S3 버킷, RDS 데이터베이스 와 같은 인프라 리소스(resource는 provider document에서 확인) 

 

테라폼 명령어

init : 테라폼 프로젝트 초기화, 테라폼이 사용할 Provider를 다운로드하고, 테라폼 State 파일을 생성

plan : 테라폼 코드를 실행하여 인프라가 어떻게 생성될지 보여줌

apply : 테라폼 코드를 실행하여 인프라를 생성

destroy : 테라폼 코드를 실행하여 인프라를 삭제

 

테라폼 개발순서

0. 개발환경

IDE : VSCode

Extension : HashiCorp Terraform

1. 프로젝트 생성

mkdir terraform-aws-study

2. provider.tf 작성

terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "5.0.1"
    }
  }
}

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

3. terraform init

4. main.tf 작성

resource를 정의하는 파일

resource "aws_s3_bucket" "static_website_bucket" {
  bucket = "aws-s3-quest"
}

5. terraform plan

6. terraform apply

7. 최종 프로젝트 구조

├── .terraform
├── .terraform.lock.hcl
├── main.tf
├── providers.tf
├── terraform.tfstate
└── terraform.tfstate.backup