반응형
방화벽 룰 또한 모듈을 통해 관리하도록 구성하였다.
1. 방화벽 룰 생성
임시로 생성한 방화벽이라 source_ranges를 0.0.0.0/0으로 설정했지만 실제 환경에서는 필요한 대역만 오픈해야한다.
network 부분을 보면 (1) 네트워크 및 서브넷 설정 에서 구성한 모듈을 활용해서 네트워크로 지정하고 있다.
# 방화벽 룰 생성
module "sg-an3-hrc-ssh" {
source = "./modules/firewall"
firewall_name = "sg-an3-hrc-ssh"
protocol = "tcp"
ports = ["22"]
source_ranges = ["0.0.0.0/0"]
target_tags = "sgtag-an3-hrc-ssh"
description = "ssh connect"
network = module.vpc_network.google_compute_network_id
}
module "sg-an3-hrc-jenkins" {
source = "./modules/firewall"
firewall_name = "sg-an3-hrc-jenkins"
protocol = "tcp"
ports = ["80", "8080"]
source_ranges = ["59.15.35.75/32","165.243.5.20/32"]
target_tags = "sgtag-an3-hrc-jenkins"
description = "jenkins connect"
network = module.vpc_network.google_compute_network_id
}
1) main.tf
방화벽 룰이 생성되는 부분이다.
resource "google_compute_firewall" "firewall_rule" {
name = "${var.firewall_name}"
network = "${var.network}"
description = "${var.description}"
allow {
protocol = "${var.protocol}"
ports = "${var.ports}"
}
source_ranges = "${var.source_ranges}"
target_tags = ["${var.target_tags}"]
}
2.output.tf
vm, gke node 등을 테라폼을 통해 관리할 때 방화벽 태그를 사용하기 위한 output을 설정했다.
output "fw_target_tags" {
description = "ID of the created google_compute_network id"
value = google_compute_firewall.firewall_rule.target_tags
}
3.variables.tf
변수를 전달받기 위해 변수를 지정하였다.
variable "firewall_name" {
description = "firewall_name"
type = string
}
variable "protocol" {
description = "protocol"
type = string
}
variable "ports" {
description = "ports"
type = list(string)
}
variable "network" {
description = "service_name"
type = string
}
variable "source_ranges" {
description = "source_ranges"
type = list(string)
}
variable "target_tags" {
description = "target_tags"
type = string
}
variable "description" {
description = "description"
type = string
}
2. 생성된 방화벽 GCP 콘솔에서 확인
1) sg-an3-hrc-ssh
2) sg-an3-hrc-jenkins
반응형
'클라우드 > GCP 자원 테라폼으로 관리해보기' 카테고리의 다른 글
(5) GCP 공유 VPC 설정 모듈화 (0) | 2024.06.09 |
---|---|
(4) GCP Cloud function 모듈로 관리하기 (0) | 2024.06.09 |
(3-2) GCP IAM Policy 관리 - serviceAccount, group, user를 role에 연결해주는 모듈 (0) | 2024.06.09 |
(3-1) GCP IAM Policy 관리 - custom role을 구성하는 모듈 (0) | 2024.06.09 |
(1) GCP 네트워크 및 서브넷 설정 (0) | 2024.06.09 |