Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AWS Events - Cognito User Pool Triggers fail to deploy #6593

Closed
zacharywenner opened this issue Aug 26, 2019 · 14 comments
Closed

AWS Events - Cognito User Pool Triggers fail to deploy #6593

zacharywenner opened this issue Aug 26, 2019 · 14 comments
Assignees

Comments

@zacharywenner
Copy link

This is a Bug Report

Description

  • What went wrong?
    I tried to deploy Cognito Triggers on 1.50.0. The error I'm getting related to PreSignUp: "is not authorized to perform: iam:PassRole on resource". In 1.49.0 it deploys correctly however I need to include a UserMigration trigger and this is only available in 1.50.0. I am deploying with full Admin privileges.

  • What did you expect should have happened?
    Deploy all

  • What was the config you used?

functions:
  preSignUp:
    handler: handler.autoConfirmUser
    events:
      - cognitoUserPool:
          pool: ${self:provider.environment.COGNITO_POOL}
          trigger: PreSignUp
          existing: true
  migrateUser:
    handler: handler.migrateUser
    events:
      - cognitoUserPool:
          pool: ${self:provider.environment.COGNITO_POOL}
          trigger: UserMigration
          existing: true
  postConfirmation:
    handler: handler.addUserToGroup
    events:
      - cognitoUserPool:
          pool: ${self:provider.environment.COGNITO_POOL}
          trigger: PostConfirmation
          existing: true
  • What stacktrace or error message from your provider did you see?
 
  An error occurred: PreSignUpCustomCognitoUserPool1 - Failed to create resource. User: arn:aws:sts::844180208621:assumed-role/ttw3-cognito-triggers-liv-IamRoleCustomResourcesLa-9S662UBZG46B/ttw3-cognito-triggers-live2-custom-resource-existing-cup is not authorized to perform: iam:PassRole on resource: arn:aws:iam::844180208621:role/ttw3c9c9ab1014_sns-role-live See details in CloudWatch Log: 2019/08/24/[$LATEST]8062f5ca32e947f6a06a0e36a5284457.

Similar or dependent issues:

https://forum.serverless.com/t/deploy-failed-when-existing-user-pool-triggered-function-is-removed/9135/2

Additional Data

  • Serverless Framework Version you're using: 1.50.0
  • Operating System: MacOS
  • Stack Trace:
  • Provider Error messages:
Serverless: Packaging service...
Serverless: Excluding development dependencies...
Serverless: Installing dependencies for custom CloudFormation resources...
Serverless: Uploading CloudFormation file to S3...
Serverless: Uploading artifacts...
Serverless: Uploading service ttw3-cognito-triggers.zip file to S3 (58.28 MB)...
Serverless: Uploading custom CloudFormation resources...
Serverless: Validating template...
Serverless: Updating Stack...
Serverless: Checking Stack update progress...
............................................
Serverless: Operation failed!
Serverless: View the full error output: https://us-west-2.console.aws.amazon.com/cloudformation/home?region=us-west-2#/stack/detail?stackId=arn%3Aaws%3Acloudformation%3Aus-west-2%3A844180208621%3Astack%2Fttw3-cognito-triggers-live2%2Ff71c10f0-c6b6-11e9-994f-021a2c452506
 
  Serverless Error ---------------------------------------
 
  An error occurred: PreSignUpCustomCognitoUserPool1 - Failed to create resource. User: arn:aws:sts::844180208621:assumed-role/ttw3-cognito-triggers-liv-IamRoleCustomResourcesLa-9S662UBZG46B/ttw3-cognito-triggers-live2-custom-resource-existing-cup is not authorized to perform: iam:PassRole on resource: arn:aws:iam::844180208621:role/ttw3c9c9ab1014_sns-role-live See details in CloudWatch Log: 2019/08/24/[$LATEST]8062f5ca32e947f6a06a0e36a5284457.
 
  Get Support --------------------------------------------
     Docs:          docs.serverless.com
     Bugs:          github.com/serverless/serverless/issues
     Issues:        forum.serverless.com
 
  Your Environment Information ---------------------------
     Operating System:          darwin
     Node Version:              10.15.3
     Framework Version:         1.50.0
     Plugin Version:            1.3.8
     SDK Version:               2.1.0
@pmuens
Copy link
Contributor

pmuens commented Dec 4, 2019

Hey @zacharywenner thanks for opening 👍

I just tested this today and couldn't reproduce the problem. Here's the service I've used:

service: test-${self:custom.idx}

provider:
  name: aws
  runtime: nodejs10.x
  versionFunctions: false
  region: eu-central-1
  stage: dev

custom:
  idx: 0
  COGNITO_POOL: TestPool

functions:
  preSignUp:
    handler: functions/handler.handler
    events:
      - cognitoUserPool:
          pool: ${self:custom.COGNITO_POOL}
          trigger: PreSignUp
          existing: true
  migrateUser:
    handler: functions/handler.handler
    events:
      - cognitoUserPool:
          pool: ${self:custom.COGNITO_POOL}
          trigger: UserMigration
          existing: true
  postConfirmation:
    handler: functions/handler.handler
    events:
      - cognitoUserPool:
          pool: ${self:custom.COGNITO_POOL}
          trigger: PostConfirmation
          existing: true

Could you update your Serverless Framework version and test again? We've shipped quite some fixes in the past (including fixes for the cognitoUserPool event source).

@zacharywenner
Copy link
Author

Thanks!!! Perfect.

@ghariosk
Copy link

ghariosk commented Dec 9, 2019

Hi there,

I am experiencing a similar issue on MacOS:

~/Test-Cognito via ⬢ v12.12.0 took 4m 28s
➜ sls -v
Framework Core: 1.59.3
Plugin: 3.2.5
SDK: 2.2.1
Components Core: 1.1.2
Components CLI: 1.4.0
Serverless: Operation failed!
Serverless: View the full error output: https://eu-central-1.console.aws.amazon.com/cloudformation/home?region=eu-central-1#/stack/detail?stackId=arn%3Aaws%3Acloudformation%3Aeu-central-1%3Axxxxxx%3Astack%2FTestCognitoTriggers-dev%2F8fb29640-1a90-11ea-98f1-0234c67787da

  Serverless Error ---------------------------------------

  An error occurred: SignUp2CustomCognitoUserPool1 - Failed to create resource. User: arn:aws:sts::xxxxxxx:assumed-role/TestCognitoTriggers-dev-IamRoleCustomResourcesLamb-1QB91PFWRLPIJ/TestCognitoTriggers-dev-custom-resource-existing-cup is not authorized to perform: iam:PassRole on resource: arn:aws:iam::xxxxxx:role/wazifa0a84a872_sns-role-dev See details in CloudWatch Log: 2019/12/09/[$LATEST]b120bcd6f8b74dc5a582ce6c9175d1ea.
service: TestCognitoTriggers
provider:
  name: aws
  runtime: nodejs12.x
  stage: dev
  region: eu-central-1
  versionFunctions: false


custom:
  COGNITO_POOL: <an-exisiting-user-pool-name>


functions:
  SignUp2:
    handler: handler.SignUp
    events:
      - cognitoUserPool:
          pool: ${self:custom.COGNITO_POOL}
          trigger: PreSignUp
          existing: true

I have updated serverless but still getting this error :(

@ghariosk
Copy link

ghariosk commented Dec 9, 2019

Update:
I have created a new user pool and it looks like its working now.
Something to note is that I had created that user pool using aws-amplify

@zacharywenner
Copy link
Author

@ghariosk That might have been related to my issue too. I created the user pool with aws-amplify.

@adir1661
Copy link

Hello , i am having the same issue without amplify, and i tried even to delete my whole cloudformation stack , and my user-pool, it had been created via serverless framework like i show in the yml below:
version:

??/??>sls -v
Framework Core: 1.66.0
Plugin: 3.4.1
SDK: 2.3.0
Components: 2.22.3

here is the error:

An error occurred: PreSignUpCustomCognitoUserPool1 - Failed to create resource. Cannot read 
property 'Id' of null See details in CloudWatch Log: 2020/03/12/[$LATEST]26151ea6b7c04756aa6
3c234ec49675a.

yml necessary details:

provider:
  name: aws
  runtime: nodejs12.x
  region: eu-west-1
  stage: ${opt:stage, 'dev'} 
functions:
  preSignUp:
     handler: confirm.preSignUp
     events:
     - cognitoUserPool:
        pool: CoverUserPool
        trigger: PreSignUp
        existing: true  
resources:
  Resources:
    CognitoUserPoolCoverUserPool:
      Type: AWS::Cognito::UserPool
      Properties:
        MfaConfiguration: OFF
        UserPoolName: ${self:custom.userPoolName}
        UsernameAttributes:
        - email
        AutoVerifiedAttributes:
        - email
        Policies:
          PasswordPolicy:
            MinimumLength: 6
            RequireLowercase: False
            RequireNumbers: True
            RequireSymbols: False
            RequireUppercase: False
        Schema:
        - AttributeDataType: String
          Mutable: true
          Name: role
          Required: false
          DeveloperOnlyAttribute: true
        - AttributeDataType: String
          Mutable: true
          Name: providerId
          Required: false
          DeveloperOnlyAttribute: true

@DavidWells
Copy link
Contributor

⊂◉‿◉つ I just ran into this as well. When removing the stack:

 Serverless Error ---------------------------------------

  An error occurred: UserMigrationCustomCognitoUserPool1 - The specified key does not exist. (Service: Amazon S3; Status Code: 404; Error Code: NoSuchKey; Request ID: FB8708D3526BEC46).

  Your Environment Information ---------------------------
     Operating System:          darwin
     Node Version:              10.4.1
     Framework Version:         1.67.0
     Plugin Version:            3.5.0
     SDK Version:               2.3.0
     Components Version:        2.22.3

I had the manually delete the stack and re-deploy.

It also seems like there *might be a race condition with the creation of these custom resources & when the permissions get attached to the lambda functions with cognitoUserPool triggers.

Aside, the reason I was removing the deploy was an error from PreSignUp trigger when using my login form. It was returning a UnexpectedLambdaException error something about the cognito not having permissions to invoke the presignup function.

After re-deploying the stack, everything works no lambda permission issue. I might try to use dependsOn if I see it again.

@christhomas
Copy link

I'm running into this problem. I don't understand why it's creating a lambda with a weird, non-controlled name that I can't set or specify. All I want is to set an existing cognito user pool, but it seems to want to do more than that, but create a lambda and I've got a highly controlled deployment role and this lambda is not in that list.

Whats the purpose of this lambda and can I avoid creating it?

@pgrzesik
Copy link
Contributor

Hey @christhomas - the Lambda that is created is needed for backing the CloudFormation Custom Resource that is used for managing the attachment of the trigger handlers to the Cognito User Pool - unfortunately, there is no native CloudFormation resource that allows to configure it if the user pool already exists and we need to do it via SDK calls that are performed by this Custom Resource. You cannot avoid it if you want to use an already existing User Pool - you can see explanation in the docs as well: https://www.serverless.com/framework/docs/providers/aws/events/cognito-user-pool#using-existing-pools

@christhomas
Copy link

I've been looking into this and I think it's a mistake that attaching a lambda to a cognito user pool creates the user pool. If you want to attach a lambda to that pool the user should create the pool, then this is merely just attaching A to B. No need for a custom lambda or any other logic. If you think about it, a user pool is quite a considerably important resource. It should be managed in responsible way, not created as a by-product of attaching a lambda.

Then the issue becomes easy. You take the Id and you just attach it. If it doesn't exist, surely the CF template will fail and the stack will roll back.

I honestly think this is way more complicated tahn it needs to be and if I specify existing:true, then it should just attach or fail. That is how you test for it's existence. Whether attaching succeeds or fails.

Doesn't that make more sense?

@christhomas
Copy link

Surely I can create a CF template which attaches a lambda to any cognito user pool just by Id and I don't need a lambda to check it's existence first? If it fails, then it's my problem and I have to fix that. Or is that just not possible to do?

@christhomas
Copy link

sorry, I moved my comments to the discussion if you prefer to comment there: #10917

@pgrzesik
Copy link
Contributor

No worries @christhomas - I've responded in the discussion

@reilg
Copy link

reilg commented Jun 15, 2023

Also ran into this issue today:

$ sls -v
Running "serverless" from node_modules
Framework Core: 3.31.0 (local) 3.30.1 (global)
Plugin: 6.2.3
SDK: 4.3.2

Basically, I was just trying to rename the function.

functions:
  cognitoEvents: # renamed from cognito
    handler: app/CognitoHandler.php 
    events:
      - cognitoUserPool:
        pool: <existing-pool-name>
        trigger: PostConfirmation
        existing: true

The error:

DELETE_FAILED: CognitoCustomCognitoUserPool1 (AWS::CloudFormation::CustomResource)                                                                                                                                                                                                
Received response status [FAILED] from custom resource. Message returned: User: arn:aws:sts::0000:assumed-role/api-dev-IamRoleCustomResourcesLambdaExecution-1JDQDZXG1Z4BW/api-dev-custom-resource-existing-cup is not authorized to perform: lambda:RemovePermission on resource: arn:aws:lambda:us-east-1:0000:function:api-dev-cognito because no identity-based policy allows the lambda:RemovePermission action See details in CloudWatch Log: 2023/06/15/[$LATEST]9b06105ce31b401b8c1021613e59a9f2 (RequestId: 163915a1-42fc-4e25-af22-e4be121a71a7)

The user pool was deployed through a separate serverless.yml config file.

I'm also using bref runtime, if that matters.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants