如何在 AWS CloudFormation 模板中定义和测试空字符串?

Chr*_*s F 7 aws-cloudformation

我有一个接受此参数的 CloudFormation 模板

TargetGroupName:
  Type: String
  Description: 'Parameter to override target group name'
  Default: ''
Run Code Online (Sandbox Code Playgroud)

但是,我如何将 is 定义为空字符串,而不是默认的空字符串,就像这样?我知道Null 不是有效的关键字,但我想说明我想要做什么。

TargetGroupName:
  Type: String
  Description: 'Parameter to override target group name'
  Default: Null
Run Code Online (Sandbox Code Playgroud)

然后,我如何设置一个条件来测试空字符串,就像这样?

Conditions:
  CreateTargetGroup:
    !Not [ !Equals [ !Ref TargetGroupName, Null ] ]
Run Code Online (Sandbox Code Playgroud)

当然,该关键字Null会引发 CloudFormation 脚本验证异常,因为它不是有效的关键字。

Sag*_*ari 12

你可以这样做:

Parameters:

TargetGroupName:
    Description: 'Parameter to override target group name'
    Type: String
    Default: ""

Condition:

  IsTargetGroupNameEmpty:  !Equals [!Ref "TargetGroupName", ""]
Run Code Online (Sandbox Code Playgroud)

然后在您不想创建的每个资源中只需传递此行:

!If [ IsTargetGroupNameEmpty, [ !Ref <RESOURCE_NAME> ], !Ref "AWS::NoValue" ]
Run Code Online (Sandbox Code Playgroud)

请查看此文档: https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/pseudo-parameter-reference.html#cfn-pseudo-param-novalue