Skip to content

NimTechnology

Trình bày các công nghệ CLOUD một cách dễ hiểu.

  • Kubernetes & Container
    • Docker
    • Kubernetes
      • Ingress
      • Pod
    • Helm Chart
    • OAuth2 Proxy
    • Isito-EnvoyFilter
    • Apache Kafka
      • Kafka
      • Kafka Connect
      • Lenses
    • Vault
    • Longhorn – Storage
    • VictoriaMetrics
    • MetalLB
    • Kong Gateway
  • CI/CD
    • ArgoCD
    • ArgoWorkflows
    • Argo Events
    • Spinnaker
    • Jenkins
    • Harbor
    • TeamCity
    • Git
      • Bitbucket
  • Coding
    • DevSecOps
    • Terraform
      • GCP – Google Cloud
      • AWS – Amazon Web Service
      • Azure Cloud
    • Golang
    • Laravel
    • Python
    • Jquery & JavaScript
    • Selenium
  • Log, Monitor & Tracing
    • DataDog
    • Prometheus
    • Grafana
    • ELK
      • Kibana
      • Logstash
  • BareMetal
    • NextCloud
  • Toggle search form

[Terraform] Lesson 3: GCP Networking with Terraform

Posted on April 8, 2022April 18, 2022 By nim No Comments on [Terraform] Lesson 3: GCP Networking with Terraform

Contents

Toggle
  • 1) Create VPC from Console
    • 1.1) Subnet creation mode: Automatic
    • 1.2) Subnet creation mode: Custom
  • 2) VPC with Terraform
    • 1.1) Create VPC with Terraform Script
      • 1.1.1) Find out a role according to requiring permission
    • 1.2) Create Subnet – Terraform
    • 1.3) Create Firewall Rule – Terraform

1) Create VPC from Console

1.1) Subnet creation mode: Automatic

sau khi chờ enable

Chúng ta sẽ có săn 1 vpc default

Giờ chúng ta sẽ tạo tay 1 VPC

Chỗ này đơn giản bạn sẽ hiểu là firewall sẽ chăn all.
Xong giờ ta mở Ping, mở SSH, mở internal access,… thì các VMs mới connect qua lại vơi nhau được.

Giờ click create.

1.2) Subnet creation mode: Custom

OK rồi he

2) VPC with Terraform

tạo thư mục network và bên trong có 3 file

File keys.json và provider.tf bạn tham khảo các bài trước.

Và sau đây là main.tf

resource "google_compute_network" "auto-vpc-tf" {
  name = "auto-vpc-tf"
  auto_create_subnetworks = true
}

resource "google_compute_network" "custom-vpc-tf" {
  name = "custom-vpc-tf"
  auto_create_subnetworks = false
}

resource "google_compute_subnetwork" "sub-sg" {
  name = "sub-sg"
  network = google_compute_network.custom-vpc-tf.id
  ip_cidr_range = "10.1.0.0/24"
  region = "asia-southeast1"
  private_ip_google_access = true

}

resource "google_compute_firewall" "allow-icmp" {
  name = "allow-icmp"
  network = google_compute_network.custom-vpc-tf.id
  allow {
    protocol = "icmp"
  }
  source_ranges = ["49.36.82.10/32"]
  priority = 455
}



output "auto" {
  value = google_compute_network.auto-vpc-tf.id
}

output "custom" {
  value = google_compute_network.custom-vpc-tf.id
}

1.1) Create VPC with Terraform Script

Đâu tiên chúng ta sẽ run như dưới trước
https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/compute_network

resource "google_compute_network" "auto-vpc-tf" {
  name = "auto-vpc-tf"
  auto_create_subnetworks = true
}

resource "google_compute_network" "custom-vpc-tf" {
  name = "custom-vpc-tf"
  auto_create_subnetworks = false
}

output "auto" {
  value = google_compute_network.auto-vpc-tf.id
}

output "custom" {
  value = google_compute_network.custom-vpc-tf.id
}

1.1.1) Find out a role according to requiring permission

Error: Error creating Network: googleapi: Error 403: Required ‘compute.networks.create’ permission for ‘projects/terraform-gcp-xxxx/global/networks/auto-vpc-tf’, forbidden

Nếu bạn lên terraform apply mà bị lỗi trên, bạn để ý dòng mình bôi đỏ lên.

Giờ chúng ta đã biết role nào là phù hợp thì chúng ta trở về role thôi

Giờ thì gõ terraform apply thôi

Đây là những j hiển thị trên terminal
trên web

1.2) Create Subnet – Terraform

Bạn thấy vpc custom không có config gì?
Chúng ta sẽ làm tiếp ở step tiếp theo.

resource "google_compute_network" "custom-vpc-tf" {
  name = "custom-vpc-tf"
  auto_create_subnetworks = false
}

resource "google_compute_subnetwork" "sub-sg" {
  name = "sub-sg"
  network = google_compute_network.custom-vpc-tf.id
  ip_cidr_range = "10.1.0.0/24"
  region = "asia-southeast1"
  private_ip_google_access = true
}

output "custom" {
  value = google_compute_network.custom-vpc-tf.id
}
Bạn để ý là khi tạo VPC thì rource: google_compute_network sẽ sinh ra id

Ở đây có description của values rồi nè!
https://registry.terraform.io/providers/hashicorp/google/latest/docs/data-sources/compute_regions

Mình có 1 điểm này để lưu ý
làm sào để google_compute_subnetwork liên kết với google_compute_network
bạn thấy terraform đã thêm config về subnet cho VPC
Trên web cũng đã hiển thị rồi he

1.3) Create Firewall Rule – Terraform

resource "google_compute_network" "auto-vpc-tf" {
  name = "auto-vpc-tf"
  auto_create_subnetworks = true
}

resource "google_compute_network" "custom-vpc-tf" {
  name = "custom-vpc-tf"
  auto_create_subnetworks = false
}

resource "google_compute_subnetwork" "sub-sg" {
  name = "sub-sg"
  network = google_compute_network.custom-vpc-tf.id
  ip_cidr_range = "10.1.0.0/24"
  region = "asia-southeast1"
  private_ip_google_access = true

}

resource "google_compute_firewall" "allow-icmp" {
  name = "allow-icmp"
  network = google_compute_network.custom-vpc-tf.id
  allow {
    protocol = "icmp"
  }
  source_ranges = ["49.36.82.10/32"]
  priority = 455
}



output "auto" {
  value = google_compute_network.auto-vpc-tf.id
}

output "custom" {
  value = google_compute_network.custom-vpc-tf.id
}
Bạn sẽ thấy là đây là rule cho phép ping từ source_ranges IP vào VM của bạn.

Priority thì đơn giản là đổ ưu tiên của rule đó
Lower value of priority implies higher precedence (eg, a rule with priority 0 has higher precedence than a rule with priority 1). DENY rules take precedence over ALLOW rules having equal priority

Không nói nhiều terraform apply thôi

và nếu bạn gặp lỗi bên dưới

Error: Error creating Firewall: googleapi: Error 403: Required ‘compute.firewalls.create‘ permission for ‘projects/terraform-gcp-346216/global/firewalls/allow-icmp’, forbidden

thì như mình chỉ ở trên 1.1.1) Find out a role according to requiring permission

Đi vào IAM -> role

Không nói nhiều terraform apply thôi

GCP - Google Cloud, Terraform

Post navigation

Previous Post: [Terraform] Lesson 2: Create GCS and upload file via terraform
Next Post: [Bootstrap] Hide and show elements by bootstrap.

More Related Articles

[Terraform Issue] Blocks of type “precondition” are not expected here, or Optional object type attributes are experimental. Terraform
[Terraform] DRY in Terraform Configurations With Dynamic Blocks Terraform
[Terraform] How to create or public a module terraform on the public registry Terraform
[Terraform] Lesson 6: Google Cloud Function with Terraform GCP - Google Cloud
[AWS] Create EKS Cluster and EKS Node Groups in Public and Private Subnets AWS - Amazon Web Service
[Terraform] Lesson 8: Cloud PubSub with Terraform GCP - Google Cloud

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Tham Gia Group DevOps nhé!
Để Nim có nhiều động lực ra nhiều bài viết.
Để nhận được những thông báo mới nhất.

Recent Posts

  • [Azure/Loadbalancer] Creating an internal load balancer on Azure Kubernetes Service (AKS). May 13, 2025
  • [Azure] The subscription is not registered to use namespace ‘Microsoft.ContainerService’ May 8, 2025
  • [Azure] Insufficient regional vcpu quota left May 8, 2025
  • [WordPress] How to add a Dynamic watermark on WordPress. May 6, 2025
  • [vnet/Azure] VNet provisioning via Terraform. April 28, 2025

Archives

  • May 2025
  • April 2025
  • March 2025
  • February 2025
  • January 2025
  • December 2024
  • November 2024
  • October 2024
  • September 2024
  • August 2024
  • July 2024
  • June 2024
  • May 2024
  • April 2024
  • March 2024
  • February 2024
  • January 2024
  • December 2023
  • November 2023
  • October 2023
  • September 2023
  • August 2023
  • July 2023
  • June 2023
  • May 2023
  • April 2023
  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022
  • August 2022
  • July 2022
  • June 2022
  • May 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021

Categories

  • BareMetal
    • NextCloud
  • CI/CD
    • Argo Events
    • ArgoCD
    • ArgoWorkflows
    • Git
      • Bitbucket
    • Harbor
    • Jenkins
    • Spinnaker
    • TeamCity
  • Coding
    • DevSecOps
    • Golang
    • Jquery & JavaScript
    • Laravel
    • NextJS 14 & ReactJS & Type Script
    • Python
    • Selenium
    • Terraform
      • AWS – Amazon Web Service
      • Azure Cloud
      • GCP – Google Cloud
  • Kubernetes & Container
    • Apache Kafka
      • Kafka
      • Kafka Connect
      • Lenses
    • Docker
    • Helm Chart
    • Isito-EnvoyFilter
    • Kong Gateway
    • Kubernetes
      • Ingress
      • Pod
    • Longhorn – Storage
    • MetalLB
    • OAuth2 Proxy
    • Vault
    • VictoriaMetrics
  • Log, Monitor & Tracing
    • DataDog
    • ELK
      • Kibana
      • Logstash
    • Fluent
    • Grafana
    • Prometheus
  • Uncategorized
  • Admin

Copyright © 2025 NimTechnology.