如何在AWS CLI命令中为CloudFormation模板传入参数及解决参数缺失报错
问题描述
执行AWS CLI命令创建CloudFormation栈时触发验证错误:
aws cloudformation create-stack \ --stack-name explaining-credit-decisions \ --template-body file://template.yaml
报错信息:
An error occurred (ValidationError) when calling the CreateStack operation: Parameters: [SolutionPrefix] must have values
使用的CloudFormation模板如下:
AWSTemplateFormatVersion: 2010-09-09 Description: > (SA0003) - sm-soln-explaining-credit-decisions: Used as root template for 'explaining credit decisions' stack. Version 1 Parameters: SolutionPrefix: Type: String Description: | Used to name resources created as part of this stack (and inside nested stacks too). Can be the same as the stack name used by AWS CloudFormation, but this field has extra constraints because it's used to name resources with restrictions (e.g. Amazon S3 bucket names cannot contain capital letters). AllowedPattern: '^sm-soln-explaining-[a-z0-9\-]{1,20}$' ConstraintDescription: Only allowed to use lowercase letters, hyphens and/or numbers. Should start with 'sm-soln-explaining-' for permission management. Should be 39 characters or less. CreateSageMakerNotebookInstance: Description: Create a SageMaker Notebook Instance as part of the stack? Type: String Default: 'true' AllowedValues: - 'true' - 'false' SageMakerNotebookInstanceType: Description: | Amazon SageMaker Notebook instance type Type: String Default: ml.t3.medium StackVersion: Description: | CloudFormation Stack version. Use 'release' version unless you are customizing the CloudFormation templates and solution artifacts. Type: String Default: release AllowedValues: - release - development Mappings: SolutionsS3BucketNamePrefix: development: Prefix: sagemaker-solutions-build release: Prefix: sagemaker-solutions Conditions: CreateSageMakerNotebookInstance: !Equals [ !Ref CreateSageMakerNotebookInstance, 'true' ] Resources: S3Bucket: Type: "AWS::S3::Bucket" Properties: BucketName: !Sub ${SolutionPrefix}-${AWS::Region}-${AWS::AccountId} PublicAccessBlockConfiguration: BlockPublicAcls: true BlockPublicPolicy: true IgnorePublicAcls: true RestrictPublicBuckets: true BucketEncryption: ServerSideEncryptionConfiguration: - ServerSideEncryptionByDefault: SSEAlgorithm: AES256 Metadata: cfn_nag: rules_to_suppress: - id: W35 reason: Avoids user having to manually create an Amazon S3 bucket for logs. - id: W51 reason: Current default access policy is sufficient. GlueStack: Type: "AWS::CloudFormation::Stack" Properties: TemplateURL: !Sub - "https://${SolutionsS3BucketNamePrefix}-${AWS::Region}.s3.amazonaws.com/Explaining-credit-decisions/cloudformation/glue/glue.yaml" - SolutionsS3BucketNamePrefix: Fn::FindInMap: [SolutionsS3BucketNamePrefix, Ref: StackVersion, Prefix] Parameters: SolutionPrefix: !Ref SolutionPrefix S3BucketName: !Ref S3Bucket SolutionsS3BucketName: !Sub - ${SolutionsS3BucketNamePrefix}-${AWS::Region} - SolutionsS3BucketNamePrefix: Fn::FindInMap: [SolutionsS3BucketNamePrefix, Ref: StackVersion, Prefix] SageMakerStack: Type: "AWS::CloudFormation::Stack" Properties: TemplateURL: !Sub - "https://${SolutionsS3BucketNamePrefix}-${AWS::Region}.s3.amazonaws.com/Explaining-credit-decisions/cloudformation/sagemaker/sagemaker.yaml" - SolutionsS3BucketNamePrefix: Fn::FindInMap: [SolutionsS3BucketNamePrefix, Ref: StackVersion, Prefix] Parameters: SolutionPrefix: !Ref SolutionPrefix StackVersion: !Ref StackVersion S3BucketName: !Ref S3Bucket GlueDatabase: !GetAtt GlueStack.Outputs.GlueDatabase GlueWorkflow: !GetAtt GlueStack.Outputs.GlueWorkflow SolutionsS3BucketName: !Sub - ${SolutionsS3BucketNamePrefix}-${AWS::Region} - SolutionsS3BucketNamePrefix: Fn::FindInMap: [SolutionsS3BucketNamePrefix, Ref: StackVersion, Prefix] CreateSageMakerNotebookInstance: !Ref CreateSageMakerNotebookInstance SageMakerNotebookInstanceType: !Ref SageMakerNotebookInstanceType SolutionAssistantStack: Type: "AWS::CloudFormation::Stack" Properties: TemplateURL: !Sub - "https://${SolutionsS3BucketNamePrefix}-${AWS::Region}.s3.amazonaws.com/Explaining-credit-decisions/cloudformation/solution-assistant/solution-assistant.yaml" - SolutionsS3BucketNamePrefix: Fn::FindInMap: [SolutionsS3BucketNamePrefix, Ref: StackVersion, Prefix] Parameters: SolutionPrefix: !Ref SolutionPrefix S3BucketName: !Ref S3Bucket SolutionsS3BucketName: !Sub - ${SolutionsS3BucketNamePrefix}-${AWS::Region} - SolutionsS3BucketNamePrefix: Fn::FindInMap: [SolutionsS3BucketNamePrefix, Ref: StackVersion, Prefix] ECRRepository: !GetAtt SageMakerStack.Outputs.ECRRepository Outputs: AwsAccountId: Value: !Ref AWS::AccountId AwsRegion: Value: !Ref AWS::Region EcrRepository: Value: !GetAtt SageMakerStack.Outputs.ECRRepository ContainerBuildProject: Value: !GetAtt SageMakerStack.Outputs.ContainerBuildProject GlueDatabase: Value: !GetAtt GlueStack.Outputs.GlueDatabase GlueWorkflow: Value: !GetAtt GlueStack.Outputs.GlueWorkflow SolutionPrefix: Value: !Ref SolutionPrefix S3Bucket: Value: !Ref S3Bucket IamRole: Value: !GetAtt SageMakerStack.Outputs.SageMakerIAMRole SagemakerNotebookInstanceNotebooks: Description: URL to notebooks on the Amazon SageMaker Notebook Instance. Condition: CreateSageMakerNotebookInstance Value: !GetAtt SageMakerStack.Outputs.SageMakerNotebookInstanceNotebooks SagemakerMode: Value: !If - CreateSageMakerNotebookInstance - "NotebookInstance" - "Studio"
报错原因
模板中的SolutionPrefix参数未设置Default属性,而CloudFormation要求所有没有默认值的参数必须在创建栈时显式提供值,否则会触发ValidationError。
同时,该参数有严格的格式约束:
- 必须以
sm-soln-explaining-开头 - 仅允许小写字母、数字和连字符
- 总长度不超过39字符
解决方法
可以通过两种方式在AWS CLI中传递SolutionPrefix参数:
方式1:命令行直接指定
使用--parameters选项,格式为ParameterKey=参数名,ParameterValue=参数值:
aws cloudformation create-stack \ --stack-name explaining-credit-decisions \ --template-body file://template.yaml \ --parameters ParameterKey=SolutionPrefix,ParameterValue=sm-soln-explaining-my-demo
注意替换sm-soln-explaining-my-demo为符合约束的前缀,比如sm-soln-explaining-us-west-2-test。
方式2:使用参数文件
创建JSON格式的参数文件(如params.json):
[ { "ParameterKey": "SolutionPrefix", "ParameterValue": "sm-soln-explaining-my-demo" } ]
然后在CLI命令中引用该文件:
aws cloudformation create-stack \ --stack-name explaining-credit-decisions \ --template-body file://template.yaml \ --parameters file://params.json
额外说明
如果需要修改其他有默认值的参数(比如调整Notebook实例类型),可以在参数列表中添加对应键值对:
aws cloudformation create-stack \ --stack-name explaining-credit-decisions \ --template-body file://template.yaml \ --parameters \ ParameterKey=SolutionPrefix,ParameterValue=sm-soln-explaining-my-demo \ ParameterKey=SageMakerNotebookInstanceType,ParameterValue=ml.t3.large
内容的提问来源于stack exchange,提问作者thamil_v
相关产品推荐
相关产品推荐

