programing

Terraform은 모듈에 따라 달라집니다.

sourcejob 2023. 5. 4. 19:53
반응형

Terraform은 모듈에 따라 달라집니다.

저는 새로운 지형에서 모듈 구조에 대한 맞춤형 방위 정책을 만들었습니다.각 정책은 사용자 지정 모듈을 나타냅니다.제가 만든 모듈 중 하나는 새로 만든 zure 리소스에 대한 진단 로그를 활성화하는 것입니다.(진단 설정을 활성화하기 전에 "depends_on"을 구현하려면 어떻게 해야 합니까?) 아니면 다른 방법이 있습니까?먼저 저장소 계정을 생성한 다음 진단 설정 모듈을 생성하려고 합니다.에서main.tf(다른 모든 모듈을 호출하는 위치) 또는 리소스(모듈) 내부?

도와주셔서 감사합니다!!:)

아래 코드는 main.tf 파일을 나타냅니다.

//calling the create storage account name

module "createstorageaccount" {

source = "./modules/module_create_storage_account"
    depends_on = [
    "module_enable_diagnostics_logs"
  ]

}

이것은 스토리지 계정 생성 모듈을 나타냅니다.

resource "azurerm_resource_group" "management" {


  name     = "management-rg"
  location = "West Europe"
}

resource "azurerm_storage_account" "test" {
  name                     = "diagnostics${azurerm_resource_group.management.name}"
  resource_group_name      = "${azurerm_resource_group.management.name}"
  location                 = "${azurerm_resource_group.management.location}"
  account_tier             = "Standard"
  account_replication_type = "LRS"

  tags = {
    environment = "diagnostics"
  }
}

    depends_on = [
    "module_enable_diagnostics_logs"
  ]

대부분의 경우 참조 결과로 필요한 종속성이 자동으로 발생합니다.한 리소스에 대한 구성이 다른 리소스를 직접 또는 간접적으로 참조하는 경우 Terraform은 명시적으로 사용할 필요 없이 리소스 간의 종속성을 자동으로 추론합니다.depends_on.

이것은 모듈 변수와 출력이 종속성 그래프의 노드이기도 하기 때문에 작동합니다: 하위 모듈 리소스가 다음을 참조하는 경우:var.foo그런 다음 이 변수의 값이 의존하는 모든 항목에 간접적으로 의존합니다.

자동 종속성 감지가 불충분한 드문 상황에서도 모듈 변수와 출력이 종속성 그래프의 노드라는 사실을 이용하여 다음과 같은 간접 명시적 종속성을 생성할 수 있습니다.

variable "storage_account_depends_on" {
  # the value doesn't matter; we're just using this variable
  # to propagate dependencies.
  type    = any
  default = []
}

resource "azurerm_storage_account" "test" {
  name                     = "diagnostics${azurerm_resource_group.management.name}"
  resource_group_name      = "${azurerm_resource_group.management.name}"
  location                 = "${azurerm_resource_group.management.location}"
  account_tier             = "Standard"
  account_replication_type = "LRS"

  tags = {
    environment = "diagnostics"
  }

  # This resource depends on whatever the variable
  # depends on, indirectly. This is the same
  # as using var.storage_account_depends_on in
  # an expression above, but for situations where
  # we don't actually need the value.
  depends_on = [var.storage_account_depends_on]
}

이 모듈을 호출하면 다음과 같이 설정할 수 있습니다.storage_account_depends_on저장 계정 이전에 생성할 개체를 포함하는 모든 식에 대해 다음과 같이 입력합니다.

module "diagnostic_logs" {
  source = "./modules/diagnostic_logs"
}

module "storage_account" {
  source = "./modules/storage_account"

  storage_account_depends_on = [module.diagnostic_logs.logging]
}

그럼 당신의diagnostic_logs모듈에 대한 간접 종속성을 구성할 수 있습니다.logging모듈 간의 종속성 링크를 완료하기 위한 출력:

output "logging" {
  # Again, the value is not important because we're just
  # using this for its dependencies.
  value = {}

  # Anything that refers to this output must wait until
  # the actions for azurerm_monitor_diagnostic_setting.example
  # to have completed first.
  depends_on = [azurerm_monitor_diagnostic_setting.example]
}

ID가 포함된 출력과 같이 실제 값을 전달하여 관계를 표현할 수 있는 경우에는 이 방법을 사용하는 것이 더 쉽습니다.그러나 리소스 간에 데이터 흐름으로 모델링할 수 없는 관계가 있는 드문 상황에서는 출력과 변수를 사용하여 모듈 간의 명시적인 종속성도 전파할 수 있습니다.

모듈 종속성은 현재 Terraform 13에서 지원되며, 현재 릴리스 후보 단계에 있습니다.

resource "aws_iam_policy_attachment" "example" {
  name       = "example"
  roles      = [aws_iam_role.example.name]
  policy_arn = aws_iam_policy.example.arn
}

module "uses-role" {
  # ...

  depends_on = [aws_iam_policy_attachment.example]
}

리소스 수준에서 depends_on을 사용하는 것은 모듈 간 수준에서 depends_on을 사용하는 것과 다릅니다. 모듈 수준에서 매우 간단한 방법을 찾았습니다.

module "eks" {
source = "../modules/eks"
vpc_id = module.vpc.vpc_id
vpc_cidr = [module.vpc.vpc_cidr_block]
public_subnets = flatten([module.vpc.public_subnets])
private_subnets_id = flatten([module.vpc.private_subnets])
depends_on = [module.vpc] 
}

복잡한 관계 없이 간단한 모듈로 직접 종속성을 만들었습니다.

언급URL : https://stackoverflow.com/questions/58275233/terraform-depends-on-with-modules

반응형