AWS CloudFormation元数据中能否用条件控制参数栈界面显示?
Absolutely, you can make those S3 parameters appear only when pOption is set to true! Here's how to implement this using CloudFormation Conditions and the AWS::CloudFormation::Interface metadata:
Step 1: Define a Condition to Check the Option Value
First, add a condition that triggers when pOption is set to "true":
Conditions: ShowS3Parameters: !Equals [!Ref pOption, "true"]
Step 2: Update Your Parameters Section
Set a default value of "false" for pOption (as you requested) and formalize your parameter definitions:
Parameters: pOption: Type: String Default: "false" AllowedValues: - "true" - "false" Description: Do you want to run this option pS3BucketName: Type: String Description: S3 Bucket pS3KeyPrefix: Type: String Description: S3 Key Prefix
Step 3: Modify the Metadata for Conditional Display
You have two clean options here—either split parameters into conditional groups, or keep them in one group with conditional entries:
Option A: Split into Conditional Parameter Groups
This keeps your UI organized:
Metadata: 'AWS::CloudFormation::Interface': ParameterGroups: - Label: default: Option Selection Parameters: - pOption - !If - ShowS3Parameters - Label: default: AWS CloudFormation Bucket Info Parameters: - pS3BucketName - pS3KeyPrefix - !Ref "AWS::NoValue" ParameterLabels: pOption: default: Do you want to run this option pS3BucketName: default: S3 Bucket pS3KeyPrefix: default: S3 Key Prefix
With this setup, only the "Option Selection" group shows by default. When you toggle pOption to "true", the S3 parameter group will appear automatically.
Option B: Keep in a Single Group
If you prefer all parameters in one section, use !If directly in the parameters list:
Metadata: 'AWS::CloudFormation::Interface': ParameterGroups: - Label: default: AWS CloudFormation Bucket Info Parameters: - pOption - !If [ShowS3Parameters, pS3BucketName, !Ref "AWS::NoValue"] - !If [ShowS3Parameters, pS3KeyPrefix, !Ref "AWS::NoValue"] ParameterLabels: pOption: default: Do you want to run this option pS3BucketName: default: S3 Bucket pS3KeyPrefix: default: S3 Key Prefix
Here, the S3 parameters will only show up in the group when pOption is "true".
How It Works
The !Ref "AWS::NoValue" tells CloudFormation to omit the conditional parameter (or parameter group) entirely when the condition isn't met. This hides the fields from the stack creation UI until the option is enabled.
Your existing template version (2010-09-09) fully supports these features, so no need to update that.
内容的提问来源于stack exchange,提问作者Zach Olinske

