diff --git a/packages/amplify-category-auth/provider-utils/awscloudformation/triggers/CreateAuthChallenge/boilerplate-create-challenge.js b/packages/amplify-category-auth/provider-utils/awscloudformation/triggers/CreateAuthChallenge/boilerplate-create-challenge.js index 42145a4faa6..2c48a0f6d41 100644 --- a/packages/amplify-category-auth/provider-utils/awscloudformation/triggers/CreateAuthChallenge/boilerplate-create-challenge.js +++ b/packages/amplify-category-auth/provider-utils/awscloudformation/triggers/CreateAuthChallenge/boilerplate-create-challenge.js @@ -1,12 +1,12 @@ /* tslint:disable */ /* eslint-disable */ -exports.handler = (event, context) => { +exports.handler = async event => { if (event.request.session.length === 2 && event.request.challengeName === 'CUSTOM_CHALLENGE') { event.response.publicChallengeParameters = { trigger: 'true' }; event.response.privateChallengeParameters = {}; event.response.privateChallengeParameters.answer = process.env.CHALLENGEANSWER; } - context.done(null, event); + return event; }; diff --git a/packages/amplify-category-auth/provider-utils/awscloudformation/triggers/CreateAuthChallenge/captcha-create-challenge.js b/packages/amplify-category-auth/provider-utils/awscloudformation/triggers/CreateAuthChallenge/captcha-create-challenge.js index 65c0ee3a404..98a5231b0bc 100644 --- a/packages/amplify-category-auth/provider-utils/awscloudformation/triggers/CreateAuthChallenge/captcha-create-challenge.js +++ b/packages/amplify-category-auth/provider-utils/awscloudformation/triggers/CreateAuthChallenge/captcha-create-challenge.js @@ -1,11 +1,11 @@ /* tslint:disable */ /* eslint-disable */ -exports.handler = (event, context) => { +exports.handler = async event => { if (event.request.session.length === 2 && event.request.challengeName === 'CUSTOM_CHALLENGE') { event.response.publicChallengeParameters = { trigger: 'true' }; event.response.privateChallengeParameters = { answer: '' }; event.response.challengeMetadata = 'CAPTCHA_CHALLENGE'; } - context.done(null, event); + return event; }; diff --git a/packages/amplify-category-auth/provider-utils/awscloudformation/triggers/CreateAuthChallenge/function-template-dir/trigger-index.js b/packages/amplify-category-auth/provider-utils/awscloudformation/triggers/CreateAuthChallenge/function-template-dir/trigger-index.js deleted file mode 100644 index 80c77ea4e38..00000000000 --- a/packages/amplify-category-auth/provider-utils/awscloudformation/triggers/CreateAuthChallenge/function-template-dir/trigger-index.js +++ /dev/null @@ -1,14 +0,0 @@ -/* - this file will loop through all js modules which are uploaded to the lambda resource, - provided that the file names (without extension) are included in the "MODULES" env variable. - "MODULES" is a comma-delimmited string. -*/ -const moduleNames = process.env.MODULES.split(','); -const modules = moduleNames.map(name => require(`./${name}`)); - -exports.handler = (event, context, callback) => { - for (let i = 0; i < modules.length; i += 1) { - const { handler } = modules[i]; - handler(event, context, callback); - } -}; diff --git a/packages/amplify-category-auth/provider-utils/awscloudformation/triggers/CustomMessage/function-template-dir/trigger-index.js b/packages/amplify-category-auth/provider-utils/awscloudformation/triggers/CustomMessage/function-template-dir/trigger-index.js deleted file mode 100644 index 80c77ea4e38..00000000000 --- a/packages/amplify-category-auth/provider-utils/awscloudformation/triggers/CustomMessage/function-template-dir/trigger-index.js +++ /dev/null @@ -1,14 +0,0 @@ -/* - this file will loop through all js modules which are uploaded to the lambda resource, - provided that the file names (without extension) are included in the "MODULES" env variable. - "MODULES" is a comma-delimmited string. -*/ -const moduleNames = process.env.MODULES.split(','); -const modules = moduleNames.map(name => require(`./${name}`)); - -exports.handler = (event, context, callback) => { - for (let i = 0; i < modules.length; i += 1) { - const { handler } = modules[i]; - handler(event, context, callback); - } -}; diff --git a/packages/amplify-category-auth/provider-utils/awscloudformation/triggers/CustomMessage/verification-link.js b/packages/amplify-category-auth/provider-utils/awscloudformation/triggers/CustomMessage/verification-link.js index eb81ccc0e72..2a1b41765a8 100644 --- a/packages/amplify-category-auth/provider-utils/awscloudformation/triggers/CustomMessage/verification-link.js +++ b/packages/amplify-category-auth/provider-utils/awscloudformation/triggers/CustomMessage/verification-link.js @@ -1,4 +1,4 @@ -exports.handler = (event, context, callback) => { +exports.handler = async event => { // Define the URL that you want the user to be directed to after verification is complete if (event.triggerSource === 'CustomMessage_SignUp') { const { codeParameter } = event.request; @@ -26,7 +26,7 @@ exports.handler = (event, context, callback) => { redirectUrl, region, clientId, - }) + }), ).toString('base64'); const bucketUrl = `http://${resourcePrefix}verificationbucket-${process.env.ENV}.s3-website${seperator}${region}.amazonaws.com`; const url = `${bucketUrl}/?data=${payload}&code=${codeParameter}`; @@ -35,8 +35,7 @@ exports.handler = (event, context, callback) => { event.response.emailSubject = process.env.EMAILSUBJECT; event.response.emailMessage = message; console.log('event.response', event.response); - callback(null, event); - } else { - callback(null, event); } + + return event; }; diff --git a/packages/amplify-category-auth/provider-utils/awscloudformation/triggers/DefineAuthChallenge/boilerplate-define-challenge.js b/packages/amplify-category-auth/provider-utils/awscloudformation/triggers/DefineAuthChallenge/boilerplate-define-challenge.js index 9acf23986f8..77ed08c72c4 100644 --- a/packages/amplify-category-auth/provider-utils/awscloudformation/triggers/DefineAuthChallenge/boilerplate-define-challenge.js +++ b/packages/amplify-category-auth/provider-utils/awscloudformation/triggers/DefineAuthChallenge/boilerplate-define-challenge.js @@ -1,4 +1,4 @@ -exports.handler = (event, context) => { +exports.handler = async event => { if (event.request.session.length === 1 && event.request.session[0].challengeName === 'SRP_A') { event.response.issueTokens = false; event.response.failAuthentication = false; @@ -22,5 +22,6 @@ exports.handler = (event, context) => { event.response.issueTokens = false; event.response.failAuthentication = true; } - context.done(null, event); + + return event; }; diff --git a/packages/amplify-category-auth/provider-utils/awscloudformation/triggers/DefineAuthChallenge/captcha-define-challenge.js b/packages/amplify-category-auth/provider-utils/awscloudformation/triggers/DefineAuthChallenge/captcha-define-challenge.js index 9acf23986f8..77ed08c72c4 100644 --- a/packages/amplify-category-auth/provider-utils/awscloudformation/triggers/DefineAuthChallenge/captcha-define-challenge.js +++ b/packages/amplify-category-auth/provider-utils/awscloudformation/triggers/DefineAuthChallenge/captcha-define-challenge.js @@ -1,4 +1,4 @@ -exports.handler = (event, context) => { +exports.handler = async event => { if (event.request.session.length === 1 && event.request.session[0].challengeName === 'SRP_A') { event.response.issueTokens = false; event.response.failAuthentication = false; @@ -22,5 +22,6 @@ exports.handler = (event, context) => { event.response.issueTokens = false; event.response.failAuthentication = true; } - context.done(null, event); + + return event; }; diff --git a/packages/amplify-category-auth/provider-utils/awscloudformation/triggers/DefineAuthChallenge/function-template-dir/trigger-index.js b/packages/amplify-category-auth/provider-utils/awscloudformation/triggers/DefineAuthChallenge/function-template-dir/trigger-index.js deleted file mode 100644 index 80c77ea4e38..00000000000 --- a/packages/amplify-category-auth/provider-utils/awscloudformation/triggers/DefineAuthChallenge/function-template-dir/trigger-index.js +++ /dev/null @@ -1,14 +0,0 @@ -/* - this file will loop through all js modules which are uploaded to the lambda resource, - provided that the file names (without extension) are included in the "MODULES" env variable. - "MODULES" is a comma-delimmited string. -*/ -const moduleNames = process.env.MODULES.split(','); -const modules = moduleNames.map(name => require(`./${name}`)); - -exports.handler = (event, context, callback) => { - for (let i = 0; i < modules.length; i += 1) { - const { handler } = modules[i]; - handler(event, context, callback); - } -}; diff --git a/packages/amplify-category-auth/provider-utils/awscloudformation/triggers/PostAuthentication/function-template-dir/trigger-index.js b/packages/amplify-category-auth/provider-utils/awscloudformation/triggers/PostAuthentication/function-template-dir/trigger-index.js deleted file mode 100644 index 80c77ea4e38..00000000000 --- a/packages/amplify-category-auth/provider-utils/awscloudformation/triggers/PostAuthentication/function-template-dir/trigger-index.js +++ /dev/null @@ -1,14 +0,0 @@ -/* - this file will loop through all js modules which are uploaded to the lambda resource, - provided that the file names (without extension) are included in the "MODULES" env variable. - "MODULES" is a comma-delimmited string. -*/ -const moduleNames = process.env.MODULES.split(','); -const modules = moduleNames.map(name => require(`./${name}`)); - -exports.handler = (event, context, callback) => { - for (let i = 0; i < modules.length; i += 1) { - const { handler } = modules[i]; - handler(event, context, callback); - } -}; diff --git a/packages/amplify-category-auth/provider-utils/awscloudformation/triggers/PostConfirmation/add-to-group.js b/packages/amplify-category-auth/provider-utils/awscloudformation/triggers/PostConfirmation/add-to-group.js index c33dca12fc1..b3cd62cd3a8 100644 --- a/packages/amplify-category-auth/provider-utils/awscloudformation/triggers/PostConfirmation/add-to-group.js +++ b/packages/amplify-category-auth/provider-utils/awscloudformation/triggers/PostConfirmation/add-to-group.js @@ -1,34 +1,31 @@ /* eslint-disable-line */ const aws = require('aws-sdk'); -exports.handler = async (event, context) => { - const cognitoidentityserviceprovider = new aws.CognitoIdentityServiceProvider({ apiVersion: '2016-04-18' }); +const cognitoidentityserviceprovider = new aws.CognitoIdentityServiceProvider({ + apiVersion: '2016-04-18', +}); + +exports.handler = async event => { const groupParams = { GroupName: process.env.GROUP, UserPoolId: event.userPoolId, }; - const addUserParams = { GroupName: process.env.GROUP, UserPoolId: event.userPoolId, Username: event.userName, }; - + /** + * Check if the group exists; if it doesn't, create it. + */ try { await cognitoidentityserviceprovider.getGroup(groupParams).promise(); } catch (e) { await cognitoidentityserviceprovider.createGroup(groupParams).promise(); } + /** + * Then, add the user to the group. + */ + await cognitoidentityserviceprovider.adminAddUserToGroup(addUserParams).promise(); - try { - await cognitoidentityserviceprovider.adminAddUserToGroup(addUserParams).promise(); - return { - statusCode: 200, - body: JSON.stringify(event) - } - } catch (error) { - return { - statusCode: 500, - body: JSON.stringify(error) - } - } + return event; }; diff --git a/packages/amplify-category-auth/provider-utils/awscloudformation/triggers/PostConfirmation/function-template-dir/trigger-index.js b/packages/amplify-category-auth/provider-utils/awscloudformation/triggers/PostConfirmation/function-template-dir/trigger-index.js deleted file mode 100644 index 80c77ea4e38..00000000000 --- a/packages/amplify-category-auth/provider-utils/awscloudformation/triggers/PostConfirmation/function-template-dir/trigger-index.js +++ /dev/null @@ -1,14 +0,0 @@ -/* - this file will loop through all js modules which are uploaded to the lambda resource, - provided that the file names (without extension) are included in the "MODULES" env variable. - "MODULES" is a comma-delimmited string. -*/ -const moduleNames = process.env.MODULES.split(','); -const modules = moduleNames.map(name => require(`./${name}`)); - -exports.handler = (event, context, callback) => { - for (let i = 0; i < modules.length; i += 1) { - const { handler } = modules[i]; - handler(event, context, callback); - } -}; diff --git a/packages/amplify-category-auth/provider-utils/awscloudformation/triggers/PreAuthentication/function-template-dir/trigger-index.js b/packages/amplify-category-auth/provider-utils/awscloudformation/triggers/PreAuthentication/function-template-dir/trigger-index.js deleted file mode 100644 index 80c77ea4e38..00000000000 --- a/packages/amplify-category-auth/provider-utils/awscloudformation/triggers/PreAuthentication/function-template-dir/trigger-index.js +++ /dev/null @@ -1,14 +0,0 @@ -/* - this file will loop through all js modules which are uploaded to the lambda resource, - provided that the file names (without extension) are included in the "MODULES" env variable. - "MODULES" is a comma-delimmited string. -*/ -const moduleNames = process.env.MODULES.split(','); -const modules = moduleNames.map(name => require(`./${name}`)); - -exports.handler = (event, context, callback) => { - for (let i = 0; i < modules.length; i += 1) { - const { handler } = modules[i]; - handler(event, context, callback); - } -}; diff --git a/packages/amplify-category-auth/provider-utils/awscloudformation/triggers/PreSignup/email-filter-allowlist-legacy.js b/packages/amplify-category-auth/provider-utils/awscloudformation/triggers/PreSignup/email-filter-allowlist-legacy.js index dc937d2d9c9..1e2873aeffa 100644 --- a/packages/amplify-category-auth/provider-utils/awscloudformation/triggers/PreSignup/email-filter-allowlist-legacy.js +++ b/packages/amplify-category-auth/provider-utils/awscloudformation/triggers/PreSignup/email-filter-allowlist-legacy.js @@ -1,4 +1,4 @@ -exports.handler = (event, context, callback) => { +exports.handler = async event => { // allowed domains const ald = process.env.DOMAINWHITELIST.split(',').map(d => d.trim()); @@ -6,8 +6,8 @@ exports.handler = (event, context, callback) => { const domain = email.substring(email.indexOf('@') + 1); if (!ald.includes(domain)) { - callback(new Error(`Invalid email domain: ${domain}`), event); - } else { - callback(null, event); + throw new Error(`Invalid email domain: ${domain}`); } + + return event; }; diff --git a/packages/amplify-category-auth/provider-utils/awscloudformation/triggers/PreSignup/email-filter-allowlist.js b/packages/amplify-category-auth/provider-utils/awscloudformation/triggers/PreSignup/email-filter-allowlist.js index 0f5eb4ad7a8..dc5b9e7b882 100644 --- a/packages/amplify-category-auth/provider-utils/awscloudformation/triggers/PreSignup/email-filter-allowlist.js +++ b/packages/amplify-category-auth/provider-utils/awscloudformation/triggers/PreSignup/email-filter-allowlist.js @@ -1,4 +1,4 @@ -exports.handler = (event, context, callback) => { +exports.handler = async event => { // allowed domains const ald = process.env.DOMAINALLOWLIST.split(',').map(d => d.trim()); @@ -6,8 +6,8 @@ exports.handler = (event, context, callback) => { const domain = email.substring(email.indexOf('@') + 1); if (!ald.includes(domain)) { - callback(new Error(`Invalid email domain: ${domain}`), event); - } else { - callback(null, event); + throw new Error(`Invalid email domain: ${domain}`); } + + return event; }; diff --git a/packages/amplify-category-auth/provider-utils/awscloudformation/triggers/PreSignup/email-filter-denylist-legacy.js b/packages/amplify-category-auth/provider-utils/awscloudformation/triggers/PreSignup/email-filter-denylist-legacy.js index ee976c5ebc5..fea04763e6b 100644 --- a/packages/amplify-category-auth/provider-utils/awscloudformation/triggers/PreSignup/email-filter-denylist-legacy.js +++ b/packages/amplify-category-auth/provider-utils/awscloudformation/triggers/PreSignup/email-filter-denylist-legacy.js @@ -1,4 +1,4 @@ -exports.handler = (event, context, callback) => { +exports.handler = async event => { // disallowed domains const dld = process.env.DOMAINBLACKLIST.split(',').map(d => d.trim()); @@ -6,8 +6,8 @@ exports.handler = (event, context, callback) => { const domain = email.substring(email.indexOf('@') + 1); if (dld.includes(domain)) { - callback(new Error(`Invalid email domain: ${domain}`), event); - } else { - callback(null, event); + throw new Error(`Invalid email domain: ${domain}`); } + + return event; }; diff --git a/packages/amplify-category-auth/provider-utils/awscloudformation/triggers/PreSignup/email-filter-denylist.js b/packages/amplify-category-auth/provider-utils/awscloudformation/triggers/PreSignup/email-filter-denylist.js index 034e2e74e3f..49fe4248f84 100644 --- a/packages/amplify-category-auth/provider-utils/awscloudformation/triggers/PreSignup/email-filter-denylist.js +++ b/packages/amplify-category-auth/provider-utils/awscloudformation/triggers/PreSignup/email-filter-denylist.js @@ -1,4 +1,4 @@ -exports.handler = (event, context, callback) => { +exports.handler = async event => { // disallowed domains const dld = process.env.DOMAINDENYLIST.split(',').map(d => d.trim()); @@ -6,8 +6,8 @@ exports.handler = (event, context, callback) => { const domain = email.substring(email.indexOf('@') + 1); if (dld.includes(domain)) { - callback(new Error(`Invalid email domain: ${domain}`), event); - } else { - callback(null, event); + throw new Error(`Invalid email domain: ${domain}`); } + + return event; }; diff --git a/packages/amplify-category-auth/provider-utils/awscloudformation/triggers/PreSignup/function-template-dir/trigger-index.js b/packages/amplify-category-auth/provider-utils/awscloudformation/triggers/PreSignup/function-template-dir/trigger-index.js deleted file mode 100644 index 80c77ea4e38..00000000000 --- a/packages/amplify-category-auth/provider-utils/awscloudformation/triggers/PreSignup/function-template-dir/trigger-index.js +++ /dev/null @@ -1,14 +0,0 @@ -/* - this file will loop through all js modules which are uploaded to the lambda resource, - provided that the file names (without extension) are included in the "MODULES" env variable. - "MODULES" is a comma-delimmited string. -*/ -const moduleNames = process.env.MODULES.split(','); -const modules = moduleNames.map(name => require(`./${name}`)); - -exports.handler = (event, context, callback) => { - for (let i = 0; i < modules.length; i += 1) { - const { handler } = modules[i]; - handler(event, context, callback); - } -}; diff --git a/packages/amplify-category-auth/provider-utils/awscloudformation/triggers/PreTokenGeneration/alter-claims.js b/packages/amplify-category-auth/provider-utils/awscloudformation/triggers/PreTokenGeneration/alter-claims.js index 28427fd51b4..9f9d0f4ce4f 100644 --- a/packages/amplify-category-auth/provider-utils/awscloudformation/triggers/PreTokenGeneration/alter-claims.js +++ b/packages/amplify-category-auth/provider-utils/awscloudformation/triggers/PreTokenGeneration/alter-claims.js @@ -1,4 +1,4 @@ -exports.handler = async (event, context, callback) => { +exports.handler = async event => { event.response = { claimsOverrideDetails: { claimsToAddOrOverride: { @@ -9,5 +9,5 @@ exports.handler = async (event, context, callback) => { }, }; // Return to Amazon Cognito - callback(null, event); + return event; }; diff --git a/packages/amplify-category-auth/provider-utils/awscloudformation/triggers/PreTokenGeneration/function-template-dir/trigger-index.js b/packages/amplify-category-auth/provider-utils/awscloudformation/triggers/PreTokenGeneration/function-template-dir/trigger-index.js deleted file mode 100644 index 80c77ea4e38..00000000000 --- a/packages/amplify-category-auth/provider-utils/awscloudformation/triggers/PreTokenGeneration/function-template-dir/trigger-index.js +++ /dev/null @@ -1,14 +0,0 @@ -/* - this file will loop through all js modules which are uploaded to the lambda resource, - provided that the file names (without extension) are included in the "MODULES" env variable. - "MODULES" is a comma-delimmited string. -*/ -const moduleNames = process.env.MODULES.split(','); -const modules = moduleNames.map(name => require(`./${name}`)); - -exports.handler = (event, context, callback) => { - for (let i = 0; i < modules.length; i += 1) { - const { handler } = modules[i]; - handler(event, context, callback); - } -}; diff --git a/packages/amplify-category-auth/provider-utils/awscloudformation/triggers/VerifyAuthChallengeResponse/boilerplate-verify.js b/packages/amplify-category-auth/provider-utils/awscloudformation/triggers/VerifyAuthChallengeResponse/boilerplate-verify.js index 000a95ad10c..3134d85336f 100644 --- a/packages/amplify-category-auth/provider-utils/awscloudformation/triggers/VerifyAuthChallengeResponse/boilerplate-verify.js +++ b/packages/amplify-category-auth/provider-utils/awscloudformation/triggers/VerifyAuthChallengeResponse/boilerplate-verify.js @@ -1,8 +1,9 @@ -exports.handler = (event, context) => { +exports.handler = async event => { if (event.request.privateChallengeParameters.answer === event.request.challengeAnswer) { event.response.answerCorrect = true; } else { event.response.answerCorrect = false; } - context.done(null, event); + + return event; }; diff --git a/packages/amplify-category-auth/provider-utils/awscloudformation/triggers/VerifyAuthChallengeResponse/captcha-verify.js b/packages/amplify-category-auth/provider-utils/awscloudformation/triggers/VerifyAuthChallengeResponse/captcha-verify.js index 06cb4601928..9ad349e0db7 100644 --- a/packages/amplify-category-auth/provider-utils/awscloudformation/triggers/VerifyAuthChallengeResponse/captcha-verify.js +++ b/packages/amplify-category-auth/provider-utils/awscloudformation/triggers/VerifyAuthChallengeResponse/captcha-verify.js @@ -2,23 +2,23 @@ const axios = require('axios'); /* eslint-enable */ -exports.handler = (event, context, callback) => { - axios - .post( - `https://www.google.com/recaptcha/api/siteverify?secret=${process.env.RECAPTCHASECRET}&response=${event.request.challengeAnswer}`, - {} - ) - .then(response => { - if (response && response.data && response.data.success) { - event.response.answerCorrect = true; - callback(null, event); - } else { - event.response.answerCorrect = false; - callback(new Error('captcha verification error'), event); - } - }) - .catch(() => { - event.response.answerCorrect = false; - callback(new Error('captcha verification error'), event); - }); +exports.handler = async event => { + const response = await axios.post( + `https://www.google.com/recaptcha/api/siteverify?secret=${process.env.RECAPTCHASECRET}&response=${event.request.challengeAnswer}`, + {}, + ); + /** + * Verify that the CAPTCHA challenge succeeded, and if it did, indicate so in + * the event response. + * + * If the challenge fails, throw an error. + */ + const challengeSucceeded = response && response.data && response.data.success; + event.response.answerCorrect = !!challengeSucceeded; + + if (!challengeSucceeded) { + throw new Error('CAPTCHA verification error'); + } + + return event; }; diff --git a/packages/amplify-category-auth/provider-utils/awscloudformation/triggers/VerifyAuthChallengeResponse/function-template-dir/trigger-index.js b/packages/amplify-category-auth/provider-utils/awscloudformation/triggers/VerifyAuthChallengeResponse/function-template-dir/trigger-index.js deleted file mode 100644 index 80c77ea4e38..00000000000 --- a/packages/amplify-category-auth/provider-utils/awscloudformation/triggers/VerifyAuthChallengeResponse/function-template-dir/trigger-index.js +++ /dev/null @@ -1,14 +0,0 @@ -/* - this file will loop through all js modules which are uploaded to the lambda resource, - provided that the file names (without extension) are included in the "MODULES" env variable. - "MODULES" is a comma-delimmited string. -*/ -const moduleNames = process.env.MODULES.split(','); -const modules = moduleNames.map(name => require(`./${name}`)); - -exports.handler = (event, context, callback) => { - for (let i = 0; i < modules.length; i += 1) { - const { handler } = modules[i]; - handler(event, context, callback); - } -}; diff --git a/packages/amplify-category-auth/provider-utils/awscloudformation/triggers/function-template-dir/trigger-index.js b/packages/amplify-category-auth/provider-utils/awscloudformation/triggers/function-template-dir/trigger-index.js new file mode 100644 index 00000000000..f9854bb863a --- /dev/null +++ b/packages/amplify-category-auth/provider-utils/awscloudformation/triggers/function-template-dir/trigger-index.js @@ -0,0 +1,39 @@ +/** + * @fileoverview + * + * This CloudFormation Trigger creates a handler which awaits the other handlers + * specified in the `MODULES` env var, located at `./${MODULE}`. + */ + +/** + * The names of modules to load are stored as a comma-delimited string in the + * `MODULES` env var. + */ +const moduleNames = process.env.MODULES.split(','); +/** + * The array of imported modules. + */ +const modules = moduleNames.map(name => require(`./${name}`)); + +/** + * This async handler iterates over the given modules and awaits them. + * + * @see https://docs.aws.amazon.com/lambda/latest/dg/nodejs-handler.html#nodejs-handler-async + * + * @param {object} event + * + * The event that triggered this Lambda. + * + * @returns + * + * The handler response. + */ +exports.handler = async event => { + /** + * Instead of naively iterating over all handlers, run them concurrently with + * `await Promise.all(...)`. This would otherwise just be determined by the + * order of names in the `MODULES` var. + */ + await Promise.all(modules.map(module => module.handler(event))); + return event; +}; diff --git a/packages/amplify-cli/src/extensions/amplify-helpers/trigger-flow.ts b/packages/amplify-cli/src/extensions/amplify-helpers/trigger-flow.ts index cc215b81927..519122431f0 100644 --- a/packages/amplify-cli/src/extensions/amplify-helpers/trigger-flow.ts +++ b/packages/amplify-cli/src/extensions/amplify-helpers/trigger-flow.ts @@ -45,6 +45,49 @@ export const addTrigger = async triggerOptions => { triggerEventPath, skipEdit, } = triggerOptions; + /** + * The function template dir for this trigger, i.e. + * PostConfirmation/function-template-dir. + */ + const sourceRoot = path.join(triggerDir, 'function-template-dir'); + /** + * The function template dir for the default trigger template. + */ + const defaultRoot = path.resolve(triggerDir, '..', 'function-template-dir'); + /** + * A key-value map of input filenames and output filepaths. + */ + const templateMap = { + 'trigger-index.js': path.join('src', 'index.js'), + 'package.json.ejs': path.join('src', 'package.json'), + 'event.json': path.join('src', 'event.json'), + }; + /** + * Hold a key-value map of input filepaths and output filenames. + */ + const destMap = {}; + /** + * For each templateFile (i.e. trigger-index.js), use + * {triggerDir}/../{templateFile} if {triggerDir}/{templateFile} + * override does not exist. + */ + const templateFiles = Object.keys(templateMap); + const sourceFiles = templateFiles.map(file => { + const defaultTemplate = path.resolve(defaultRoot, file); + const overrideTemplate = path.resolve(sourceRoot, file); + const templateToUse = fs.existsSync(overrideTemplate) ? overrideTemplate : defaultTemplate; + return path.relative(sourceRoot, templateToUse); + }); + /** + * Map relative template pathnames to their new filenames. + */ + for (const sourceFile of sourceFiles) { + /** + * /path/to/file.js -> file.js + */ + const fileName = path.basename(sourceFile); + destMap[sourceFile] = templateMap[fileName]; + } await context.amplify.invokePluginMethod(context, 'function', undefined, 'add', [ context, @@ -54,13 +97,9 @@ export const addTrigger = async triggerOptions => { trigger: true, cloudResourceTemplatePath: path.join(triggerDir, 'cloudformation-templates', triggerTemplate), functionTemplate: { - sourceRoot: path.join(triggerDir, 'function-template-dir'), - sourceFiles: ['trigger-index.js', 'package.json.ejs', 'event.json'], - destMap: { - 'trigger-index.js': path.join('src', 'index.js'), - 'package.json.ejs': path.join('src', 'package.json'), - 'event.json': path.join('src', 'event.json'), - }, + sourceRoot, + sourceFiles, + destMap, }, modules: values, parentResource,