在 Cloudformation 的 Fn::Sub 中使用 Ref
Using Ref inside Fn::Sub in Cloudformation
我正在尝试将 fn::sub
与其中的 Ref 一起使用。
我有一个字符串 "Comment xyz ${NAME}"。该字符串作为堆栈的参数出现。假设参数名称是 "test".
我现在想使用 Cloudformation 脚本中的 fn::sub
函数替换字符串中的 ${NAME}。
fn::sub:[{"Ref":"test"},{"NAME":"balaji"}]
预期输出是"Comment xyz balaji"。
但是,这是我得到的错误:
Template validation error: Template error: One or more Fn::Sub
intrinsic functions don't specify expected arguments. Specify a string
as first argument, and an optional second argument to specify a
mapping of values to replace in the string
让我知道这里需要修复什么。
根据docs,Fn::Sub
中的第一个参数必须是:
A string with variables that AWS CloudFormation substitutes with their associated values at runtime. Write variables as ${MyVarName}. Variables can be template parameter names, resource logical IDs, resource attributes, or a variable in a key-value map. If you specify only template parameter names, resource logical IDs, and resource attributes, don't specify a key-value map.
(强调已添加。)
因此,(在撰写本文时)不可能做到这一点。
如果你有一个参数:
"test": {
"Type": "String",
"Default": "Comment xyz ${NAME}"
}
然后替换:
"Fn::Sub": ["${test}", {"NAME": "balaji"}]
替换将产生文字字符串 Comment xyz ${NAME}
。
我没有尝试过 Fn::Sub,但是这些语法有效:
SubnetIds:
Fn::Split: [ ",", Ref: subnetsInternal]
和
EnvironmentName:
!Join ["", [Ref: applicationName, "-env"]]
我正在尝试将 fn::sub
与其中的 Ref 一起使用。
我有一个字符串 "Comment xyz ${NAME}"。该字符串作为堆栈的参数出现。假设参数名称是 "test".
我现在想使用 Cloudformation 脚本中的 fn::sub
函数替换字符串中的 ${NAME}。
fn::sub:[{"Ref":"test"},{"NAME":"balaji"}]
预期输出是"Comment xyz balaji"。
但是,这是我得到的错误:
Template validation error: Template error: One or more Fn::Sub intrinsic functions don't specify expected arguments. Specify a string as first argument, and an optional second argument to specify a mapping of values to replace in the string
让我知道这里需要修复什么。
根据docs,Fn::Sub
中的第一个参数必须是:
A string with variables that AWS CloudFormation substitutes with their associated values at runtime. Write variables as ${MyVarName}. Variables can be template parameter names, resource logical IDs, resource attributes, or a variable in a key-value map. If you specify only template parameter names, resource logical IDs, and resource attributes, don't specify a key-value map.
(强调已添加。)
因此,(在撰写本文时)不可能做到这一点。
如果你有一个参数:
"test": {
"Type": "String",
"Default": "Comment xyz ${NAME}"
}
然后替换:
"Fn::Sub": ["${test}", {"NAME": "balaji"}]
替换将产生文字字符串 Comment xyz ${NAME}
。
我没有尝试过 Fn::Sub,但是这些语法有效:
SubnetIds:
Fn::Split: [ ",", Ref: subnetsInternal]
和
EnvironmentName:
!Join ["", [Ref: applicationName, "-env"]]