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
    • Helm Chart
    • Isito-EnvoyFilter
    • Apache Kafka
      • Kafka
      • Kafka Connect
      • Lenses
    • Vault
    • Longhorn – Storage
    • VictoriaMetrics
    • MetalLB
    • Kong Gateway
  • CI/CD
    • ArgoCD
    • ArgoWorkflows
    • Spinnaker
    • Jenkins
    • Harbor
    • TeamCity
    • Git
      • Bitbucket
  • Coding
    • Terraform
      • GCP – Google Cloud
      • AWS – Amazon Web Service
    • Golang
    • Laravel
    • Python
    • Jquery & JavaScript
    • Selenium
  • Log & Monitor
    • 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

  • 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] Lesson 8: Cloud PubSub with Terraform GCP - Google Cloud
[Terraform] Lesson 2: Create GCS and upload file via terraform GCP - Google Cloud
[Terraform] Lesson 1: Terraform with Google Cloud GCP - Google Cloud
[Terraform] Lesson 10: NoSQL Database in GCP with Terraform GCP - Google Cloud
[Terraform ] Using Terragrunt to provision AWS base on Terraform Module Terraform
[AWS] Create EKS Cluster and EKS Node Groups in Public and Private Subnets AWS - Amazon Web Service

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

  • Experiences for IP Addresses Shortage on EKS Clusters March 29, 2023
  • [Talisman] Discover the sensitive information in your code. March 28, 2023
  • [Prometheus/Grafana] Install Prometheus and Grafana on ubuntu. March 27, 2023
  • [Kong Gateway] WebSocket connection failed March 26, 2023
  • [Nextcloud] Can’t download files to have a size bigger than 2Gi on NextCloud – RaspBerry March 24, 2023

Archives

  • 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
    • ArgoCD
    • ArgoWorkflows
    • Git
      • Bitbucket
    • Harbor
    • Jenkins
    • Spinnaker
    • TeamCity
  • Coding
    • Golang
    • Jquery & JavaScript
    • Laravel
    • Python
    • Selenium
    • Terraform
      • AWS – Amazon Web Service
      • GCP – Google Cloud
  • Kubernetes & Container
    • Apache Kafka
      • Kafka
      • Kafka Connect
      • Lenses
    • Docker
    • Helm Chart
    • Isito-EnvoyFilter
    • Kong Gateway
    • Kubernetes
      • Ingress
    • Longhorn – Storage
    • MetalLB
    • Vault
    • VictoriaMetrics
  • Log & Monitor
    • DataDog
    • ELK
      • Kibana
      • Logstash
    • Grafana
    • Prometheus
  • Uncategorized
  • Admin

Copyright © 2023 NimTechnology.