본문 바로가기

카테고리 없음

terraform module

terraform module이란?

우리가 앞에서 배운 module 은 분명 .tf 확장자를 가진 디렉터를 module 이라고 부르는 것으로 알고 있을 것이다

 

하지만 이번엔 block type 으로 module을 사용하는 경우이다

module "vpc"{
  source  = "terraform-aws-modules/vpc/aws"
  version = "2.278.0"
}

이러한 문법으로 구성이 되는데 

module 옆에 "VPC" 는 module 의 이름을 말해준다

source 인자는 사용할 module 을 지정하고 

version 인자는 module 버전을 지정한다

 

 

(terraform 코드 상에서 하나의 블록으로 다른 사람이 작성한 terraform 코드 그룹을 가져와 사용할 수 있다)

 

https://www.terraform.io/language/modules/sources

 

Module Sources | Terraform by HashiCorp

The source argument tells Terraform where to find child modules's configurations in locations like GitHub, the Terraform Registry, Bitbucket, Git, Mercurial, S3, and GCS.

www.terraform.io

여길 참고하면 terraform 모듈을 어떻게 가지고 올 수 있는지에 대해 알려준다

 

이 3 부분을 봐주는 것이 좋다

 

간단한 예시를 실행해보겠다

 

이코드는 Terraform Registry 에서 가지고온 코드이다 

 

리전은 유렵(아일렌드)이다

module "vpc" {
  source = "terraform-aws-modules/vpc/aws"

  name = "my-vpc"
  cidr = "10.0.0.0/16"

  azs             = ["eu-west-1a", "eu-west-1b", "eu-west-1c"]
  private_subnets = ["10.0.1.0/24", "10.0.2.0/24", "10.0.3.0/24"]
  public_subnets  = ["10.0.101.0/24", "10.0.102.0/24", "10.0.103.0/24"]

  enable_nat_gateway = true
  enable_vpn_gateway = true

  tags = {
    Terraform = "true"
    Environment = "dev"
  }
}

 

코드를 가지고 왔다면 

terraform init

을 통해 terraform 의 module 을 다운 받는다음 

 

plan 이후 apply 를 해줘야한다

terraform apply

 

 

실행과 동시에 리소스가 생성되는 것을 알 수 있다