I am trying to let the user choose the subnet from the cloud formation params and I keep getting errors when the params of the subnet are not selected with any choices.
When I do not select any subnet, it will throw an error Parameter validation failed: parameter value for parameter name SubnetIds does not exist. Rollback requested by user.
AWSTemplateFormatVersion: "2010-09-09"
Parameters:
SubnetIds:
Description: "Select multiple subnets from the list"
Type: List<AWS::EC2::Subnet::Id>
Conditions:
SubnetListNotEmpty:
Fn::Not:
- Fn::Equals:
- Fn::Join: ["", !Ref SubnetIds]
- ""
Resources:
NullResource:
Type: AWS::CloudFormation::WaitConditionHandle
Outputs:
ConditionValid:
Description: "valid condition"
Value: !If [SubnetListNotEmpty, "valid", "invalid"]
It would be a good help if anyone could drop some hints.
I understood the error, the AWS:NoValue
and ""
does not exist in the AWS::EC2::Subnet::Id. How do I handle if there is a chance that the subnet is not needed?
Updated stack
AWSTemplateFormatVersion: "2010-09-09"
Parameters:
SubnetId:
Description: "Select a subnet ID or leave blank"
Type: AWS::EC2::Subnet::Id
Default: ""
Conditions:
SubnetSelected: !Not [!Equals [!Ref SubnetId, ""]]
Resources:
NullResource:
Type:
AWS::CloudFormation::WaitConditionHandle
# Other properties
Outputs:
SubnetIdOutput:
Description: "Selected subnet ID or None"
Value: !If [SubnetSelected, !Ref SubnetId, "None selected"]
2
Answers
You can add the
AllowedPattern: ".+"
attribute to make sure it’s not empty:It’s not possible, because by definition the list cannot be empty. As you found out, AWS performs validation for AWS-specific parameter types, which means that null values are not accepted for such parameter types.
This is confirmed by the documentation (emphasis mine)
As a workaround, your best bet is to use a
CommaDelimitedList
, which alas implies not having the nice functionality offered by the AWS-specific parameter types.