Overview
이번 글에서는 Terraform을 활용하여 Google Cloud Storage(GCS)를 생성하고, Cloud CDN을 구성하는 방법에 대해 다룬다.
GCS는 정적 웹사이트 호스팅을 지원하며, Cloud CDN을 통해 글로벌 캐싱을 활용하면 빠르고 효율적인 콘텐츠 제공이 가능하다.
이를 위해 아래의 주요 작업을 수행한다.
- 도메인 설정: DNS Zone 생성 및 네임서버 등록
- 소유권 인증: Google Search Console을 활용한 도메인 소유권 인증
- GCS 버킷 생성: 웹사이트 정적 파일을 저장할 버킷 설정
- CDN 구성: HTTPS 트래픽을 지원하는 Cloud CDN을 설정하여 성능과 보안을 최적화
- SSL 인증서 설정: 자동화된 SSL 인증서를 적용하여 HTTPS 지원
Terraform을 사용하여 해당 작업을 자동화하면 인프라 배포가 더욱 간편하고 일관되게 유지될 수 있다.

📅 관련 글
2023.04.06 - [GCP] - GCP란? - 서비스 계정 & Project 생성 / SDK(gcloud) 설치
2023.04.06 - [GCP] - GCP IAM이란?
2023.04.12 - [GCP] - GCP - SDK(gcloud) 계정 2개 등록하기
2023.05.05 - [GCP] - GCP vs AWS 리소스 비교
2023.05.19 - [GCP] - GCP BigQuery란? & Data Warehouse
2023.09.23 - [GCP] - BigQuery와 DataFlow를 활용한 Data ETL(GCP)
2023.10.03 - [GCP] - Shared VPC를 사용하여 GKE 클러스터 생성시 IAM 설정
2023.12.18 - [GCP] - GCP를 활용한 데이터 자동화(MongoDB, CloudSQL, GA, Dune)
2024.01.20 - [GCP] - Terraform 으로 GCS 생성후 Cloud CDN 생성(GCP)
2024.03.04 - [GCP] - GCP에서 딥러닝을 위한 GPU VM 서버 만들기(GCP)
2024.04.24 - [Migration] - AWS에서 GCP로 마이그레이션하는 방법 및 고려사항
Google Cloud Storage 생성 후 Cloud CDN
1. Zone 생성
resource "google_dns_managed_zone" "somaz.link_zone" {
name = "somaz.link"
dns_name = "somaz.link."
}
- dns_name에 마지막에
.
을 꼭 붙여줘야 한다.
2. hosting.kr과 유사한 도메인을 관리 및 구매할 수 있게 해주는 사이트에서 네임서버를 위에 만든 zone과 동일하게 변경


아래의 사이트에서 도메인 전파를 확인하면 편하다.
https://www.whatsmydns.net/#NS/
3. 소유권 설정 및 TXT 레코드 등록
resource "google_dns_record_set" "somaz.link_google_verification" {
name = "somaz.link."
type = "TXT"
ttl = 300
managed_zone = google_dns_managed_zone.somaz.link_zone.name
rrdatas = ["google-site-verification=...."]
depends_on = [google_dns_managed_zone.somaz.link_zone]
}
- 소유권 설정을 하지 않으면, Google Cloud bucket을 생성할 수 없다.
아래의 사이트에서 소유권 설정을 위한 TXT 레코드를 제공받을 수 있다.
https://search.google.com/search-console

소유권 인증 후에 설정 - 사용자 및 권한
에서 테라폼에서 사용하고 있는 서비스 계정을 등록해준다.

4. bucket 생성
variables.tf
variable "somaz.link" {}
variable "somaz_link_lb_ip_name" {}
somaz.tfvars
somaz_link = "somaz.link"
somaz_link_lb_ip_name = "somaz-link-cdn-lb-ip"
cloud-storage.tf
resource "google_storage_bucket" "somaz_link" {
name = var.somaz_link
location = var.region
uniform_bucket_level_access = true
storage_class = "STANDARD"
labels = local.default_labels
// delete bucket and contents on destroy.
force_destroy = true
// Assign specialty files
website {
main_page_suffix = "index.html"
not_found_page = "404.html"
}
}
resource "google_storage_bucket_iam_member" "public_read_somaz_link" {
bucket = google_storage_bucket.somaz_link.name
role = "roles/storage.objectViewer"
member = "allUsers"
}
5. cdn 설정
cloud-dns-lb.tf
resource "google_compute_global_address" "somaz_link_lb_ip" {
name = var.somaz_link_lb_ip_name
}
resource "google_dns_record_set" "somaz_link_record" {
name = "somaz.link." # Notice the trailing dot, it's necessary
type = "A"
ttl = 300
managed_zone = google_dns_managed_zone.somaz_link_zone.name
rrdatas = [google_compute_global_address.somaz_link_lb_ip.address] # Replace with your IP address
depends_on = [google_dns_managed_zone.somaz_link_zone, google_compute_global_address.somaz_link_lb_ip]
}
cloud-cdn.tf
## CDN(somaz_link) ##
resource "google_compute_managed_ssl_certificate" "cdn_lb_certificate" {
name = "somaz-link-ssl-cert"
managed {
domains = [var.somaz_link]
}
}
resource "google_compute_backend_bucket" "somaz_link_bucket_backend" {
name = "somaz-link-backend"
bucket_name = var.somaz_link
enable_cdn = true
edge_security_policy = module.cloud_armor_region_block.policy_self_link
compression_mode = "AUTOMATIC"
custom_response_headers = [
"Access-Control-Allow-Origin: *",
"Access-Control-Allow-Methods: GET, HEAD, OPTIONS, PUT, POST, PATCH, DELETE",
"Access-Control-Allow-Headers: *",
"Strict-Transport-Security: max-age=31536000",
"X-Content-Type-Options: nosniff",
"X-XSS-Protection: 1; mode=block",
"Referrer-Policy: strict-origin-when-cross-origin"
]
depends_on = [google_storage_bucket.somaz_link, module.cloud_armor_ip_allow]
}
resource "google_compute_url_map" "somaz_link_http_url_map" {
name = "somaz-link-http-url-map"
default_url_redirect {
https_redirect = true
strip_query = false
}
depends_on = [google_storage_bucket.somaz_link, google_compute_backend_bucket.somaz_link_bucket_backend]
}
resource "google_compute_url_map" "somaz_link_https_url_map" {
name = "somaz-link-https-url-map"
default_service = google_compute_backend_bucket.somaz_link_bucket_backend.id
depends_on = [google_storage_bucket.somaz_link, google_compute_backend_bucket.somaz_link_bucket_backend]
}
resource "google_compute_target_http_proxy" "somaz_link_http_proxy" {
name = "somaz-link-http-proxy"
url_map = google_compute_url_map.somaz_link_http_url_map.self_link
}
resource "google_compute_global_forwarding_rule" "somaz_link_http_forwarding_rule" {
name = "somaz-link-http-forwarding-rule"
target = google_compute_target_http_proxy.somaz_link_http_proxy.self_link
port_range = "80"
ip_address = google_compute_global_address.somaz_link_lb_ip.address
}
resource "google_compute_target_https_proxy" "somaz_link_https_proxy" {
name = "somaz-link-https-proxy"
url_map = google_compute_url_map.somaz_link_https_url_map.self_link
ssl_certificates = [google_compute_managed_ssl_certificate.cdn_lb_certificate.self_link]
}
resource "google_compute_global_forwarding_rule" "somaz_link_https_forwarding_rule" {
name = "somaz-link-https-forwarding-rule"
target = google_compute_target_https_proxy.somaz_link_https_proxy.self_link
port_range = "443"
ip_address = google_compute_global_address.somaz_link_lb_ip.address
}
마무리
Terraform을 활용하면 GCS 및 Cloud CDN을 빠르고 안정적으로 배포할 수 있다.
이를 통해 정적 웹사이트를 글로벌하게 제공하고, 성능과 보안을 강화할 수 있다.
특히, Cloud Armor를 추가하여 보안성을 높이고, Object Lifecycle Management를 활용하여 비용 절감할 수 있다.
또한 Cloud Logging과 Monitoring을 설정하여 운영 중 문제를 빠르게 감지하고 대응할 수 있다.
향후에는 Terraform과 함께 Pulumi 또는 다른 IaC 도구를 활용한 비교 분석도 진행하면 보다 최적화된 클라우드 인프라 관리가 가능할 것이다.
Reference
none
'GCP' 카테고리의 다른 글
GCP에서 딥러닝을 위한 GPU VM 서버 만들기(GCP) (0) | 2024.03.04 |
---|---|
GCP를 활용한 데이터 자동화(MongoDB, CloudSQL, GA, Dune) (2) | 2023.12.22 |
Shared VPC를 사용하여 GKE 클러스터 생성시 IAM 설정 (0) | 2023.10.08 |
BigQuery와 DataFlow를 활용한 Data ETL(GCP) (0) | 2023.10.02 |
GCP BigQuery란? & Data Warehouse (0) | 2023.05.21 |