Skip to content

Commit

Permalink
fix(lambda): add region check for environment variables (#1690)
Browse files Browse the repository at this point in the history
In some regions Lambda environment variables don't work. Add a check
for that so that there is quicker feedback on this missing feature.
  • Loading branch information
rix0rrr committed Feb 8, 2019
1 parent f19602c commit 846ed9f
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
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-');
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) {
// 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

0 comments on commit 846ed9f

Please sign in to comment.