You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将ARM的Concat字符串转换为Terraform可用的Concat字符串?

Terraform 存储账户连接字符串拼接正确实现

推荐优先使用字符串插值方案,写法最简洁直观:

value = "DefaultEndpointsProtocol=https;AccountName=${azurerm_storage_account.website_storage_account.name};AccountKey=${azurerm_storage_account.website_storage_account.primary_access_key};EndpointSuffix=core.windows.net"

如果偏好使用format函数实现,正确写法如下:

value = format(
  "DefaultEndpointsProtocol=https;AccountName=%s;AccountKey=%s;EndpointSuffix=core.windows.net",
  azurerm_storage_account.website_storage_account.name,
  azurerm_storage_account.website_storage_account.primary_access_key
)

如果需要用concat/join类列表拼接函数实现,写法如下:

# join 方案(更适合字符串拼接场景)
value = join("", [
  "DefaultEndpointsProtocol=https;AccountName=",
  azurerm_storage_account.website_storage_account.name,
  ";AccountKey=",
  azurerm_storage_account.website_storage_account.primary_access_key,
  ";EndpointSuffix=core.windows.net"
])

# concat 方案
value = concat(
  ["DefaultEndpointsProtocol=https;AccountName="],
  [azurerm_storage_account.website_storage_account.name],
  [";AccountKey="],
  [azurerm_storage_account.website_storage_account.primary_access_key],
  [";EndpointSuffix=core.windows.net"]
)

之前写法的问题说明

  • 误用了ARM模板的parameters()函数,Terraform中引用已声明资源的属性直接使用资源类型.资源名.属性名格式即可,不需要套parameters函数;注意存储账户名需要取资源的.name属性,不能直接使用资源对象本身
  • format函数的字符串占位符直接使用%s即可,不需要额外加{},也不需要手动拆分字符串拼接多余的引号
  • concat函数要求所有入参都是同类型列表,之前的写法混合了列表和独立字符串,不符合语法规范

内容的提问来源于stack exchange,提问作者Jason

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.30 02:06:02