我正在嘗試使用 terraform 模塊并創建了一個非常簡單的模塊。結構如下。
├───dev
│ dev.tfvars
│ main.tf
│ output.tf
│ variables.tf
│
└───modules
└───instances
main.tf
output.tf
variables.tf
versions.tf
terraform init
PS C:\Users\61421\github\IAC-TF\dev> terraform init
Initializing modules...
- ec2_create in ..\modules\instances
Initializing the backend...
Initializing provider plugins...
- Finding hashicorp/aws versions matching "~> 4.17.1"...
- Installing hashicorp/aws v4.17.1...
- Installed hashicorp/aws v4.17.1 (signed by HashiCorp)
terraform plan
PS C:\Users\61421\github\IAC-TF\dev> terraform plan
No changes. Your infrastructure matches the configuration.
我正在從我的 DEV 執行這些命令(我計劃在不同的目錄中構建 prod)。
dev.tfvars
instance_type = "t2.micro"
instance_keypair = "user001"
no_of_instances = 1
ami_id = "ami-0c6120f461d6b39e9"
main.tf
provider "aws" {
region = "ap-southeast-2"
}
module "ec2_create" {
source = "../modules/instances"
ami = var.ami_id
instance_type = var.instance_type
count = var.no_of_instances
}
variables.tf
variable "no_of_instances" {
description = "Number of Instances"
type = number
default = 0
}
variable "ami_id" {
description = "ami_id"
type = string
default = ""
}
variable "instance_type" {
description = "instance_type"
type = string
default = ""
}
我的示例模塊如下。
main.tf
resource "aws_instance" "web" {
ami = var.ami
instance_type = var.instance_type
count = var.no_of_instances
}
variables.tf
variable "ami" {
description = "ID of AMI to use for the instance"
type = string
default = ""
}
variable "instance_type" {
description = "instance_type of AMI to use for the instance"
type = string
default = ""
}
variable "no_of_instances" {
description = "Number of Instances"
type = number
default = 1
}
versions.tf
terraform {
required_version = "~> 1.1.7" # which means any version equal & above 4.17.1 like 4.17.2
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 4.17.1"
}
}
看起來沒有創建任何東西,我嘗試了相同的資源 aws_instance 與一個沒有模塊的檔案并創建了它。看起來我在呼叫模塊時遺漏了一些東西。感謝您對此的幫助。我是 terraform 的新手。
uj5u.com熱心網友回復:
這很可能發生,因為默認情況下您no_of_instances是0。因此沒有要創建的資源。
要使用您的dev.tfvars,您必須明確傳遞此檔案:
terraform plan -var-file=dev.tfvars
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/486290.html
標籤:亚马逊网络服务 地形 terraform-provider-aws
上一篇:在沒有ecs服務的情況下使用terraform運行新任務
下一篇:Excel合并兩個表
