我很想知道是否有人知道在 Terraform 中使用条件计数语句的更好替代方法。我所说的“条件计数语句”是指根据变量输入等条件,计数将计算以创建 0 或 1 个资源的语句。
一个简单的例子:
resource "xxxxxxxx" "example" {
count = var.example != null ? 1 : 0
}
Run Code Online (Sandbox Code Playgroud)
在这里,只有当 var.example 有值(不为空)时才会创建资源,否则不会创建。
条件计数在实践中通常工作正常,但有时在比上述更复杂的用途中,它会带来在 Terraform Plan 期间出现错误的风险,因为它无法评估预应用计数的结果。
The "count" value depends on resource attributes that cannot be determined
until apply, so Terraform cannot predict how many instances will be created.
To work around this, use the -target argument to first apply only the
resources that the count depends on.
Run Code Online (Sandbox Code Playgroud)
有没有更好的方法可以达到在 Terraform 中按条件创建资源的相同效果?
我正在尝试使用提供商跨多个区域部署相同的实例。文件夹的树是:
.
|-- main.tf
`-- aws_instance
| `-- main.tf
`-- versions.tf
Run Code Online (Sandbox Code Playgroud)
main.tf 看起来像:
module "aws_instances" {
source = "./aws_instance"
providers = {
aws.east1 = aws.east1
aws.east2 = aws.east2
}
}
Run Code Online (Sandbox Code Playgroud)
aws_instance/main.tf 如下所示:
resource "aws_instance" "webserver" {
ami = "webserver-image"
instance_type = "t2.micro"
key_name = "EC2-keyPair-Name"
associate_public_ip_address = true
root_block_device {
volume_type = "gp2"
volume_size = "30"
delete_on_termination = false
}
Run Code Online (Sandbox Code Playgroud)
versions.tf 看起来像:
terraform {
required_version = ">= 1.0.4"
required_providers {
aws = {
source = "hashicorp/aws"
version = ">= 3.23.0" …Run Code Online (Sandbox Code Playgroud) 我有许多 Terraform 数据源和一个这样创建的本地块
data "aws_subnets" "subs" {
for_each = toset(["a", "b", "c"])
filter {
name = "vpc-id"
values = [data.aws_vpc.vpc.id]
}
filter {
name = "availability-zone"
values = ["${data.aws_region.region.name}${each.key}"]
}
}
data "aws_vpc" "vpc" {
default = false
}
data "aws_region" "region" {}
locals {
ids = [for az in data.aws_subnets.subs : az.ids[1]]
}
Run Code Online (Sandbox Code Playgroud)
和一个输出块
output "main" {
value = local.ids
}
Run Code Online (Sandbox Code Playgroud)
但是当我运行 terraform apply 时出现错误
The given key does not identify an element in this collection value: the given …
amazon-web-services terraform terraform-provider-aws terraform0.12+
我试图了解 Terraform 中的版本是如何工作的。
例如Terraform 文档中的这个演示:
terraform {
required_providers {
mycloud = {
source = "mycorp/mycloud"
version = "~> 1.0"
configuration_aliases = [ mycloud.alternate ]
}
}
}
Run Code Online (Sandbox Code Playgroud)
我没有在与版本相关的官方 Terraform 文档中找到任何相关内容,尤其是此~>. 我错过了吗?
~>terraform required_providers 版本中的含义是什么?有文件吗?谢谢
我试图提供一个简单的使用keyvault方法,但出现以下错误:azureterraform
错误:检索
contactKeyVault:keyvault.BaseClient#GetCertificateContacts:发送请求失败:StatusCode=0 -- 原始错误:超出上下文截止时间
这是我的地形片段:
# data "azurerm_client_config" "current" {}
data "azuread_client_config" "current" {}
resource "azurerm_key_vault" "aks_key_vault" {
name = var.aks_kv_name
location = var.location
resource_group_name = var.rg_name
enabled_for_disk_encryption = true
tenant_id = data.azuread_client_config.current.tenant_id
# soft_delete_retention_days = var.aks_kv_soft_delete_retention_days
# purge_protection_enabled = false
sku_name = var.aks_kv_sku
access_policy {
tenant_id = data.azuread_client_config.current.tenant_id
object_id = data.azuread_client_config.current.object_id
key_permissions = var.aks_kv_key_permissions
secret_permissions = var.aks_kv_secret_permissions
storage_permissions = var.aks_kv_storage_permissions
}
tags = merge(var.common_tags)
}
Run Code Online (Sandbox Code Playgroud)
这是variables.tf
# Key Vault related variables
aks_kv_name …Run Code Online (Sandbox Code Playgroud) 地形图是否可以默认返回 null?
例子:
variable "MY_VAR" {
type = map(number)
default = {}
}
Run Code Online (Sandbox Code Playgroud)
并在资源中引用:
var.MY_VAR["someKey"]
Run Code Online (Sandbox Code Playgroud)
如果 someKey 没有条目,我只想返回 null,而不抛出“给定的键无法识别此集合中的元素”错误。是否有办法将 terraform 地图配置为这种方式?是否有某种可以利用的默认功能?
我在 MacOS 上编写 terraform,这是应用在 Linux 上运行的 terraform 的构建管道。
当我在 mac 上本地更新 terraform 提供程序然后提交时,version.tf它.terraform.lock.hcl会将新的哈希值放入 mac 中,而不是 linux 中。
当构建管道执行 a 时terraform init,会下载提供程序的 Linux 版本并将其哈希添加到.terraform.lock.hcl. 如果 git 工作副本脏了,我希望构建失败。
是否可以在我的 mac 上生成哈希值并将其添加到 linux 的 terraform 中?
我正在尝试生成证书并使其通过 DNS 进行验证...一切似乎都有效,直到我使用资源“aws_acm_certificate_validation”时的最后步骤
\n我的代码如下:
\n# Create Certificate\nresource "aws_acm_certificate" "ic_cert" {\n provider = aws.us-east-1\n domain_name = aws_s3_bucket.ic_bucket_main.bucket\n subject_alternative_names = [aws_s3_bucket.ic_bucket_redirect.bucket]\n validation_method = "DNS"\n\n tags = {\n Billing = "company X"\n }\n\n lifecycle {\n create_before_destroy = true\n }\n}\n\n# Validate Certificate via DNS\n# get zone_id\n\ndata "aws_route53_zone" "selected" {\n provider = aws.us-east-1\n name = aws_s3_bucket.ic_bucket_main.bucket\n}\n\n# Generate DNS Records\nresource "aws_route53_record" "ic_DNS_validation" {\n provider = aws.us-east-1\n for_each = {\n for dvo in aws_acm_certificate.ic_cert.domain_validation_options : dvo.domain_name => {\n name = dvo.resource_record_name\n record = dvo.resource_record_value\n type …Run Code Online (Sandbox Code Playgroud) amazon-web-services amazon-route53 terraform terraform-provider-aws
我在为我的模块定义提供程序时遇到问题。\n当我运行时,Terraform 无法找到提供程序的插件,terraform init并且当我运行时,它会显示我的模块的错误提供程序terraform providers。
我在 Debian 11 上使用 Terraform版本 1.3.7。
\n这是我正在尝试做的一个例子。
\n我有一个 main.tf,其中是我的主要配置和模块。在此示例中,我使用单个模块来创建 Docker 容器。
\n.\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 main.tf\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 modules/\n \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 container_module/\n \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 main.tf\nRun Code Online (Sandbox Code Playgroud)\n在根模块project/main.tf文件中,我定义提供程序并调用该模块:
terraform { \n required_providers { \n docker = { \n source = "kreuzwerker/docker" \n version = "3.0.1" \n } \n } \n} \n \nprovider "docker" { \n host = "unix:///var/run/docker.sock" \n} \n \nmodule "container" { \n source = "./modules/container_module" …Run Code Online (Sandbox Code Playgroud) aws_wafv2_web_acl_logging_configuration我正在尝试将该资源与aws_cloudwatch_log_groupTerraform 配置中的资源集成。但是,我遇到了一个错误,指出:
Error reason: The ARN isn't valid. A valid ARN begins with arn: and includes other information separated by colons or slashes
Run Code Online (Sandbox Code Playgroud)
根据错误aws_cloudwatch_log_group arn是不正确的。
但我根据 Terraform 文档遵循了正确的格式。 https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/wafv2_web_acl_logging_configuration
有人知道这个错误的原因吗?我的代码如下。
resource "aws_cloudwatch_log_group" "test_waf_log_group" {
name = var.waf_log_group_name
retention_in_days = 14
}
resource "aws_wafv2_web_acl_logging_configuration" "log_test_waf" {
depends_on = [aws_cloudwatch_log_group.test_waf_log_group]
log_destination_configs = [aws_cloudwatch_log_group.test_waf_log_group.arn]
resource_arn = aws_wafv2_web_acl.test_waf.arn
}
Run Code Online (Sandbox Code Playgroud)