简体   繁体   中英

Unable to reference CloudFormation resource in serverless.yml. Invalid variable reference syntax for variable UserPoolId

I am using the Serverless framework to deploy a serverless app to AWS. However, the CloudFormation part is not working as expected. I have looked up on the web and couldn't find anything wrong with my YAML.

I am creating a UserPool and then UserPoolClient by using CloudFormation. I have the following YAML for 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"

I am unable to reference UserPool when specifying UserPoolClient (to be used as UserPoolId ).

The following:

UserPoolId: 
  Ref: UserPool

yields the error:

Invalid variable reference syntax for variable UserPoolId. You can only reference env vars, options, & files. You can check our docs for more info.

Another thing which I am not sure about, I have seen people sharing YAML containing the following syntax:

UserPoolId: !Ref UserPool

But it also fails and errors about the invalid syntax (due to !Ref ). Can anyone clear me on this?

After taking some time off the computer, I thought that the problem could be somewhere else which is what the case here.

I was using ${UserPoolId} under environment variables and realized that this is where the problem lies. I changed to the following way and the problem got solved. I was not referencing user pool ID correctly (it was referencing serverless.yml local variable named UserPoolId which didn't exist)

userPoolId: 
      Ref: UserPool

This was just my mistake and is now solved.

I found whilst writing my template it sometimes would do incorrect syntax highlighting and I would have to write it so that it couldn't check.

Ref: UserPool

Fn::Sub: '${UserPool}'

By swapping it with Substitute, it cannot check that the contents of the string are valid and then it will construct a string containing the Ref to the UserPool, or the user pool id

Fn::Sub documentation

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM