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

Add forceDeploy property in Cognito User Pools #10435

Merged
merged 5 commits into from
Jan 5, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 16 additions & 0 deletions docs/providers/aws/events/cognito-user-pool.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,5 +134,21 @@ resources:
Type: AWS::Cognito::UserPool
```

## Forcing deploying of triggers

A Cognito User Pool with triggers attached may not be correctly updated by AWS Cloudformation on subsequent deployments. To circumvent this issue you can use the `forceDeploy` flag which will try to force Cloudformation to update the triggers no matter what. This flag has to be used in conjuction with the `existing: true` flag.

```yml
functions:
preSignUp:
handler: preSignUp.handler
events:
- cognitoUserPool:
pool: MyUserPool1
trigger: PreSignUp
existing: true
forceDeploy: true
```

[aws-triggers-guide]: http://docs.aws.amazon.com/cognito/latest/developerguide/cognito-user-identity-pools-working-with-aws-lambda-triggers.html
[aws-triggers-list]: https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-cognito-userpool-lambdaconfig.html
1 change: 1 addition & 0 deletions docs/providers/aws/guide/serverless.yml.md
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,7 @@ functions:
pool: MyUserPool
trigger: PreSignUp
existing: true # optional, if you're referencing an existing User Pool
forceDeploy: true # optional, for forcing deployment of triggers on existing User Pools
- alb:
listenerArn: arn:aws:elasticloadbalancing:us-east-1:12345:listener/app/my-load-balancer/50dc6c495c0c9188/
priority: 1
Expand Down
5 changes: 4 additions & 1 deletion lib/plugins/aws/package/compile/events/cognitoUserPool.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class AwsCompileCognitoUserPoolEvents {
pool: { type: 'string', maxLength: 128, pattern: '^[\\w\\s+=,.@-]+$' },
trigger: { enum: validTriggerSources },
existing: { type: 'boolean' },
forceDeploy: { type: 'boolean' },
},
required: ['pool', 'trigger'],
additionalProperties: false,
Expand Down Expand Up @@ -141,7 +142,7 @@ class AwsCompileCognitoUserPoolEvents {
functionObj.events.forEach((event) => {
if (event.cognitoUserPool && event.cognitoUserPool.existing) {
numEventsForFunc++;
const { pool, trigger } = event.cognitoUserPool;
const { pool, trigger, forceDeploy } = event.cognitoUserPool;
usesExistingCognitoUserPool = funcUsesExistingCognitoUserPool = true;

if (!currentPoolName) {
Expand Down Expand Up @@ -171,6 +172,7 @@ class AwsCompileCognitoUserPoolEvents {
}

let customCognitoUserPoolResource;
const forceDeployProperty = forceDeploy ? Date.now() : undefined;
if (numEventsForFunc === 1) {
customCognitoUserPoolResource = {
[customPoolResourceLogicalId]: {
Expand All @@ -188,6 +190,7 @@ class AwsCompileCognitoUserPoolEvents {
Trigger: trigger,
},
],
ForceDeploy: forceDeployProperty,
},
},
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const chai = require('chai');
const proxyquire = require('proxyquire').noCallThru();
const AwsProvider = require('../../../../../../../../lib/plugins/aws/provider');
const Serverless = require('../../../../../../../../lib/Serverless');
const runServerless = require('../../../../../../../utils/run-serverless');

const { expect } = chai;
chai.use(require('sinon-chai'));
Expand Down Expand Up @@ -339,11 +340,41 @@ describe('AwsCompileCognitoUserPoolEvents', () => {
Trigger: 'CustomMessage',
},
],
ForceDeploy: undefined,
},
});
});
});

it('should create the necessary resources for the most minimal configuration with forceDeploy', async () => {
medikoo marked this conversation as resolved.
Show resolved Hide resolved
const result = await runServerless({
fixture: 'cognitoUserPool',
configExt: {
functions: {
existingSimple: {
events: [
{
cognitoUserPool: {
forceDeploy: true,
},
},
],
},
},
},
command: 'package',
});

const { Resources } = result.cfTemplate;
const { awsNaming } = result;

const customResource =
Resources[awsNaming.getCustomResourceCognitoUserPoolResourceLogicalId('existingSimple')];

expect(customResource.Properties.ForceDeploy).to.not.deep.equal(undefined);
expect(customResource.Properties.ForceDeploy).to.be.below(Date.now()).and.to.be.above(0);
medikoo marked this conversation as resolved.
Show resolved Hide resolved
});

it('should create the necessary resources for a service using multiple event definitions', () => {
awsCompileCognitoUserPoolEvents.serverless.service.functions = {
first: {
Expand Down Expand Up @@ -427,6 +458,7 @@ describe('AwsCompileCognitoUserPoolEvents', () => {
Trigger: 'DefineAuthChallenge',
},
],
ForceDeploy: undefined,
},
});
});
Expand Down Expand Up @@ -556,6 +588,7 @@ describe('AwsCompileCognitoUserPoolEvents', () => {
Trigger: 'DefineAuthChallenge',
},
],
ForceDeploy: undefined,
},
});
expect(Resources.SecondCustomCognitoUserPool1).to.deep.equal({
Expand Down Expand Up @@ -583,6 +616,7 @@ describe('AwsCompileCognitoUserPoolEvents', () => {
Trigger: 'PostAuthentication',
},
],
ForceDeploy: undefined,
},
});
});
Expand Down