From 846ed9f7f503b1c66e9cdcf061e63d77cc2d6f4b Mon Sep 17 00:00:00 2001 From: Rico Huijbers Date: Fri, 8 Feb 2019 11:18:31 +0100 Subject: [PATCH] fix(lambda): add region check for environment variables (#1690) In some regions Lambda environment variables don't work. Add a check for that so that there is quicker feedback on this missing feature. --- packages/@aws-cdk/aws-lambda/lib/function.ts | 6 +++ .../@aws-cdk/aws-lambda/test/test.lambda.ts | 46 +++++++++++++++++++ 2 files changed, 52 insertions(+) diff --git a/packages/@aws-cdk/aws-lambda/lib/function.ts b/packages/@aws-cdk/aws-lambda/lib/function.ts index 9d5188a4d726a..590fca681d4a6 100644 --- a/packages/@aws-cdk/aws-lambda/lib/function.ts +++ b/packages/@aws-cdk/aws-lambda/lib/function.ts @@ -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, diff --git a/packages/@aws-cdk/aws-lambda/test/test.lambda.ts b/packages/@aws-cdk/aws-lambda/test/test.lambda.ts index 83350ce1aad4d..030a44d15a78c 100644 --- a/packages/@aws-cdk/aws-lambda/test/test.lambda.ts +++ b/packages/@aws-cdk/aws-lambda/test/test.lambda.ts @@ -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();