Ssoon
[T101-4주차] 04 테라폼 모듈로 재사용 가능한 인프라 생성하기 - (1) 모듈의 기본 본문
CloudNet@ 팀의 가시다님이 진행하는 Terraform 101 Study 04주차 정리입니다.
✅ terraform 코드를 module에 넣고 전체 코드의 여러 위치에서 해당 module 을 재사용이 가능합니다.
📌 폴더에 있는 모든 테라폼 구성 파일은 module 입니다.
💠 stage 에서 참조하기 위한 modules 디렉터리를 생성합니다.
- modules 최상위 폴더 생성
⛔ modules/services/webserver-cluster/main.tf
#! # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
#! VPC 설정
#! # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
resource "aws_vpc" "ssoon_vpc" {
cidr_block = "10.10.0.0/16"
enable_dns_support = true
enable_dns_hostnames = true
tags = {
Name = "ssoon_vpc"
}
}
#! # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
#! Subnet 설정
#! # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
resource "aws_subnet" "ssoon_subnet1" {
vpc_id = aws_vpc.ssoon_vpc.id
cidr_block = "10.10.1.0/24"
availability_zone = "ap-northeast-2a"
tags = {
Name = "ssoon_subnet1"
}
}
resource "aws_subnet" "ssoon_subnet2" {
vpc_id = aws_vpc.ssoon_vpc.id
cidr_block = "10.10.2.0/24"
availability_zone = "ap-northeast-2c"
tags = {
Name = "ssoon_subnet2"
}
}
#! # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
#! Internet Gateway 설정
#! # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
resource "aws_internet_gateway" "ssoon_igw" {
vpc_id = aws_vpc.ssoon_vpc.id
tags = {
Name = "ssoon_igw"
}
}
#! # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
#! Route Table 설정
#! # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
resource "aws_route_table" "ssoon_rt" {
vpc_id = aws_vpc.ssoon_vpc.id
tags = {
Name = "ssoon_rt"
}
}
resource "aws_route_table_association" "ssoon_rtassociation1" {
subnet_id = aws_subnet.ssoon_subnet1.id
route_table_id = aws_route_table.ssoon_rt.id
}
resource "aws_route_table_association" "ssoon_rtassociation2" {
subnet_id = aws_subnet.ssoon_subnet2.id
route_table_id = aws_route_table.ssoon_rt.id
}
resource "aws_route" "ssoon_defaultroute" {
route_table_id = aws_route_table.ssoon_rt.id
destination_cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.ssoon_igw.id
}
#! # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
#! Security Group 설정
#! # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
resource "aws_security_group" "ssoon_sg" {
vpc_id = aws_vpc.ssoon_vpc.id
name = "ssoon SG"
}
resource "aws_security_group_rule" "ssoon_sginbound" {
type = "ingress"
from_port = 8080
to_port = 8080
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
security_group_id = aws_security_group.ssoon_sg.id
}
resource "aws_security_group_rule" "ssoon_sgoutbound" {
type = "egress"
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
security_group_id = aws_security_group.ssoon_sg.id
}
#! # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
#! Auto Scailing Group 설정
#! # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
data "aws_ami" "amazonlinux2" {
most_recent = true
filter {
name = "owner-alias"
values = ["amazon"]
}
filter {
name = "name"
values = ["amzn2-ami-hvm-*-x86_64-ebs"]
}
owners = ["amazon"]
}
resource "aws_launch_configuration" "ssoon_lauchconfig" {
name_prefix = "ssoon_lauchconfig-"
image_id = data.aws_ami.amazonlinux2.id
instance_type = "t2.micro"
security_groups = [aws_security_group.ssoon_sg.id]
associate_public_ip_address = true
user_data = templatefile("${path.module}/user-data.sh", {
server_port = "8080"
})
lifecycle {
create_before_destroy = true
}
}
resource "aws_autoscaling_group" "ssoon_asg" {
name = "ssoon_asg"
launch_configuration = aws_launch_configuration.ssoon_lauchconfig.name
vpc_zone_identifier = [aws_subnet.ssoon_subnet1.id, aws_subnet.ssoon_subnet2.id]
min_size = 2
max_size = 10
health_check_type = "ELB"
target_group_arns = [aws_lb_target_group.ssoon_albtg.arn]
tag {
key = "Name"
value = "terraform-asg"
propagate_at_launch = true
}
}
#! # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
#! Application Load Balancer 설정
#! # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
resource "aws_lb" "ssoon_alb" {
name = "ssoon-alb"
load_balancer_type = "application"
subnets = [aws_subnet.ssoon_subnet1.id, aws_subnet.ssoon_subnet2.id]
security_groups = [aws_security_group.ssoon_sg.id]
tags = {
Name = "ssoon-alb"
}
}
resource "aws_lb_listener" "ssoon_http" {
load_balancer_arn = aws_lb.ssoon_alb.arn
port = 8080
protocol = "HTTP"
# By default, return a simple 404 page
default_action {
type = "fixed-response"
fixed_response {
content_type = "text/plain"
message_body = "404: page not found - T101 Study"
status_code = 404
}
}
}
resource "aws_lb_target_group" "ssoon_albtg" {
name = "t101-alb-tg"
port = 8080
protocol = "HTTP"
vpc_id = aws_vpc.ssoon_vpc.id
health_check {
path = "/"
protocol = "HTTP"
matcher = "200-299"
interval = 5
timeout = 3
healthy_threshold = 2
unhealthy_threshold = 2
}
}
resource "aws_lb_listener_rule" "ssoon_albrule" {
listener_arn = aws_lb_listener.ssoon_http.arn
priority = 100
condition {
path_pattern {
values = ["*"]
}
}
action {
type = "forward"
target_group_arn = aws_lb_target_group.ssoon_albtg.arn
}
}
💠 module 사용 구문
module "<NAME>" {
source = "<SOURCE>"
[CONFIG...]
}
NAME : module 를 참조하기 위한 식별자
SOURCE : module 의 경로
CONFIG : module 과 관련된 하나 이상의 인수
💠 module 사용 - stage/services/webserver-cluster/main.tf
provider "aws" {
region = "ap-northeast-2"
}
module "webserver_cluster" {
source = "../../../modules/services/webserver-cluster"
}
🔑 module 을 추가하거나 module 의 SOURCE 을 수정할 때마다 init 명령을 실행해야 합니다.
⛔ stage/services/webserver-cluster/main.tf
💊 아래와 같이 module 사용으로 간단하게 stage의 리소스를 생성할 수 있습니다.
#! # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
#! Provider 설정
#! # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
provider "aws" {
region = "ap-northeast-2"
}
#! # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
#! Backend 설정
#! # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
terraform {
backend "s3" {
bucket = "ssoon-t101study-tfstate-week4-files"
key = "stage/services/webserver-cluster/terraform.tfstate"
region = "ap-northeast-2"
dynamodb_table = "terraform-locks-week4-files"
}
}
#! # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
#! Module 설정
#! # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
module "webserver_cluster" {
source = "../../../modules/services/webserver-cluster"
}
🚩 Console 확인
✔ module 을 이용해 생성한 정보를 확인합니다.
'Terraform 101 Study' 카테고리의 다른 글
[T101-4주차] 04 테라폼 모듈로 재사용 가능한 인프라 생성하기 - (3) 모듈과 지역변수 (0) | 2022.11.13 |
---|---|
[T101-4주차] 04 테라폼 모듈로 재사용 가능한 인프라 생성하기 - (2) 모듈 입력 (0) | 2022.11.10 |
[T101-3주차] 03 테라폼 상태 관리하기 - (5) terraform_remote_state 데이터 소스 (0) | 2022.11.05 |
[T101-3주차] 03 테라폼 상태 관리하기 - (4) 상태파일격리 - 파일 레이아웃을 이용한 격리 (0) | 2022.11.01 |
[T101-3주차] 03 테라폼 상태 관리하기 - (4) 상태파일격리 - workspaces 을 통한 격리 (0) | 2022.10.31 |
Comments