Skip to content

Commit

Permalink
feat(AWS Cognito): Support forceDeploy setting (#10435)
Browse files Browse the repository at this point in the history
  • Loading branch information
TsimpDim committed Jan 5, 2022
1 parent 9e1fe0a commit c67a3f1
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 1 deletion.
16 changes: 16 additions & 0 deletions docs/providers/aws/events/cognito-user-pool.md
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
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
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
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,40 @@ describe('AwsCompileCognitoUserPoolEvents', () => {
Trigger: 'CustomMessage',
},
],
ForceDeploy: undefined,
},
});
});
});

it('should support `forceDeploy` setting', async () => {
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(typeof customResource.Properties.ForceDeploy).to.equal('number');
});

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

0 comments on commit c67a3f1

Please sign in to comment.