AWS CloudFormation模板-必需的值

时间:2020-07-02 12:29:29

标签: amazon-web-services amazon-cloudformation

当我们定义参数并在Resources中引用它时,仅在创建堆栈时才检查参数值的存在;并且如果我们没有提到参数值,那么堆栈创建将失败并回滚,但出现以下异常

参数验证失败:参数名称的参数值 xxx不存在。用户请求回滚。

我知道为什么,并且从AWS Cloud Formation文档中的general requirement of parameters很清楚。

•必须在运行时为AWS每个参数分配一个值 通过CloudFormation成功配置堆栈。

但是,我想向用户指示在堆栈创建之前很多时间都没有提及参数值的情况。

问题:有没有一种方法可以强制我们在继续创建堆栈之前输入参数的值?

例如,如果我们未提及堆栈名称,则控制台将不允许您继续操作。我想要类似的东西,如果缺少值,它将停止继续进行堆栈创建。此图像与我的问题无关。但是向您展示我希望自定义参数在缺少值的情况下显示什么

更新:如果有人喜欢类似的功能,则下面是示例解决方案,其中参数“名称”限制在继续操作前使用至少输入一个字符,并且“ SecuritygroupIngressCIDR”强制使用有效的IP。

Parameters:
  Name:
    Type: String
    AllowedPattern: ^[a-zA-Z0-9]*$
    MinLength: 1
    
  SecurityGroupIngressCIDR:
    Description: The IP address range that can be used to communicate to the EC2 instances
    Type: String
    MinLength: '9'
    MaxLength: '18'
    AllowedPattern: (\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})/(\d{1,2})
    ConstraintDescription: must be a valid IP CIDR range of the form x.x.x.x/x.

SampleVisual

2 个答案:

答案 0 :(得分:2)

您可以将AllowedPatternConstraint description组合为参数。

来自documentation

AllowedPattern
A regular expression that represents the patterns to allow for String types

ConstraintDescription
A string that explains a constraint when the constraint is violated.

答案 1 :(得分:0)

正如@Paolo所说,这是通常的方式。一个补充的例子是:

Parameters:
  Name:
    Type: String
    AllowedPattern: ^[a-zA-Z0-9]*$
    MinLength: 1

仅在您按Create stack(控制台中的第4步)时才会出错,这毫无意义。在指定参数或堆栈名称时(步骤2),不是同时。