TL;DR结尾
我完全是terragrunt的新手,我正在尝试使用它来利用它提供的DRY(不要重复自己)功能。我的树如下所示,我计划将这个存储库用于aws、gcp和azure,在这个用例中,我只关注azure,一旦我理解了terragrunt的用法,我应该能够将逻辑应用于其他提供商。我的树可能是问题之一,所以如果我做错了,请不要犹豫,让我知道。
我想重用我的代码,而不是一遍又一遍地复制粘贴相同的东西。关注azure,这里的最终目标是只创建一个资源组,其中包含一个azure密钥库,以便能够理解terragrunt的使用方式。
从我的理解创建租户。盐酸,subcription。HCL和envy。HCL允许我不必在我的代码中更改此值。
关于其余的我可能已经忘记了一些依赖根据我的错误信息…我将自己定位在IaC/部署/订阅-测试-1/开发/客户端-测试/和运行一个"terragrunt计划";但随后会出现一些错误,并显示以下错误消息:
错误信息:
系统找不到指定的路径。time=2023-03-30T09:26:00+02:00 level=error msg=无法确定底层退出代码,因此Terragrunt将以错误代码1退出
IaC/
├─ deployments/
│ ├─ azure/
│ │ ├─ subscription-test-1/
│ │ │ ├─ dev/
│ │ │ │ ├─ client-test/
│ │ │ │ │ ├─ terragrunt.hcl
│ │ │ │ ├─ env.hcl
│ │ │ ├─ ppd/
│ │ │ ├─ subscription.hcl
│ │ ├─ subscription-test-2/
│ │ │ ├─ dev/
│ │ │ ├─ ppd/
│ │ ├─ subscription-test-3/
│ │ │ ├─ prd/
│ │ │ ├─ sbx/
│ ├─ aws/
│ ├─ gcp/
│ ├─ tenant.hcl
├─ modules/
│ ├─ aws/
│ ├─ azuread/
│ │ ├─ security-groups/
│ │ ├─ spn/
│ ├─ azurerm/
│ │ ├─ akv/
│ │ │ ├─ main.tf
│ │ │ ├─ variables.tf
│ │ ├─ rg/
│ │ │ ├─ main.tf
│ │ │ ├─ variables.tf
│ ├─ databricks/
│ ├─ gcp/
├─ project-templates/
│ ├─ aws/
│ ├─ azure/
│ │ ├─ project-template-solution-1/
│ │ │ ├─ akv.tf
│ │ │ ├─ main.tf
│ │ │ ├─ rg.tf
│ │ │ ├─ variables.tf
│ │ │ ├─ terragrunt.hcl
│ │ ├─ project-template-solution-2/
│ │ ├─ project-template-solution-3/
│ ├─ gcp/
├─ terragrunt.hcl
下面是terragrunt &每个文件夹的地形代码:
IaC/terragrunt.hcl
locals {
# Automatically load subscription variables
subscription_vars = read_terragrunt_config(find_in_parent_folders("subscription.hcl"))
# Automatically load tenant-level variables
tenant_var = read_terragrunt_config(find_in_parent_folders("tenant.hcl"))
# Automatically load environment-level variables
env_vars = read_terragrunt_config(find_in_parent_folders("env.hcl"))
environment = local.env_vars.locals.environment
subscription_id = local.subscription_vars.locals.subscription_id
}
IaC/模块/azurerm/akv main.tf
terraform {
required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = "3.42.0"
}
}
}
#Configure the Azure Resource Management Provider
provider "azurerm" {
subscription_id = var.azure_subscription_id
tenant_id = var.azure_tenant_id
features {
key_vault {
purge_soft_delete_on_destroy = true
recover_soft_deleted_key_vaults = true
}
}
}
#create azure key vault
resource "azurerm_key_vault" "akv" {
name = lower("${var.azure_project_code}-${var.azure_env_code}-akv-01")
location = var.azure_resource_group_location
resource_group_name = var.azure_rg_name
enabled_for_disk_encryption = true
tenant_id = var.azure_tenant_id
soft_delete_retention_days = 7
purge_protection_enabled = false
sku_name = "standard"
}
IaC/模块/azurerm/akv variables.tf
variable "azure_subscription_id" {
type = string
description = "Azure Subscription Id"
}
variable "azure_tenant_id" {
type = string
description = "Azure Tenant Id"
}
variable "azure_rg_name" {
type = string
description = "Azure Resource Group Name"
}
variable "azure_resource_group_location" {
default = "west europe"
description = "Location of the resource group."
}
variable "azure_env_code" {
type = string
description = "Azure Environment Code"
}
variable "azure_project_code" {
type = string
description = "Azure Project Code"
}
IaC/模块/azurerm/rg main.tf
terraform {
required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = "3.42.0"
}
}
}
provider "azurerm" {
subscription_id = var.azure_subscription_id
tenant_id = var.azure_tenant_id
features {
resource_group {
prevent_deletion_if_contains_resources = false
}
}
}
#create azure resource group
resource "azurerm_resource_group" "rg" {
name = var.azure_rg_name
location = var.azure_resource_group_location
}
IaC/模块/azurerm/rg variables.tf
variable "azure_subscription_id" {
type = string
description = "Azure Subscription Id"
}
variable "azure_tenant_id" {
type = string
description = "Azure Tenant Id"
}
variable "azure_rg_name" {
type = string
description = "Azure Resource Group Name"
}
variable "azure_resource_group_location" {
default = "west europe"
description = "Location of the resource group."
}
IaC/project-template-solution-1/terragrunt.hcl
include {
path = find_in_parent_folders()
}
IaC/project-template-solution-1/akv.tf
module "akv" {
source ="../..//modules/azurerm/akv/"
azure_subscription_id = var.azure_subscription_id
azure_tenant_id = var.azure_tenant_id
azure_rg_name = var.azure_rg_name
azure_resource_group_location = var.azure_resource_group_location
azure_project_code = var.azure_project_code
azure_env_code = var.azure_env_code
}
IaC/project-template-solution-1/rg.tf
module "rg" {
source ="../..//modules/azurerm/rg/"
azure_subscription_id = var.azure_subscription_id
azure_tenant_id = var.azure_tenant_id
azure_rg_name = var.azure_rg_name
azure_resource_group_location = var.azure_resource_group_location
}
IaC/project-template-solution-1/main.tf
terraform {
required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = "3.42.0"
}
}
}
provider "azurerm" {
subscription_id = var.azure_subscription_id
tenant_id = var.azure_tenant_id
features {
resource_group {
prevent_deletion_if_contains_resources = false
}
}
}
IaC/project-template-solution-1/variables.tf
variable "azure_subscription_id" {
type = string
description = "Azure Subscription Id"
}
variable "azure_tenant_id" {
type = string
description = "Azure Tenant Id"
}
variable "azure_rg_name" {
type = string
description = "Azure Resource Group Name"
}
variable "azure_resource_group_location" {
default = "west europe"
description = "Location of the resource group."
}
variable "azure_env_code" {
type = string
description = "Azure Environment Code"
}
variable "azure_project_code" {
type = string
description = "Azure Project Code"
}
IaC/部署/天蓝色/tenant.hcl
locals {
tenant_id = "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX"
}
IaC/部署/天蓝色/subscription-test-1 subscription.hcl
locals {
subscription_id = "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX"
}
IaC/部署/天蓝色/subscription-test-1/dev/env.hcl
locals {
environment = "dev"
}
TL;DR:我正在尝试通过使用模块(模块文件夹)和"调用模块"来部署混合地形和地形的架构。(项目模板文件夹)。首先用azure部署一个资源组,其中包含一个azure密钥库。
我试图将我的代码上传到GitHub,但这是我第一次使用它,所以我可能犯了错误。你可以在这里找到它,如果你想下载和编辑它,并把你的更新发给我。https://github.com/leanne-kami/IaC
感谢任何愿意花时间帮助我的人:)
一个朋友帮我用以下两个步骤解决了这个问题:
-
删除IaC/deploy/azure/subscription-test-1/dev/client-test/terragrunt.hcl中的依赖块
-
编辑路径IaC/项目模板/天蓝色/project-template-solution-1/akv。特遣部队和rg。Tf使目录理解模块的实际位置文件夹。
模块"rg"{源 =& ". ./. .//模块/azurerm/rg/";Azure_subscription_id = var.azure_subscription_idAzure_tenant_id = var.azure_tenant_idAzure_rg_name = var.azure_rg_nameAzure_resource_group_location = var.azure_resource_group_location}