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

feat(core): pass environment variables to CustomResourceProvider #10560

Merged
merged 2 commits into from
Oct 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,13 @@ export interface CustomResourceProviderProps {
* @default Size.mebibytes(128)
*/
readonly memorySize?: Size;

/**
* Key-value pairs that are passed to Lambda as Environment
*
* @default - No environment variables.
*/
readonly environment?: { [key: string]: string };
}

/**
Expand Down Expand Up @@ -177,11 +184,30 @@ export class CustomResourceProvider extends CoreConstruct {
Handler: `${ENTRYPOINT_FILENAME}.handler`,
Role: role.getAtt('Arn'),
Runtime: 'nodejs12.x',
Environment: this.renderEnvironmentVariables(props.environment),
},
});

handler.addDependsOn(role);

this.serviceToken = Token.asString(handler.getAtt('Arn'));
}

private renderEnvironmentVariables(env?: { [key: string]: string }) {
if (!env || Object.keys(env).length === 0) {
return undefined;
}

// Sort environment so the hash of the function used to create
// `currentVersion` is not affected by key order (this is how lambda does
// it)
const variables: { [key: string]: string } = {};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wait, why is this not simply

return { Variables: env };

?

We're copying from a K/V map into a K/V map. Is there value in the copying?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is done to ensure the hash of lambda asset does not change per deployment based on order of the keys (notice we are sorting the keys in line 198)

I got the reference for this from the Lambda Function implementation

const keys = Object.keys(env).sort();

for (const key of keys) {
variables[key] = env[key];
}

return { Variables: variables };
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -169,4 +169,31 @@ nodeunitShim({
test.deepEqual(lambda.Properties.Timeout, 300);
test.done();
},

'environment variables'(test: Test) {
// GIVEN
const stack = new Stack();

// WHEN
CustomResourceProvider.getOrCreate(stack, 'Custom:MyResourceType', {
codeDirectory: TEST_HANDLER,
runtime: CustomResourceProviderRuntime.NODEJS_12,
environment: {
B: 'b',
A: 'a',
},
});

// THEN
const template = toCloudFormation(stack);
const lambda = template.Resources.CustomMyResourceTypeCustomResourceProviderHandler29FBDD2A;
test.deepEqual(lambda.Properties.Environment, {
Variables: {
A: 'a',
B: 'b',
},
});
test.done();
},
});