【问题标题】:How to inject variables into a Terraform JSON template for a Logic App如何将变量注入逻辑应用的 Terraform JSON 模板
【发布时间】:2021-11-14 11:31:55
【问题描述】:

我有一个在 Azure 中构建的逻辑应用的导出模板。 JSON 文件包含订阅 ID 等信息,这些信息会因环境而异。如何使用变量而不是文字值,例如:

"defaultValue": "/subscriptions/**328974123908741329180713290587125**/resourceGroups/rg-management/providers/Microsoft.Web/connections/azureblob-5"

被替换为:

"defaultValue": "/subscriptions/**var.subscription**/resourceGroups/rg-management/providers/Microsoft.Web/connections/azureblob-5"

我不确定两件事。首先,如何实际使用它来创建逻辑应用程序,其次,我从 Azure 门户导出的 JSON 模板不包含有关与存储帐户的连接的任何信息,因此不知道如何解决这个问题。 JSON 遵循我更新的模块,它相当大。

这是我更新的 terraform 模块:

terraform {
  required_providers {
    azurerm = {
      configuration_aliases = [azurerm.env, azurerm.mgmt]
    }
  }
}

resource "azurerm_logic_app_workflow" "storage_replication" {
  name                = "logic-app-storage-replication-${var.environment_name}-${var.resource_location}"
  location            = var.resource_location
  resource_group_name = var.resource_group
}

templatefile("${path.module}/LogicAppStorageReplicationTemplate.json", }"
  {
    subscription = var.subscription_id,
    resource_group = var.resource_group,
    resource_location = var.resource_location,
    container = var.container
  }
)

这是导出的 JSON 模板,其中添加了变量,稍后我将处理密钥库:

{
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "workflows_ReplicateStorage_name": {
            "defaultValue": "ReplicateStorage",
            "type": "String"
        },
        "connections_azureblob_4_externalid": {
            "defaultValue": "/subscriptions/${subscription}/resourceGroups/${resource_group}/providers/Microsoft.Web/connections/azureblob-4",
            "type": "String"
        },
        "connections_azureblob_5_externalid": {
            "defaultValue": "/subscriptions/${subscription}/resourceGroups/${resource_group}/providers/Microsoft.Web/connections/azureblob-5",
            "type": "String"
        }
    },
    "variables": {},
    "resources": [
        {
            "type": "Microsoft.Logic/workflows",
            "apiVersion": "2017-07-01",
            "name": "[parameters('workflows_ReplicateStorage_name')]",
            "location": "${resource_location}",
            "identity": {
                "principalId": "#############",
                "tenantId": "################",
                "type": "SystemAssigned"
            },
            "properties": {
                "state": "Enabled",
                "definition": {
                    "$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
                    "contentVersion": "1.0.0.0",
                    "parameters": {
                        "$connections": {
                            "defaultValue": {},
                            "type": "Object"
                        }
                    },
                    "triggers": {
                        "When_a_blob_is_added_or_modified_(properties_only)_(V2)_2": {
                            "recurrence": {
                                "frequency": "Minute",
                                "interval": 10
                            },
                            "evaluatedRecurrence": {
                                "frequency": "Minute",
                                "interval": 10
                            },
                            "splitOn": "@triggerBody()",
                            "type": "ApiConnection",
                            "inputs": {
                                "host": {
                                    "connection": {
                                        "name": "@parameters('$connections')['azureblob']['connectionId']"
                                    }
                                },
                                "method": "get",
                                "path": "/v2/datasets/@{encodeURIComponent(encodeURIComponent('AccountNameFromSettings'))}/triggers/batch/onupdatedfile",
                                "queries": {
                                    "checkBothCreatedAndModifiedDateTime": false,
                                    "folderId": "/${container}",
                                    "maxFileCount": 1
                                }
                            }
                        }
                    },
                    "actions": {
                        "Create_blob_(V2)": {
                            "runAfter": {
                                "Get_blob_content_using_path_(V2)": [
                                    "Succeeded"
                                ]
                            },
                            "type": "ApiConnection",
                            "inputs": {
                                "body": "@body('Get_blob_content_using_path_(V2)')",
                                "headers": {
                                    "ReadFileMetadataFromServer": true
                                },
                                "host": {
                                    "connection": {
                                        "name": "@parameters('$connections')['azureblob_1']['connectionId']"
                                    }
                                },
                                "method": "post",
                                "path": "/v2/datasets/@{encodeURIComponent(encodeURIComponent('AccountNameFromSettings'))}/files",
                                "queries": {
                                    "folderPath": "@{replace(body('Get_Blob_Metadata_using_path_(V2)')?['Path'], body('Get_Blob_Metadata_using_path_(V2)')?['Name'], '')}",
                                    "name": "@body('Get_Blob_Metadata_using_path_(V2)')?['Name']",
                                    "queryParametersSingleEncoded": true
                                }
                            },
                            "runtimeConfiguration": {
                                "contentTransfer": {
                                    "transferMode": "Chunked"
                                }
                            }
                        },
                        "Get_Blob_Metadata_using_path_(V2)": {
                            "runAfter": {},
                            "type": "ApiConnection",
                            "inputs": {
                                "host": {
                                    "connection": {
                                        "name": "@parameters('$connections')['azureblob']['connectionId']"
                                    }
                                },
                                "method": "get",
                                "path": "/v2/datasets/@{encodeURIComponent(encodeURIComponent('AccountNameFromSettings'))}/GetFileByPath",
                                "queries": {
                                    "path": "@triggerBody()?['Path']",
                                    "queryParametersSingleEncoded": true
                                }
                            }
                        },
                        "Get_blob_content_using_path_(V2)": {
                            "runAfter": {
                                "Get_Blob_Metadata_using_path_(V2)": [
                                    "Succeeded"
                                ]
                            },
                            "type": "ApiConnection",
                            "inputs": {
                                "host": {
                                    "connection": {
                                        "name": "@parameters('$connections')['azureblob']['connectionId']"
                                    }
                                },
                                "method": "get",
                                "path": "/v2/datasets/@{encodeURIComponent(encodeURIComponent('AccountNameFromSettings'))}/GetFileContentByPath",
                                "queries": {
                                    "inferContentType": true,
                                    "path": "@body('Get_Blob_Metadata_using_path_(V2)')?['Path']",
                                    "queryParametersSingleEncoded": true
                                }
                            }
                        }
                    },
                    "outputs": {}
                },
                "parameters": {
                    "$connections": {
                        "value": {
                            "azureblob": {
                                "connectionId": "[parameters('connections_azureblob_4_externalid')]",
                                "connectionName": "azureblob-4",
                                "id": "/subscriptions/${subscription}/providers/Microsoft.Web/locations/${resource_location}/managedApis/azureblob"
                            },
                            "azureblob_1": {
                                "connectionId": "[parameters('connections_azureblob_5_externalid')]",
                                "connectionName": "azureblob-5",
                                "id": "/subscriptions/${subscription}/providers/Microsoft.Web/locations/${resource_location}/managedApis/azureblob"
                            }
                        }
                    }
                }
            }
        }
    ]
}

【问题讨论】:

  • 我已经阅读了该文档,但我仍然有点困惑,Terraform 不是我的强项。我还忘记了键/值对来自 Azure 密钥保管库。所以我不确定如何将这些拼凑在一起。

标签: terraform


【解决方案1】:

您可以使用templatefile 函数来做到这一点。

首先,在您的 .json 文件中,将 id 替换为 interpolation 表达式,如下所示:

"defaultValue": "/subscriptions/${subscription}/resourceGroups/rg-management/providers/Microsoft.Web/connections/azureblob-5"

然后在 Terraform 中,您将生成最终的 JSON,如下所示:

templatefile("${path.module}/my_template.json", { 
  subscription = "328974123908741329180713290587125" 
})

您提到您希望值来自Azure Key Vault secret,所以看起来像这样:

templatefile("${path.module}/my_template.json", { 
  subscription = azurerm_key_vault_secret.my_secret.value
})

当然,templatefile() 函数调用的结果需要分配给局部变量、资源属性或模块输入,但是如果没有看到您的任何代码,很难给出更完整的答案。

【讨论】:

  • 感谢您的帮助,这是到目前为止的模块,没什么可看的:terraform { required_providers { azurerm = { configuration_aliases = [azurerm.env, azurerm.mgmt] } } } resource "azurerm_logic_app_workflow " "storagereplication" { name = "StorageReplication" location = var.resource_location resource_group_name = azurerm_resource_group.example.name }
  • 对不起,我试图编辑评论但没有成功:感谢您的帮助,这是目前的模块,没什么可看的:terraform { required_providers { azurerm = { configuration_aliases = [azurerm.env, azurerm.mgmt] } } } resource "azurerm_logic_app_workflow" "storagereplication" { name = "StorageReplication" location = var.resource_location resource_group_name = azurerm_resource_group.example.name }
  • Azure 保管库 ID 是 data.azurerm_key_vault.mgmt_infra.id。不幸的是,我继承了这个项目,所以找到一半的东西有很多问题。
  • 你现在应该够了。如果您仍然卡住,请将您的所有代码发布在您的原始问题中(通过编辑问题),包括您尝试使用 templatefile() 函数,格式化为可读,。
  • 我为可读性道歉,我不确定那里发生了什么。我觉得我很好,完全同意,再次感谢您的帮助。我有最后一个问题。是否可以将地图存储在另一个文件中而不是内联调用它。模板 json 文件中将包含大量变量。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-10-21
  • 1970-01-01
  • 2021-01-20
  • 2020-09-26
  • 1970-01-01
  • 1970-01-01
  • 2017-05-26
相关资源
最近更新 更多