관리 메뉴

근묵자흑

Terraform provider 본문

IaC/terraform

Terraform provider

Luuuuu 2024. 10. 20. 22:24

프로바이더 란?

  • 공급자, 원격 환경을 제공하는 대상
  • 프로바이터다 제공하는 API를 호출하여 상호작용
  • ex) aws, azure

Docs

 

https://registry.terraform.io/providers/hashicorp/aws/latest/docs

Provider 설정 (aws example)

Terraform 13.0 이상의 버전부터 AWS Provider를 사용하기 위해서, 위의 code와 같이 terraform{} block에 source와 AWS provider의 버전을 선택하면 된다.


    # Terraform 0.13 and later:

    terraform {
      required_providers {
        aws = {
          source  = "hashicorp/aws"
          version = "~> 5.0"
        }
      }
    }

    # Configure the AWS Provider
    provider "aws" {
      region = "us-east-1"
    }

    # Create a VPC
    resource "aws_vpc" "example" {
      cidr_block = "10.0.0.0/16"
    }

    # Configure the AWS Provider
    provider "aws" {
      region     = "ap-northeast-2"
      # Hard-coding credentials is not recommended
      access_key = "XXXXXXXXXXXXXX"
      secret_key = "XXXXXXXXXXXXXX"
    }
    
  .

프로바이더 요구사항 정의

    terraform {
      required_providers {  
        aws = {  # 프로바이더 로컬 이름 
          source  = "hashicorp/aws"  # [호스트주소]/네임스페이스/유형
          version = "~> 5.0"         # 버전
        }
      }
    }

provider에서 사용할 수 있는 arguments에 대해서는 아래 링크 참조

https://registry.terraform.io/providers/hashicorp/aws/latest/docs#argument-reference

다중 프로바이더일 경우

동일한 프로바이더를 사용하지만 다른 조건을 갖는 경우, 리소스마다 별도 선언된 프로바이더를 지정할 수 있다.

    provider "aws" {
        region = "us-west-1"
    }

    provider "aws" {
        alias = "seoul"  # alias 명시
        region = "ap-northeast-2"
    }

    resource "aws_instance" "app_server" {
      provider = aws.seoul  # 프로바이더 지정
      ami = "{ami}"
      instance_type = "t3.micro"

Credentials 설정 (환경변수)

아래와 같은 선언은 추천하지 않는다.

screat 정보가 포함돼, github에 올라갈 경우 보안에 위험성이 있다.

    # Configure the AWS Provider
    provider "aws" {
      region     = "ap-northeast-2"
      # Hard-coding credentials is not recommended
      access_key = "XXXXXXXXXXXXXX"
      secret_key = "XXXXXXXXXXXXXX"
    }

Terminal에서 위와 같이 credentials 정보를 환경변수로 입력해 준 후,

    $ export AWS_ACCESS_KEY_ID="XXXXXXXX"
    $ export AWS_SECRET_ACCESS_KEY="XXXXXXXX"
    $ export AWS_DEFAULT_REGION="us-west-2"
    provider "aws" {}

이렇게 provider를 생성해준 뒤 사용이 가능하다.

Credentials를 source code에서 제거하고 환경변수로 선언하여 사용하는 방법이다.

Credentials 설정 (AWS credential 파일)

https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-files.html

여기를 참조해서 AWS credential 파일을 만든 후, 해당 파일을 terraform에 제공하여 credentials 정보를 전달해 줄 수 있다.

    # Configure the AWS Provider
    # in windows
    provider "aws" {
      region                   = "ap-southeast-1"
      shared_credentials_files  = ["C:\\Users\\{UserName}\\.aws\\credentials"]
      profile                  = "default"
    }

Terraform Init

    PS D:\terraform> terraform init
    Initializing the backend...
    Initializing provider plugins...
    - Finding hashicorp/aws versions matching "~> 5.0"...
    - Installing hashicorp/aws v5.69.0...
    - Installed hashicorp/aws v5.69.0 (signed by HashiCorp)
    Terraform has made some changes to the provider dependency selections recorded
    in the .terraform.lock.hcl file. Review those changes and commit them to your
    version control system if they represent changes you intended to make.

    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.t

 

Terraform apply | Terraform destroy

'IaC > terraform' 카테고리의 다른 글

Terraform Module  (8) 2025.01.05
Terraform State  (3) 2024.12.29
테라폼 로드밸런서 배포  (5) 2024.12.22
왜 테라폼 인가?  (4) 2024.12.15
workflow  (7) 2024.11.05