[英]Is it possible to create GCP resources using terraform without changing main.tf & varaible.tf everytime?
共享示例用於創建 GCP VM。 這里的目標是在一個項目中創建 5 個不同的 VM,而無需每次都更改 main.tf 和 varaible.tf。 當我第一次申請 terraform 時,我能夠使用以下模塊成功創建一個 VM,但要創建另一個 VM - 我正在做的是更新 variables.tfvars 中的新VM “名稱” 。 然后,當我執行 terraform 申請時,TF 正在刪除在第一次運行時創建的現有 VM,然后嘗試創建一個新的 VM。 但我的目標是擁有一個模塊,我不應該每次都更改 main.tf & varaible.tf 並創建不同的虛擬機。 這可以通過 terraform 或任何可能的解決方案實現嗎?
主程序
resource "google_compute_instance" "default" {
project = var.project
name = var.name
machine_type = var.machine-type
zone = var.zone
boot_disk {
initialize_params {
image = "debian-cloud/debian-9"
}
disk_encryption_key_raw = "5DPzBhkLSfXk8kg="
}
network_interface {
subnetwork_project = var.host_project_id
subnetwork = var.subnet_name
}
metadata = {
block-project-ssh-keys = true,
enable-oslogin = true,
serial-port-enable = false
}
metadata_startup_script = "sudo apt-get update && sudo apt-get install apache2 -y && echo '<!doctype html><html><body><h1>our collaborative problem-solving ability, and the warm professionalism of our teams.!</h1></body></html>' | sudo tee /var/www/html/index.html"
// Apply the firewall rule to allow external IPs to access this instance
tags = ["http-server"]
}
變量.tf
variable "name" {
type = string
description = "Name of the instance"
}
variable "machine-type" {
type = string
description = "Machine type of the instance"
}
variable "region" {
type = string
description = "Region in which the instance has to be created"
}
variable "zone" {
type = string
description = "Zone in which the instance has to be created"
}
variable "project" {
type = string
description = "Name of the project"
}
variable "network" {
type = string
description = "Name of the subnet"
}
variable "host_project_id" {
type = string
description = "Name of the host project for network"
}
variable "subnet_name" {
type = string
description = "Name of the subnet"
}
變量.tfvars
name = "test-2"
machine-type = "n1-standard-4"
region = "us-east4"
zone = "us-east4-a"
project = "demoapp"
network = "https://www.googleapis.com/compute/v1/projects/network-dev/global/networks/vpc-1"
host_project_id = "network-dev"
subnet_name = "subnet-1"
根據以下評論創建了一個文件夾結構模塊,但在刪除過程中遇到了問題。
project
|-- sa-01
| `-- variable.tfvars
|-- sa-02
| `-- variable.tfvars
|-- main.tf
`-- variables.tf
sa-01/變量.tfvars
sa_name = ["sa-test1","sa-test2","sa-test3"]
sa_dis_name = ["sa-test1","sa-test2","sa-test3"]
sa-02/變量.tfvars
sa_name = ["sa-test1","sa-test2","sa-test3"]
sa_dis_name = ["sa-test1","sa-test2","sa-test3"]
主程序
resource "google_service_account" "sa_npe_policy" {
count = "${length(var.sa_name)}"
project = "tools-npe"
account_id = "${element(var.sa_name, count.index)}"
display_name = "${element(var.sa_dis_name, count.index)}"
}
變量.tf
variable sa_name {
type = list(string)
description = "SA Name"
}
variable sa_dis_name {
type = list(string)
description = "SA Display Name"
}
有多種方法可以實現這一目標,每種方法各有利弊……
.tfvars
文件和多個.tfstate
文件您定義了多個變量文件,一個用於您的每個 VM,也就是環境。 應用 terraform 定義時,必須指定要使用的變量文件以及存儲此 terraform 配置的 state 文件的位置:
terraform apply -var-file=./env1/terraform.tfvars -state ./env1/env1.tfstate
我建議將每個環境的變量文件和 state 文件一起存儲在一個專用文件夾中。 在這種情況下,您最終會得到如下文件夾結構:
project
|-- env1
| |-- env1.tfstate
| `-- terraform.tfvars
|-- env2
| |-- env2.tfstate
| `-- terraform.tfvars
|-- env3
| |-- env3.tfstate
| `-- terraform.tfvars
|-- main.tf
`-- variables.tf
臨
缺點
*.tf
文件)一起存儲在一個存儲庫中。注意:如果您在 terraform 中使用本地后端,則此方法效果很好。我沒有使用遠程后端對其進行測試,但我認為它需要進一步改進。
您只使用main.tf
和variables.tf
創建一個模塊文件夾,但不使用variables.tfvars
。 您為每個虛擬機創建一個單獨的文件夾,每個虛擬機都有自己的main.tf
,其中包含您的模塊,如下所示:
module "vm" {
source = "../path/to/your/module"
# or: source = "git::https://github.com/yourorg/yourrepo//path/to/your/module"
name = "test-2"
machine-type = "n1-standard-4"
region = "us-east4"
zone = "us-east4-a"
project = "demoapp"
network = "https://www.googleapis.com/compute/v1/projects/network-dev/global/networks/vpc-1"
host_project_id = "network-dev"
subnet_name = "subnet-1"
}
臨
缺點
一般來說Terraform 工作區也可以是解決這個問題的一種方法,所以為了完整起見,我只想提及它們。 但是,我認為它們更適合您需要在不同階段(開發、測試、生產)或區域等管理等效環境的用例。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.