diff --git a/packages/amplify-category-function/src/__tests__/provider-utils/awscloudformation/service-walkthroughs/lambda-walkthrough.test.ts b/packages/amplify-category-function/src/__tests__/provider-utils/awscloudformation/service-walkthroughs/lambda-walkthrough.test.ts new file mode 100644 index 00000000000..07d5f8685f3 --- /dev/null +++ b/packages/amplify-category-function/src/__tests__/provider-utils/awscloudformation/service-walkthroughs/lambda-walkthrough.test.ts @@ -0,0 +1,46 @@ +import { topLevelCommentPrefix, topLevelCommentSuffix, envVarPrintoutPrefix } from '../../../../constants'; +import { buildTopLevelComment, buildShowEnvVars } from '../../../../provider-utils/awscloudformation/service-walkthroughs/lambda-walkthrough'; + + + +describe("Lambda Walkthrough : Advanced options and Environment Vars ", () => { + test("buildTopLevelComment should insert all environment variables in top-level-comment (example code header)", () => { + const inputEnvMap : Record = { + "ENV": { + "Ref": "env" + }, + "REGION": { + "Ref": "AWS::Region" + }, + "STORAGE_MOCK_BUCKETNAME": { + "Ref": "storageMockBucketName" + }, + "SES_EMAIL": { + "Ref": "sesEmail" + } + } + const outputString = `${topLevelCommentPrefix}ENV\n\tREGION\n\tSTORAGE_MOCK_BUCKETNAME\n\tSES_EMAIL${topLevelCommentSuffix}` + expect( buildTopLevelComment( inputEnvMap ) ).toEqual(outputString); + + }); + + test("buildShowEnvVars should insert all environment variables to be displayed", () => { + const inputEnvMap : Record = { + "ENV": { + "Ref": "env" + }, + "REGION": { + "Ref": "AWS::Region" + }, + "STORAGE_MOCK_BUCKETNAME": { + "Ref": "storageMockBucketName" + }, + "SES_EMAIL": { + "Ref": "sesEmail" + } + } + const outputString = `${envVarPrintoutPrefix}ENV\n\tREGION\n\tSTORAGE_MOCK_BUCKETNAME\n\tSES_EMAIL`; + expect( buildShowEnvVars( inputEnvMap ) ).toEqual(outputString); + }); + +}); \ No newline at end of file diff --git a/packages/amplify-category-function/src/provider-utils/awscloudformation/service-walkthroughs/lambda-walkthrough.ts b/packages/amplify-category-function/src/provider-utils/awscloudformation/service-walkthroughs/lambda-walkthrough.ts index f2576ad5a47..64935d66c6d 100644 --- a/packages/amplify-category-function/src/provider-utils/awscloudformation/service-walkthroughs/lambda-walkthrough.ts +++ b/packages/amplify-category-function/src/provider-utils/awscloudformation/service-walkthroughs/lambda-walkthrough.ts @@ -3,7 +3,7 @@ import { FunctionParameters, ProjectLayer } from 'amplify-function-plugin-interf import inquirer from 'inquirer'; import _ from 'lodash'; import path from 'path'; -import { categoryName } from '../../../constants'; +import { categoryName, envVarPrintoutPrefix, topLevelCommentPrefix, topLevelCommentSuffix } from '../../../constants'; import { getNewCFNEnvVariables, getNewCFNParameters } from '../utils/cloudformationHelpers'; import { advancedSettingsList, @@ -88,6 +88,11 @@ export async function createWalkthrough( // ask environment variable questions and merge in results if (await context.amplify.confirmPrompt('Do you want to configure environment variables for this function?', false)) { templateParameters = merge(templateParameters, await askEnvironmentVariableQuestions(templateParameters.functionName)); + //update top-level comment to be inserted into example + templateParameters.topLevelComment = buildTopLevelComment( templateParameters.environmentMap ); + //show updated environment variables + const envVarViewString = buildShowEnvVars(templateParameters.environmentMap); + context.print.info(envVarViewString); } // ask function secrets questions and merge in results @@ -103,6 +108,24 @@ export async function createWalkthrough( return templateParameters; } +//Function to build message to be displayed when +//Lambda environment variables are enabled. +export function buildShowEnvVars(envVariableMap){ + const envVarViewArr = Object.keys(envVariableMap); //sorted list of existing env variables + const envVarViewString= envVarViewArr.join("\n\t"); + const showEnvComment = `${envVarPrintoutPrefix}${envVarViewString}`; + return showEnvComment; +} + +//Insert 'Environment Map' keys - (env, region, resource-names, user supplied variables) +//into topLevelComment to be inserted into example files and displayed to the user +export function buildTopLevelComment( envVariableMap ){ + const envVarViewArr = Object.keys(envVariableMap); + const envVarViewString= envVarViewArr.join("\n\t"); + const topLevelComment = `${topLevelCommentPrefix}${envVarViewString}${topLevelCommentSuffix}`; + return topLevelComment; +} + function provideInformation(context, lambdaToUpdate, functionRuntime, currentParameters, scheduleParameters) { // Provide general information context.print.success('General information'); @@ -279,6 +302,8 @@ export async function updateWalkthrough(context: $TSContext, lambdaToUpdate?: st functionParameters, await askEnvironmentVariableQuestions(lambdaToUpdate, undefined, !selectedSettings.includes(environmentVariableSetting)), ); + //update top-level comment to be inserted into example + functionParameters.topLevelComment = buildTopLevelComment( functionParameters.environmentMap ); return functionParameters; } diff --git a/packages/amplify-nodejs-function-template-provider/resources/lambda/trigger/trigger-dynamodb.js b/packages/amplify-nodejs-function-template-provider/resources/lambda/trigger/trigger-dynamodb.js.ejs similarity index 91% rename from packages/amplify-nodejs-function-template-provider/resources/lambda/trigger/trigger-dynamodb.js rename to packages/amplify-nodejs-function-template-provider/resources/lambda/trigger/trigger-dynamodb.js.ejs index 59f9abc26cd..76833647140 100644 --- a/packages/amplify-nodejs-function-template-provider/resources/lambda/trigger/trigger-dynamodb.js +++ b/packages/amplify-nodejs-function-template-provider/resources/lambda/trigger/trigger-dynamodb.js.ejs @@ -1,3 +1,5 @@ +<%= props.topLevelComment %> + exports.handler = event => { //eslint-disable-line console.log(JSON.stringify(event, null, 2)); diff --git a/packages/amplify-nodejs-function-template-provider/resources/lambda/trigger/trigger-kinesis.js b/packages/amplify-nodejs-function-template-provider/resources/lambda/trigger/trigger-kinesis.js.ejs similarity index 94% rename from packages/amplify-nodejs-function-template-provider/resources/lambda/trigger/trigger-kinesis.js rename to packages/amplify-nodejs-function-template-provider/resources/lambda/trigger/trigger-kinesis.js.ejs index e7fd7d264b0..47b33a210d5 100644 --- a/packages/amplify-nodejs-function-template-provider/resources/lambda/trigger/trigger-kinesis.js +++ b/packages/amplify-nodejs-function-template-provider/resources/lambda/trigger/trigger-kinesis.js.ejs @@ -1,3 +1,5 @@ +<%= props.topLevelComment %> + exports.handler = event => { // insert code to be executed by your lambda trigger console.log(JSON.stringify(event, null, 2)); diff --git a/packages/amplify-nodejs-function-template-provider/src/utils/analyticsWalkthrough.ts b/packages/amplify-nodejs-function-template-provider/src/utils/analyticsWalkthrough.ts index 1fbc7c5ac1d..4c5248201cc 100644 --- a/packages/amplify-nodejs-function-template-provider/src/utils/analyticsWalkthrough.ts +++ b/packages/amplify-nodejs-function-template-provider/src/utils/analyticsWalkthrough.ts @@ -37,7 +37,7 @@ export async function askAnalyticsCategoryKinesisQuestions(context: any) { batchSize: 100, startingPosition: 'LATEST', eventSourceArn: streamArnParamRef, - functionTemplateName: 'trigger-kinesis.js', + functionTemplateName: 'trigger-kinesis.js.ejs', triggerPolicies: [ { Effect: 'Allow', diff --git a/packages/amplify-nodejs-function-template-provider/src/utils/dynamoDBWalkthrough.ts b/packages/amplify-nodejs-function-template-provider/src/utils/dynamoDBWalkthrough.ts index 65a6ea3da0a..36ee4cbece6 100644 --- a/packages/amplify-nodejs-function-template-provider/src/utils/dynamoDBWalkthrough.ts +++ b/packages/amplify-nodejs-function-template-provider/src/utils/dynamoDBWalkthrough.ts @@ -164,7 +164,7 @@ export async function askAPICategoryDynamoDBQuestions(context: any) { batchSize: 100, startingPosition: 'LATEST', eventSourceArn: streamArnParamRef, - functionTemplateName: 'trigger-dynamodb.js', + functionTemplateName: 'trigger-dynamodb.js.ejs', triggerPolicies: [ { Effect: 'Allow', diff --git a/packages/amplify-nodejs-function-template-provider/src/utils/eventSourceWalkthrough.ts b/packages/amplify-nodejs-function-template-provider/src/utils/eventSourceWalkthrough.ts index 81c347125c7..e72b62a7c7d 100644 --- a/packages/amplify-nodejs-function-template-provider/src/utils/eventSourceWalkthrough.ts +++ b/packages/amplify-nodejs-function-template-provider/src/utils/eventSourceWalkthrough.ts @@ -69,7 +69,7 @@ export async function askEventSourceQuestions(context: any) { batchSize: 100, startingPosition: 'LATEST', eventSourceArn, - functionTemplateName: 'trigger-kinesis.js', + functionTemplateName: 'trigger-kinesis.js.ejs', triggerPolicies: [ { Effect: 'Allow', @@ -135,7 +135,7 @@ export async function askEventSourceQuestions(context: any) { batchSize: 100, startingPosition: 'LATEST', eventSourceArn, - functionTemplateName: 'trigger-dynamodb.js', + functionTemplateName: 'trigger-dynamodb.js.ejs', triggerPolicies: [ { Effect: 'Allow', @@ -168,7 +168,7 @@ export async function askEventSourceQuestions(context: any) { batchSize: 100, startingPosition: 'LATEST', eventSourceArn: dynamoDBCategoryStorageStreamArnRef, - functionTemplateName: 'trigger-dynamodb.js', + functionTemplateName: 'trigger-dynamodb.js.ejs', triggerPolicies: [ { Effect: 'Allow',