Terraform의 Module은 함께 사용되는 여러 리소스에 대한 컨테이너로서 .tf 및 .tf.json 파일 등으로 구성됩니다. Module을 통해 리소스 구성을 패키징하고 재사용할 수 있습니다. 또한, Terraform Registry를 통하여 다른 사람이 게시한 Module도 사용할 수 있습니다. 오늘은 Terraform Registry에 게시된 Module 중 VPC Module을 사용해봅시다.
1. tf 파일 작성
작업 디렉토리 하나를 생성하고, 설정 파일(config.tf) 하나를 생성합니다. 설정 파일에서는 provider 정보를 명시합니다.
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 5.0"
}
}
}
provider "aws" {
region = "ap-northeast-2"
}
module이 사용될 vpc.tf를 생성합니다. vpc.tf에 다음과 같이 작성합니다.
module "vpc" {
source = "terraform-aws-modules/vpc/aws"
name = "demo-vpc"
cidr = "10.0.0.0/16"
azs = ["ap-northeast-2a", "ap-northeast-2b", "ap-northeast-2c"]
public_subnets = ["10.0.0.0/24", "10.0.1.0/24", "10.0.2.0/24"]
public_subnet_names = ["demo-public-a", "demo-public-b", "demo-public-c"]
map_public_ip_on_launch = true
private_subnets = ["10.0.3.0/24", "10.0.4.0/24", "10.0.5.0/24"]
private_subnet_names = ["demo-private-a", "demo-private-b", "demo-private-c"]
database_subnets = ["10.0.6.0/24", "10.0.7.0/24", "10.0.8.0/24"]
database_subnet_names = ["demo-data-a", "demo-data-b", "demo-data-c"]
create_database_subnet_group = true
create_database_subnet_route_table = true
enable_nat_gateway = true
single_nat_gateway = false
one_nat_gateway_per_az = true
enable_dns_hostnames = true
enable_dns_support = true
tags = {
Environment = "dev"
}
}
다른 Input도 사용하고 싶다면 다음 링크를 참고하면 됩니다.
https://registry.terraform.io/modules/terraform-aws-modules/vpc/aws/latest
Terraform Registry
registry.terraform.io
2. 배포
리소스 배포를 위하여 Admin User의 Access Key를 등록합니다.
PS C:\Users\User\Documents\AWS\terraform-test> aws configure
AWS Access Key ID [****************M6OX]: (Access Key ID)
AWS Secret Access Key [****************t7Cj]: (Secret Access Key)
Default region name [ap-northeast-2]:
Default output format [json]:
terraform init을 통하여 Module을 가져오고, 플러그인을 설치하는 등의 준비 작업을 합니다.
PS C:\Users\User\Documents\AWS\terraform-test> terraform init
Initializing the backend...
Initializing modules...
Downloading registry.terraform.io/terraform-aws-modules/vpc/aws 5.1.2 for vpc...
- vpc in .terraform\modules\vpc
Initializing provider plugins...
- Finding hashicorp/aws versions matching ">= 5.0.0, ~> 5.0"...
- Installing hashicorp/aws v5.19.0...
- Installed hashicorp/aws v5.19.0 (signed by HashiCorp)
...
Terraform has been successfully initialized!
...
terraform plan을 통해 어떤 리소스가 생성될지 확인합니다. 에러를 발견할수도 있습니다.
PS C:\Users\User\Documents\AWS\terraform-test> terraform plan
Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
+ create
...
Plan: 41 to add, 0 to change, 0 to destroy.
...
terraform apply를 해서 정의한 리소스들을 생성합니다. 'yes'를 입력하여 배포를 시작합니다.
PS C:\Users\User\Documents\AWS\terraform-test> terraform apply
...
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
module.vpc.aws_vpc.this[0]: Creating...
module.vpc.aws_vpc.this[0]: Still creating... [10s elapsed]
module.vpc.aws_vpc.this[0]: Creation complete after 11s [id=vpc-091fa0c584fe5c3a3]
...
Apply complete! Resources: 41 added, 0 changed, 0 destroyed.
실제 AWS 환경에 리소스들이 잘 생성되었습니다.
3. 장단점
장점
- 코드 길이가 매우 짧아집니다.
- Input이 직관적이기 때문에 값들을 수정하기 편리합니다.
- subnet group 같이 VPC와 연결되는 리소스를 생성해주기도 합니다.
단점
- Input에 한계가 있어 상세한 설정이 어렵습니다.
- 예) Private Route Table, NAT Gateway 개별 Name 지정 불가능
오늘의 글은 여기까지입니다. 감사합니다!
'AWS' 카테고리의 다른 글
[AWS] Kinesis Agent로 Kinesis Data Stream에 로그 전송 (2) | 2023.10.10 |
---|---|
[AWS] CloudFront with Lambda Function URL (1) | 2023.10.09 |
[AWS] Global Aurora (Cross Region Read Replica, Global Database) (0) | 2023.10.02 |
[AWS] Amazon Aurora - RDS DB Engine (0) | 2023.10.01 |
[AWS] RDS Read Replicas & Multi AZ (2) | 2023.09.30 |