在之前的一篇文章解决Logic Apps terraform部署时大小写问题里,有说过要介绍下用terraform来部署logic apps,实际的场景其实是用terraform来做logic apps的迁移,要迁移的terraform本身已经在某个region甚至在Azure Global中已经存在,现在要做的就是要把他迁移到Azure CN或者其他region
而logic apps本质上就是在serverless上跑的一些代码,所以其实要做的就是把这些代码做下迁移,而这个迁移其实有很多种方法,可以在portal完成,也可以用ARM Template或者Terraform,这里主要介绍的就是terraform的方法
找到要迁移的logic apps,export template,可以看到logic apps包含的代码其实都是在ARM Template里的,这样要迁移就方便多了
直接把ARM Template保存成一个JSON文件,然后在terrafrom中用azurerm_resource_group_template_deployment来部署即可,这里边有一些要注意的是connector的问题,在logic apps中可能存在各种connector,比如可以连接到key vault或者其他一些外部的服务,这些服务如果要用代码部署的话是比较麻烦的,比如说key vault的connector,要部署的话首先还要先用data读取managed api,然后创建API Connection,说实话这部分用代码部署真的会比较难受,可以参考的资料相对也偏少,key vault这个还算是能找到比较多例子的
data "azurerm_managed_api" "keyvault" {
name = "keyvault"
location = local.location
}
resource "azurerm_api_connection" "firewall" {
name = "keyvault"
resource_group_name = data.azurerm_resource_group.cn1hub.name
managed_api_id = data.azurerm_managed_api.keyvault.id
display_name = "Firewall logic apps key vault connection"
parameter_values = {
"token:clientId" : "${var.clientId}",
"token:clientSecret" : "${var.clientSecret}",
"token:TenantId" : "${local.tenantId}",
"token:grantType" : "client_credentials",
"vaultName" : "${local.firewall_kv_name}"
}
lifecycle {
# NOTE: since the connectionString is a secure value it's not returned from the API
ignore_changes = ["parameter_values"]
}
}
data "template_file" "firewall" {
template = file(local.logic_apps_file_path)
}
# https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/resource_group_template_deployment
resource "azurerm_resource_group_template_deployment" "firewall_logicapps" {
name = "firewall-logic-apps-deployment-update"
resource_group_name = data.azurerm_resource_group.cn1hub.name
deployment_mode = "Incremental"
template_content = data.template_file.firewall.template
depends_on = [
azurerm_api_connection.firewall
]
}
代码准备好之后,剩下的就是标准的terraform plan & apply了