Chalice: How to capture user pool trigger (pre sign-up, custom message, etc.)

Created on 29 Sep 2017  路  3Comments  路  Source: aws/chalice

I'm new to AWS and chalice deployment, but I am the only person in my company working on our api. No one else knows anything about it and we contracted a company to create our api skeleton with basic code and chalice to deploy it. I've learned a lot and I can usually figure things out myself, but I'm stuck on this.

The chalice setup we have doesn't create a bunch of lambda functions in AWS Lambda like you would have to do if you weren't using chalice. It creates a single lambda function with my chalice app name. My app.py file has the app route for all the api calls that can be made.

I want to add a custom message trigger in my user pool to be able to replace the basic AWS verification emails so I can send customized verification emails when a user registers or wants to update their email, but the user pool triggers need a lambda function to call. Do I have to create a new chalice folder with its own app.py for each trigger or can I somehow capture those triggers in the existing app.py?

Whatever the above answer may be, assuming triggers can even be captured with chalice, I also need to know how to fill out @app.route for the trigger in app.py?
@app.route('api invoke url extension?', methods=['?'], content_types=['?'], ...?)

One last thing, is it possible to have the same trigger for all stages in my api? From documentation it appears to be stage specific.

If I can get those questions answered I'm pretty sure I could figure out the rest from http://docs.aws.amazon.com/cognito/latest/developerguide/cognito-user-identity-pools-working-with-aws-lambda-triggers.html.

Thanks in advance.

closing-soon-if-no-response question

Most helpful comment

Hi @GDavisSS,

To answer your questions:

Do I have to create a new chalice folder with its own app.py for each trigger or can I somehow capture those triggers in the existing app.py?
You do not have to create a new chalice application. However, you will have to do some configuration outside of chalice to hook up the cognito triggers. A good example of this is in a sample chalice trivia application that I have made. In it, I have two cognito lambda functions that I made using the app.lambda_function(): https://github.com/kyleknap/chalice-trivia/blob/3c23543c511db10e1b2a60e306daec2c1079d998/app.py#L87-L98. From there, you can do a chalice deploy to get the lambda functions up and running. However, you will need to use the console or the API directly to hook up the deployed lambda functions to the appropriate triggers on your cognito user pool. The good news is that this is only a one time thing. You do not have to do it for each new deployment.

One last thing, is it possible to have the same trigger for all stages in my api?
So for each chalice stage, there will be a separate lambda function created and I believe for cognito triggers you can only have one lambda function per type of trigger. So you would either have to:

1) Choose a lambda function from a particular stage (probably from a production stage) and use that for trigger.
2) Create a user pool for each chalice stage and associate the appropriate lambda functions of that stage to the associated user pool for that stage.

I would recommend doing option 2. However in general, I feel that it would be nice if chalice managed the creation of cognito user pools and triggers per stage and would be a good candidate in this proposal for managed AWS resources: https://github.com/aws/chalice/issues/516

Let us know if that helps.

All 3 comments

Hi @GDavisSS,

To answer your questions:

Do I have to create a new chalice folder with its own app.py for each trigger or can I somehow capture those triggers in the existing app.py?
You do not have to create a new chalice application. However, you will have to do some configuration outside of chalice to hook up the cognito triggers. A good example of this is in a sample chalice trivia application that I have made. In it, I have two cognito lambda functions that I made using the app.lambda_function(): https://github.com/kyleknap/chalice-trivia/blob/3c23543c511db10e1b2a60e306daec2c1079d998/app.py#L87-L98. From there, you can do a chalice deploy to get the lambda functions up and running. However, you will need to use the console or the API directly to hook up the deployed lambda functions to the appropriate triggers on your cognito user pool. The good news is that this is only a one time thing. You do not have to do it for each new deployment.

One last thing, is it possible to have the same trigger for all stages in my api?
So for each chalice stage, there will be a separate lambda function created and I believe for cognito triggers you can only have one lambda function per type of trigger. So you would either have to:

1) Choose a lambda function from a particular stage (probably from a production stage) and use that for trigger.
2) Create a user pool for each chalice stage and associate the appropriate lambda functions of that stage to the associated user pool for that stage.

I would recommend doing option 2. However in general, I feel that it would be nice if chalice managed the creation of cognito user pools and triggers per stage and would be a good candidate in this proposal for managed AWS resources: https://github.com/aws/chalice/issues/516

Let us know if that helps.

Thank you so much for that link. I believe that is all I will need to solve my problem. @app.lambda_function() was the missing piece I needed. When I deployed chalice before it only created one function. Now it creates two functions where the new one is specific to:
@app.lambda_function()
def customizeEmails(event, context):
Now I have a separate function in AWS Lambda that I put on the trigger in the user pool and I can see my code executing. I'm so glad it was a quick and easy solution.

Hey @kyleknap
Is there any way to set a lambda function as the cognito pool sign up trigger? Just like it does for SQS queue.
It would be great if chalice had a feature to automatically configure the trigger lambda function of Cognito User Pool Event.

Thanks

Was this page helpful?
0 / 5 - 0 ratings

Related issues

stannie picture stannie  路  4Comments

Erstwild picture Erstwild  路  4Comments

AtaruOhto picture AtaruOhto  路  3Comments

vrinda1410 picture vrinda1410  路  3Comments

jarretraim picture jarretraim  路  3Comments