标签: terraform0.12+

terraform 模块需要 required_providers 吗?

我对在 terraform 文档中读到的内容有点困惑。以下是关于模块的说明:

https://www.terraform.io/docs/language/modules/index.html

模块是一起使用的多种资源的容器。模块由保存在一个目录中的 .tf 和/或 .tf.json 文件的集合组成。

以下是关于提供商的说明: https ://www.terraform.io/docs/language/providers/requirements.html

要求提供者

每个 Terraform 模块必须声明它需要哪些提供程序,以便 Terraform 可以安装和使用它们。提供者要求在 required_providers 块中声明。

提供程序要求由本地名称、源位置和版本约束组成:

terraform {
 required_providers {
    mycloud = {
      source  = "mycorp/mycloud"
      version = "~> 1.0"
   }
 }
}
Run Code Online (Sandbox Code Playgroud)

我对此感到困惑,因为我从未required_providers在任何模块中指定过,即使我正在使用提供程序并且它说我必须这样做。直到今天我什至不知道文档这么说。

那么我是否误解了文档,或者文档是错误的?我的每个模块required_providers是否需要?我的 terraform 配置在没有它们的情况下肯定可以工作,那么它们是否默认为某些内容?如果是,如何以及在哪里?

terraform terraform0.12+

26
推荐指数
2
解决办法
3万
查看次数

如果满足条件,则将额外元素添加到 terraform 中的列表中

我有一个 terraform 列表,看起来像:

array = ["a","b","c"]
Run Code Online (Sandbox Code Playgroud)

在这个 terraform 文件中,有两个变量,称为年龄和性别,如果年龄等于 12 并且性别等于男性(即,如果 var. age == 12 && var.gender == 'male' 则数组应为 ["a","b","c","d"],否则数组应为 ["a","b","c “])。以下内容是否会沿着正确的道路进行,或者我是否需要使用其他方法?

array = ["a","b","c", var.age == 12 && var.gender == 'male' ? "d" : null]
Run Code Online (Sandbox Code Playgroud)

terraform terraform-template-file terraform0.12+

25
推荐指数
3
解决办法
2万
查看次数

Terraform:模块 + for_each 的输出

我有一个使用模块的 Terraform 脚本。我想创建多个资源,所以我使用了 for_each 方法。

下面是我的变量配置:

variable bridge_domains {
  description = "Bridge Domain"
  type     = map
  default  = {
    bd1 = {
      name  = "BD1",
    },
    bd2 = {
      name    = "BD2"
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

在根main.tf文件中,我使用for_each以下命令遍历该变量:

module "schema_template_bd" {
  source = "./modules/schema_template_bd"
  for_each     =    var.bridge_domains
  
  schema       =    module.tenant.mso_schema.id 
  template     =    var.template

  bd           =    each.value.name
}
Run Code Online (Sandbox Code Playgroud)

然后在modules/schema_template_bd文件中我有以下内容:

resource "mso_schema_template_bd" "bd" {
  schema_id              =      var.schema
  template_name          =      var.template
  name                   =      var.bd
}
Run Code Online (Sandbox Code Playgroud)

该模块有一个输出,我在其中定义了以下内容:

output "mso_bd" { …
Run Code Online (Sandbox Code Playgroud)

terraform terraform0.12+

20
推荐指数
2
解决办法
2万
查看次数

如何忽略块中属性的更改

我正在 Azure 中部署 Web 应用程序,我想忽略对site_config块中scm_type属性的更改。

在部署期间,scm_type属性设置为None,稍后我们将在 Azure 门户中将其更改为不同的内容。

我当前的 TF 代码如下所示:

resource "azurerm_app_service" "web_app" {
  count               = length(var.app_names)
  name                = var.app_names[count.index]
  location            = data.azurerm_resource_group.app_resource_group.location
  resource_group_name = data.azurerm_resource_group.app_resource_group.name
  app_service_plan_id = azurerm_app_service_plan.app_plan.id
  tags                = var.tags
  app_settings        = var.app_settings[count.index]

  site_config {
    always_on                 = true
    websockets_enabled        = var.websockets_enabled[count.index]
    use_32_bit_worker_process = var.use_32_bit_worker_process
    scm_type                  = "None"
  }

  lifecycle {
    ignore_changes = [
      site_config.0.scm_type
    ]
  }
}
Run Code Online (Sandbox Code Playgroud)

我希望 terraform 计划在基础设施更新期间忽略scm_type 的变化,但它试图将其恢复为None。来自地形计划输出的行:

~ scm_type …

terraform terraform-provider-azure terraform0.12+

16
推荐指数
2
解决办法
2万
查看次数

terraform:根据键过滤地图列表

我正在实现一个安全组模块,以便它将通过获取和过滤 cidr 和 source_security_group_id 创建安全组规则来创建安全组规则。

当前模块配置。

安全组模块.tf

resource "aws_security_group" "this" {
  name                   = var.name
  description            = var.description
  vpc_id                 = var.vpc_id
  revoke_rules_on_delete = var.revoke_rules_on_delete
}

## CIDR Rule

resource "aws_security_group_rule" "cidr_rule" {
  count = length(var.security_group_rules)

  type              = var.security_group_rules[count.index].type
  from_port         = var.security_group_rules[count.index].from_port
  to_port           = var.security_group_rules[count.index].to_port
  protocol          = var.security_group_rules[count.index].protocol
  cidr_blocks       = var.security_group_rules[count.index].cidr_block
  description       = var.security_group_rules[count.index].description
  security_group_id = aws_security_group.this.id
}

## Source_security_group_id Rule

resource "aws_security_group_rule" "source_sg_id_rule" {
  count = length(var.security_group_rules)

  type              = var.security_group_rules[count.index].type
  from_port         = var.security_group_rules[count.index].from_port
  to_port           = var.security_group_rules[count.index].to_port
  protocol          = var.security_group_rules[count.index].protocol
  source_security_group_id = …
Run Code Online (Sandbox Code Playgroud)

terraform hcl terraform-provider-aws terraform0.12+

11
推荐指数
2
解决办法
2万
查看次数

Terraform - 迭代模板中的对象列表

我在迭代函数解释的模板内的对象列表时遇到问题templatefile

我有以下变量:

variable "destinations" {
  description = "A list of EML Channel Destinations."

  type = list(object({
    id  = string
    url = string
  }))
}
Run Code Online (Sandbox Code Playgroud)

templatefile这将作为传递给函数destinations。相关模板的片段是这样的:

Destinations:
  %{ for dest in destinations ~}
  - Id: ${dest.id}
    Settings:
      URL: ${dest.url}
  %{ endfor }
Run Code Online (Sandbox Code Playgroud)

规划 Terraform 时会出现以下错误:

Error: "template_body" contains an invalid YAML: yaml: line 26: did not find expected key
Run Code Online (Sandbox Code Playgroud)

我尝试将模板代码切换为以下内容:

Destinations:
  %{ for id, url in destinations ~}
  - Id: ${id}
    Settings:
      URL: ${url}
  %{ endfor …
Run Code Online (Sandbox Code Playgroud)

function terraform terraform0.12+

10
推荐指数
2
解决办法
9837
查看次数

为什么我无法将 dependent_on 块添加到具有提供程序配置的模块?

为我的 Terraform 模块编写示例时,出现错误:“模块包含提供程序配置”“无法使用 count、for_each 或 dependent_on 在模块内配置提供程序。”

当我尝试depends_on向模块声明添加一个块以避免在创建部署模块内资源所需的资源组之前尝试运行模块计划时,出现此错误。

如果我不添加该depends_on块,它也会中断,因为它找不到在模块运行以填充所需资源组数据源之前应创建的声明的资源组。

我发现要求删除块providers或删除所有数据源至少让人感到不舒服。

我找不到有关此错误或如何修复它的任何详细信息。

Terraform 代码中引发此错误的特定行。

azure terraform terraform-provider-azure terraform0.12+

10
推荐指数
1
解决办法
2万
查看次数

在使用 for_each 创建的资源内包含 for_each 的动态块

我正在尝试使用 Terraform 0.12+ 及其新的 for_each 在 Azure 中构建多个 vnet,但遇到了一些麻烦。我希望新功能能够让我创建一个接受复杂变量的通用网络模块,但我可能已经达到了它的极限,或者只是没有正确地思考它。本质上我的变量是这样构建的

variable "networks" {
  type = list(object({ name = string, newbits = number, netnum = number, subnets = list(object({ name = string, newbits = number, netnum = number}))}))
}
Run Code Online (Sandbox Code Playgroud)

您可以看到它是一个网络数组,其中包含该网络的子网子数组。这样做可以轻松记录网络,而无需额外的 terraform 资源需求,因此我们的网络团队可以轻松调整/扩展,而无需担心了解 HCL。

我可以使用计数及其索引执行构建多个 vnet 资源的必要功能,但我想使用 for_each,因为它允许对键进行索引,而不是可能随时间变化的计数(需要重新部署,而我们不能做)。

网络对象

networks = [
    {
        # x.x.1.0/24
        name        = "DMZ",
        newbits     = "8",
        netnum      = "1",
        subnets      = [
            {
                # x.x.1.0/25
                name        = "DMZ"
                newbits     = "9",
                netnum      = "2"
            }
        ]
    },
    { …
Run Code Online (Sandbox Code Playgroud)

terraform terraform0.12+

9
推荐指数
1
解决办法
1万
查看次数

Terraform:如果满足条件,则设置可选资源属性,否则不声明它

Terraform 上的一些资源支持可选属性。我有兴趣仅在满足条件时才声明和设置可选属性的值。否则,根本不要声明它。

我发现的所有建议都是基于声明属性并将其值设置为null如果条件不满足,而不是根本不声明属性。

我有办法做类似以下的事情吗?在伪代码中:

resource "some_resource" "this" {
  name = var.name

  if var.name == "some_name":
    some_optional_attribute = "some_value"
  else:
    pass  # do nothing, don't even declare the optional attribute
}
Run Code Online (Sandbox Code Playgroud)

请告诉我,提前谢谢!

conditional-statements terraform hcl terraform0.12+

9
推荐指数
1
解决办法
8894
查看次数

Terraform 错误 - 错误:需要参数或块定义

我的地形代码如下:

terraform {
  backend "remote" {
    hostname     = "hostname.com"
    organization = "companyname"
    workspaces {
      name = "GIT-TIO-DEV"
    }
  }
} 


provider "aws" {
  region = "us-east-1"
  version = "~> 2.22"
}

locals {
  aws_region                      = "us-east-1"
  ami_id                          = "ami-000db10762d0c4c05"
  appid_tag                       = "S00012"
  env_tag                         = "dev"
  name_tag                        = "TESTINSTANCE"
  awsaccount_tag                  = "myaccount"
  createdby_tag                   = "testuser"
  keyName                         = "tst-cloudservices-portal-dev"
  securityGroup                   = "sg-wwwwwwwww"
  vpc_id                          = "vpc-aaaaaaaaaaaaa"
  patchgroup_tag                  = "GroupA"
  iam_role                        = "tst-SR-ServiceRole"
  division_tag                    = "TIO"
  application_segment_tag         = "DEV"
  notes_tag                       = "link to notes …
Run Code Online (Sandbox Code Playgroud)

terraform-provider-aws terraform0.12+

9
推荐指数
1
解决办法
4万
查看次数