【问题标题】:Passing Parameter Files for CloudFormation Does Not Work with YML为 CloudFormation 传递参数文件不适用于 YML
【发布时间】:2019-01-04 18:52:53
【问题描述】:

解决方案:不要使用 yml。

我正在尝试传递包含数据库名称和数据库密码的参数以及我的aws cloudformation stack-create 命令。我返回了一个没有多大意义的错误。

Value of property MasterUsername must be of type String

现在,要知道的一件事是我拥有控制台访问权限,因此我可以从字面上看到密码和用户名在控制台中按照设置返回。在我看来,它们都像字符串。

YAML 版本

我在这样的模板中定义堆栈:

Parameters:
  DBUser:
    Description: db user
    Type: String
    MinLength: '4'
    MaxLength: '16'
    AllowedPattern: '[a-zA-Z][a-zA-Z0-9]*'
    ConstraintDescription: some description
  DBPass:
    Description: db password
    Type: String
    MinLength: '8'
    MaxLength: '36'
    AllowedPattern: '[a-zA-Z0-9]*'
    ConstraintDescription: must contain only alphanumeric characters
Resources:
  AppNode:
   (...)
  DatabaseInstance:
    Type: AWS::RDS::DBInstance
    Properties:
      DBName: app
      DBInstanceClass: db.t2.micro
      Engine: postgres
      EngineVersion: 10
      MasterUsername:
        - !Ref DBUser
      MasterUserPassword:
        - !Ref DBPass
  DatabaseSG:
    Type: AWS::RDS::DBSecurityGroup
    Properties:
      GroupDescription: Security Group for RDS public access
      DBSecurityGroupIngress:
        - CIDRIP: 0.0.0.0/0

在我的.env 文件中,我有这个:

[
  {
    "ParameterKey": "DBUser",
    "ParameterValue": "root"
  },
  {
    "ParameterKey": "DBPass",
    "ParameterValue": "mydbpassword"
  }
]

我运行这个命令:

aws cloudformation create-stack --stack-name app --template-body file://$PWD/stack.json --region us-west-2 --parameters file://$PWD/.env.json

文件类型。

所以首先想到的是它可能将这些值设置为空白。但事实并非如此,因为如果我根本不传递模板或参数,则会引发不同的错误。

An error occurred (ValidationError) when calling the CreateStack operation: Parameters: [DBUser, DBPass] must have values

AWS 是否重新定义了字符串是什么或这里发生了什么?

【问题讨论】:

  • 您能否展示您使用的实际 Json 堆栈,因为您没有在 YML 中执行此命令?

标签: amazon-web-services amazon-cloudformation devops


【解决方案1】:

更正您的模板如下。

Resources:
  AppNode:
   (...)
  DatabaseInstance:
    Type: AWS::RDS::DBInstance
    Properties:
      DBName: app
      DBInstanceClass: db.t2.micro
      Engine: postgres
      EngineVersion: 10
      MasterUsername:
        !Ref DBUser
      MasterUserPassword:
        !Ref DBPass

当您使用 - !Ref DBUser 时,它表示 yaml 中的 LIST,而不是 StringMasterUserPasswordMasterUserPassword 都是 String 类型。 AWS 支持 .yml.yaml 扩展。所以你的扩展没有问题。

【讨论】:

    【解决方案2】:

    很抱歉,yaml 文件的文件不支持外部参数。

    您可以在命令行中传递它们作为一种解决方法。

    参考:

    https://github.com/aws/aws-cli/issues/2275

    仍在进行中。

    希望对你有帮助。

    EDIT1:

          MasterUsername:
            - ${DBUser}
          MasterUserPassword:
            - ${DBPass}
    

    您的 YAML 语法似乎不正确。

    参考:

    http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/quickref-general.html

    【讨论】:

    • 感谢您的链接!我试过使用这样的命令行:aws cloudformation create-stack --stack-name app --template-body file://$PWD/stack.yml --region us-west-2 --parameters ParameterKey=DBUser,ParameterValue=root ParameterKey=DBPass,ParameterValue=pleasegodletmein。但我得到同样的错误。我应该完全放弃 yml 吗?
    • 唯一我发现我和你的文件扩展名之间的区别。用过,yaml。但是json完全支持,yaml仍然完全支持呢。
    • 我的参数文件有一个json 扩展名。你的意思是他们根本不支持 yml?
    • ${DBPass} 格式仅适用于 !Sub,不能仅作为参数使用。
    【解决方案3】:

    MasterUsername 必须是 String 类型

    这里有一个列表(数组),而不是字符串:

    MasterUsername:
            - !Ref DBUser
    

    改为在同一行使用:

    MasterUsername: !Ref DBUser
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-12-18
      • 2013-06-11
      • 2014-05-17
      • 1970-01-01
      • 2018-02-25
      • 2020-10-12
      • 2018-09-23
      • 1970-01-01
      相关资源
      最近更新 更多