Skip to content

Commit

Permalink
fix(amplify-category-function): Storage env vars not added to lambda …
Browse files Browse the repository at this point in the history
…function (aws-amplify#7785)

* fix(7718) Storage env vars not added to lambda function

* unit-tests

Co-authored-by: Sachin Panemangalore <sachinrp@amazon.com>
  • Loading branch information
2 people authored and marcvberg committed Oct 13, 2021
1 parent 9434009 commit d2b8bb7
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -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<string, any> = {
"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<string, any> = {
"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);
});

});
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand All @@ -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');
Expand Down Expand Up @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
<%= props.topLevelComment %>

exports.handler = event => {
//eslint-disable-line
console.log(JSON.stringify(event, null, 2));
Expand Down
Original file line number Diff line number Diff line change
@@ -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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -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',
Expand Down

0 comments on commit d2b8bb7

Please sign in to comment.