Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 669
Usama Ejaz Avatar asked May 08 '18 19:05

Usama Ejaz


People also ask

How do I use self reference in yml serverless?

Reference Properties In serverless.yml To self-reference properties in serverless.yml, use the $ {self:someProperty} syntax in your serverless.yml. someProperty can contain the empty string for a top-level self-reference or a dotted attribute reference to any depth of attribute, so you can go as shallow or deep in the object tree as you want.

What is unresolvedvariablesnotificationmode in yml?

Variables allow users to dynamically replace config values in serverless.yml config. They are especially useful when providing secrets for your service to use and when you are working with multiple stages. If unresolvedVariablesNotificationMode is set to error, references to variables that cannot be resolved will result in an error being thrown.

Why is my CloudFormation template invalid?

The CloudFormation template is invalid: Template format error: Unresolved resource dependencies [ApiGatewayRestApi] in the Resources block of the template For debugging logs, run again after setting the "SLS_DEBUG=*" environment variable.

How do I reference environment variables in a yml file?

Referencing Environment Variables To reference environment variables, use the $ {env:SOME_VAR} syntax in your serverless.yml configuration file. It is valid to use the empty string in place of SOME_VAR.


Video Answer


1 Answers

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.

like image 71
Usama Ejaz Avatar answered Sep 21 '22 08:09

Usama Ejaz