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
Add Cognito User Pool Triggers #3657
Changes from 7 commits
4cf379d
e12e45e
1216447
838cd1b
271e3c8
ab92e3b
2f5c84c
f0253fe
f0e12ee
55d0a5e
0921dc8
dfbc5bb
5ab4230
f1367f3
78b4960
7410e5c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
<!-- | ||
title: Serverless Framework - AWS Lambda Events - Cognito User Pool Trigger | ||
menuText: Cognito User Pool Trigger | ||
menuOrder: 10 | ||
description: Setting up AWS Cognito User Pool Triggers with AWS Lambda via the Serverless Framework | ||
layout: Doc | ||
--> | ||
|
||
<!-- DOCS-SITE-LINK:START automatically generated --> | ||
### [Read this on the main serverless docs site](https://www.serverless.com/framework/docs/providers/aws/events/cognito-user-pool-trigger) | ||
<!-- DOCS-SITE-LINK:END --> | ||
|
||
# Cognito User Pool Trigger | ||
|
||
## Valid Triggers | ||
|
||
Serverless supports all Cognito User Pool Triggers as specified [here][aws-triggers-list]. | ||
|
||
## Simple event definition | ||
|
||
This will create a Cognito User Pool with the specified name. You can reference the same pool multiple times. | ||
|
||
```yml | ||
functions: | ||
preSignUp: | ||
handler: preSignUp.handler | ||
events: | ||
- cognitoUserPool: | ||
pool: MyUserPool | ||
trigger: PreSignUp | ||
customMessage: | ||
handler: customMessage.handler | ||
events: | ||
- cognitoUserPool: | ||
pool: MyUserPool | ||
trigger: CustomMessage | ||
``` | ||
|
||
## Multiple pools event definitions | ||
|
||
This will create multiple Cognito User Pools with their specified names: | ||
|
||
```yml | ||
functions: | ||
preSignUpForPool1: | ||
handler: preSignUp.handler | ||
events: | ||
- cognitoUserPool: | ||
pool: MyUserPool1 | ||
trigger: PreSignUp | ||
preSignUpForPool2: | ||
handler: preSignUp.handler | ||
events: | ||
- cognitoUserPool: | ||
pool: MyUserPool2 | ||
trigger: PreSignUp | ||
``` | ||
|
||
You can also deploy the same function for different user pools: | ||
|
||
```yml | ||
functions: | ||
preSignUp: | ||
handler: preSignUp.handler | ||
events: | ||
- cognitoUserPool: | ||
pool: MyUserPool1 | ||
trigger: PreSignUp | ||
- cognitoUserPool: | ||
pool: MyUserPool2 | ||
trigger: PreSignUp | ||
``` | ||
|
||
## Custom message trigger handlers | ||
|
||
For custom messages, you will need to check `event.triggerSource` type inside your handler function: | ||
|
||
```js | ||
// customMessage.js | ||
function handler(event, context, callback) { | ||
if (event.triggerSource === 'CustomMessage_AdminCreateUser') { | ||
// ... | ||
} | ||
if (event.triggerSource === 'CustomMessage_ResendCode') { | ||
// ... | ||
} | ||
} | ||
``` | ||
|
||
## Referencing a User Pool from the current stack | ||
|
||
You can attach the functions as events on a Cognito User Pool defined in the `Resources` section. | ||
|
||
```yml | ||
functions: | ||
preSignUp: | ||
handler: preSignUpForPool1.handler | ||
events: | ||
- cognitoUserPool: | ||
pool: MyUserPoolFromResources | ||
trigger: PreSignUp | ||
postConfirmation: | ||
handler: postConfirmation.handler | ||
events: | ||
- cognitoUserPool: | ||
pool: MyUserPoolFromResources | ||
trigger: PostConfirmation | ||
|
||
resources: | ||
Resources: | ||
MyUserPoolFromResources: | ||
Type: AWS::Cognito::UserPool | ||
``` | ||
|
||
## Overriding a generated User Pool | ||
|
||
A Cognito User Pool created by an event can be overridden by using the [logical resource name][logical-resource-names] in `Resources`: | ||
|
||
```yml | ||
functions: | ||
preSignUp: | ||
handler: preSignUpForPool1.handler | ||
events: | ||
- cognitoUserPool: | ||
pool: MyUserPool | ||
trigger: PreSignUp | ||
postConfirmation: | ||
handler: postConfirmation.handler | ||
events: | ||
- cognitoUserPool: | ||
pool: MyUserPool | ||
trigger: PostConfirmation | ||
|
||
resources: | ||
Resources: | ||
CognitoUserPoolMyUserPool: | ||
Type: AWS::Cognito::UserPool | ||
``` | ||
|
||
[aws-triggers-list]: http://docs.aws.amazon.com/cognito/latest/developerguide/cognito-user-identity-pools-working-with-aws-lambda-triggers.html#cognito-user-pools-lambda-trigger-syntax-shared | ||
[logical-resource-names]: ../guide/resources#aws-cloudformation-resource-reference |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -388,6 +388,13 @@ describe('#naming()', () => { | |
}); | ||
}); | ||
|
||
describe('#getCognitoUserPoolLogicalId()', () => { | ||
it('should normalize the user pool name and add the standard prefix', () => { | ||
expect(sdk.naming.getCognitoUserPoolLogicalId('us-east-1_v123sDAS1')) | ||
.to.equal('CognitoUserPoolUseast1v123sDAS1'); | ||
}); | ||
}); | ||
|
||
describe('#getLambdaS3PermissionLogicalId()', () => { | ||
it('should normalize the function name and add the standard suffix', () => { | ||
expect(sdk.naming.getLambdaS3PermissionLogicalId('functionName', 'bucket')) | ||
|
@@ -463,4 +470,16 @@ describe('#naming()', () => { | |
.to.equal('FunctionNameLambdaPermissionLogsSubscriptionFilterCloudWatchLog0'); | ||
}); | ||
}); | ||
|
||
describe('#getLambdaCognitoUserPoolPermissionLogicalId()', () => { | ||
it('should normalize the function name and add the standard suffix including the normalized ' + | ||
' pool name and trigger source', | ||
() => { | ||
expect(sdk.naming.getLambdaCognitoUserPoolPermissionLogicalId( | ||
'functionName', | ||
'poolId', | ||
'TriggerSource' | ||
)).to.equal('FunctionNameLambdaPermissionCognitoUserPoolTriggerSourcePoolIdTriggerSource'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Quick question: Do you know if there's a limit for the resource logical Id length in CloudFormation? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can't seem to find any information regarding length limits, the only thing I can find is this. |
||
}); | ||
}); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A newline between the end of the
functions
definition and theresources
section would make this easier to digest.