问题描述
我有 terraform 脚本,它在资源组的负载均衡器中创建后端地址池和负载均衡器规则.这些任务包含在 Azure 管道中.我第一次运行管道.它创建正确.如果我第二次运行管道.它没有更新现有的.它保留了以前版本创建的后端地址池和负载均衡器规则,并为此版本添加了额外的后端地址池和负载均衡器规则,这导致后端地址池和负载均衡器规则重复.请对此提出任何建议
资源azurerm_lb_backend_address_pool"示例"{resource_group_name = azurerm_resource_group.example.nameloadbalancer_id = azurerm_lb.example.idname = "BackEndAddressPool"}资源azurerm_lb_rule"示例"{resource_group_name = azurerm_resource_group.example.nameloadbalancer_id = azurerm_lb.example.id名称 = "LBRule"协议 = "全部"前端端口 = 0后端端口 = 0frontend_ip_configuration_name = "公共 IP 地址"enable_floating_ip = truebackend_address_pool_id = azurerm_lb_backend_address_pool.example}
这可能是因为 Terraform 状态文件在管道运行之间丢失.
默认情况下,Terraform 将状态存储在名为 terraform.tfstate 的文件中.在团队中使用 Terraform 时,使用本地文件会使 Terraform 的使用变得复杂,因为每个用户在运行 Terraform 之前必须确保他们始终拥有最新的状态数据,并确保没有其他人同时运行 Terraform.使用远程状态,Terraform 将状态数据写入远程数据存储,然后可以在团队的所有成员之间共享.Terraform 支持在 Terraform Cloud、HashiCorp Consul、Amazon S3、阿里云 OSS 等中存储状态.
远程状态是后端的一个特性.配置和使用远程后端很容易,您可以快速开始使用远程状态.如果您随后想迁移回使用本地状态,后端也可以让这一切变得简单.
您需要配置远程状态存储以保持状态.以下是使用 Azure Blob 存储的示例:
terraform {后端azurerm"{resource_group_name = "StorageAccount-ResourceGroup"storage_account_name = "abcd1234"container_name = "tfstate"key = "prod.terraform.tfstate"}}
使用 Blob 存储帐户内的 Blob 容器中的给定密钥将状态存储为 Blob.此后端还通过 Azure Blob 存储的本机功能支持状态锁定和一致性检查.
这在 azurerm Terraform 后端文档中有更完整的描述.
微软还提供了教程:将 Terraform 状态存储在 Azure Storage 中,这一步一步设置.
I have terraform script which creates Backend address pools and Loadbalancer rules in Loadbalancer in resource group. These tasks are included in Azure-pipeline. FOr the first time I run the pipeline.Its creating properly. If I run the pipeline for the second time. Its not updating the existing one .Its keeping the Backend address pools and Loadbalancer rules which are created by previous release and adding the extra Backend address pools and Loadbalancer rules for this release which is causing duplicates in Backend address pools and Loadbalancer rules. Any suggestions on this please
resource "azurerm_lb_backend_address_pool" "example" {
resource_group_name = azurerm_resource_group.example.name
loadbalancer_id = azurerm_lb.example.id
name = "BackEndAddressPool"
}
resource "azurerm_lb_rule" "example" {
resource_group_name = azurerm_resource_group.example.name
loadbalancer_id = azurerm_lb.example.id
name = "LBRule"
protocol = "All"
frontend_port = 0
backend_port = 0
frontend_ip_configuration_name = "PublicIPAddress"
enable_floating_ip = true
backend_address_pool_id = azurerm_lb_backend_address_pool.example
}
This is likely happening because the Terraform state file is being lost between pipeline runs.
You will want to configure Remote State storage to keep the state. Here is an example using Azure Blob Storage:
terraform {
backend "azurerm" {
resource_group_name = "StorageAccount-ResourceGroup"
storage_account_name = "abcd1234"
container_name = "tfstate"
key = "prod.terraform.tfstate"
}
}
This is more completely described in the azurerm Terraform backend docs.
Microsoft also provides a Tutorial: Store Terraform state in Azure Storage, which goes through the setup step by step.
这篇关于在创建另一个之前删除后端池和负载均衡器规则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!