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

fix(lambda): add a synth-time check for environment variables #1690

Merged
merged 2 commits into from
Feb 8, 2019
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
6 changes: 6 additions & 0 deletions packages/@aws-cdk/aws-lambda/lib/function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,12 @@ export class Function extends FunctionBase {
this.role.addToPolicy(statement);
}

const stack = cdk.Stack.find(this);
const isChina = stack.env.region && stack.env.region.startsWith('cn-');
Copy link
Contributor

Choose a reason for hiding this comment

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

maybe we should check if region is not a token? (cdk.unresolved)

if (isChina && props.environment && Object.keys(props.environment).length > 0) {
throw new Error(`Environment variables are not supported in this region (${stack.env.region}); consider using tags or SSM parameters instead`);
}

const resource = new CfnFunction(this, 'Resource', {
functionName: props.functionName,
description: props.description,
Expand Down
46 changes: 46 additions & 0 deletions packages/@aws-cdk/aws-lambda/test/test.lambda.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1178,6 +1178,52 @@ export = {
test.done();
},

'environment variables are prohibited in China'(test: Test) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Add a test for what happens when the region is undefined and when the region is a token

// GIVEN
const stack = new cdk.Stack(undefined, undefined, { env: { region: 'cn-north-1' }});

// WHEN
test.throws(() => {
new lambda.Function(stack, 'MyLambda', {
code: new lambda.InlineCode('foo'),
handler: 'index.handler',
runtime: lambda.Runtime.NodeJS,
environment: {
SOME: 'Variable'
}
});
}, /Environment variables are not supported/);

test.done();
},

'environment variables work in an unspecified region'(test: Test) {
// GIVEN
const stack = new cdk.Stack();

// WHEN
new lambda.Function(stack, 'MyLambda', {
code: new lambda.InlineCode('foo'),
handler: 'index.handler',
runtime: lambda.Runtime.NodeJS,
environment: {
SOME: 'Variable'
}
});

// THEN
expect(stack).to(haveResource('AWS::Lambda::Function', {
Environment: {
Variables: {
SOME: "Variable"
}
}
}));

test.done();

},

'support reserved concurrent executions'(test: Test) {
const stack = new cdk.Stack();

Expand Down