【问题标题】:Unable to reference CloudFormation resource in serverless.yml. Invalid variable reference syntax for variable UserPoolId无法在 serverless.yml 中引用 CloudFormation 资源。变量 UserPoolId 的变量引用语法无效
【发布时间】:2018-10-18 20:25:19
【问题描述】:

我正在使用无服务器框架将无服务器应用程序部署到 AWS。但是,CloudFormation 部分未按预期工作。我在网上查了一下,没有发现我的 YAML 有什么问题。

我正在使用 CloudFormation 创建 UserPool,然后创建 UserPoolClient。我有以下 YAML 用于 resources:

    resources:
      Resources:
        HttpBucket:
          Type: "AWS::S3::Bucket"
          Properties:
            BucketName: ${self:service}-${UserPoolId}
            AccessControl: PublicRead
            WebsiteConfiguration:
              IndexDocument: index.html
        UserPool:
          Type: "AWS::Cognito::UserPool"
          Properties:
            UserPoolName: ${self:service}-user-pool
            MfaConfiguration: "OFF"
            EmailVerificationSubject: "Your verification code"
            EmailVerificationMessage: "Your verification code is {####}. "  
            Schema:
              - Name: name
                AttributeDataType: String
                Mutable: true
                Required: true
              - Name: email
                AttributeDataType: String
                Mutable: false
                Required: true
              - Name: teamName
                AttributeDataType: String
                Mutable: true
                Required: false
              - Name: custom:supportedTeam
                AttributeDataType: String
                Mutable: true
                Required: false
              - Name: custom:payment
                AttributeDataType: String
                Mutable: true
                Required: false
                DeveloperOnlyAttribute: true
            UsernameAttributes:
              - email
            AutoVerifiedAttributes:
              - email
            AdminCreateUserConfig:
              InviteMessageTemplate:
                EmailMessage: 'Your username is {username} and temporary password is {####}. '
                EmailSubject: Your temporary password
                SMSMessage: 'Your username is {username} and temporary password is {####}. '
              UnusedAccountValidityDays: 7
              AllowAdminCreateUserOnly: false
            Policies:
              PasswordPolicy:
                RequireLowercase: true
                RequireSymbols: false
                RequireNumbers: true
                MinimumLength: 6
                RequireUppercase: true
        UserPoolClient:
          Type: "AWS::Cognito::UserPoolClient"
          Properties:
            ClientName: ${self:service}-client
            GenerateSecret: false
            UserPoolId: 
              Ref: UserPool
      Outputs:
        UserPoolId:
          Value: 
            Ref: UserPool
          Export:
            Name: "UserPool::Id"
        UserPoolClientId:
          Value: 
            Ref: UserPoolClient
          Export:
            Name: "UserPoolClient::Id"

在指定UserPoolClient(用作UserPoolId)时,我无法引用UserPool

以下内容:

UserPoolId: 
  Ref: UserPool

产生错误:

变量 UserPoolId 的变量引用语法无效。你可以 仅参考环境变量、选项和文件。您可以查看我们的文档 更多信息。

我不确定的另一件事是,我看到有人共享包含以下语法的 YAML:

UserPoolId: !Ref UserPool

但它也会失败并出现有关无效语法的错误(由于!Ref)。谁能帮我解惑?

【问题讨论】:

    标签: node.js amazon-web-services yaml serverless-framework serverless


    【解决方案1】:

    在离开电脑一段时间后,我认为问题可能出在其他地方,这就是这里的情况。

    我在环境变量下使用${UserPoolId} 并意识到这就是问题所在。我改成以下方式,问题就解决了。我没有正确引用用户池 ID(它引用了名为 UserPoolId 的 serverless.yml 局部变量,但它不存在)

    userPoolId: 
          Ref: UserPool
    

    这只是我的错误,现在已经解决了。

    【讨论】:

      【解决方案2】:

      我在编写模板时发现它有时会进行不正确的语法高亮显示,我必须编写它以使其无法检查。

      参考:用户池

      Fn::Sub: '${UserPool}'

      通过与 Substitute 交换,它无法检查字符串的内容是否有效,然后它将构造一个包含 UserPool 的 Ref 或用户池 id 的字符串

      Fn::Sub documentation

      【讨论】:

      • 您能详细说明一下吗?消息也会出错:unknown tag !<!Sub>
      • @UsamaEjaz 我已经更新了我的答案,你可以试试吗?
      • 感谢您的帮助。您的回答实际上引发了一个导致解决方案的想法(是我的错误)。我明白了,我可以使用像 Fn::Sub 这样的 cloudformation 参数(非常感谢你)。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-10-13
      • 2020-05-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多