Skip to content

Commit 846ed9f

Browse files
authored
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.
1 parent f19602c commit 846ed9f

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

packages/@aws-cdk/aws-lambda/lib/function.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,12 @@ export class Function extends FunctionBase {
347347
this.role.addToPolicy(statement);
348348
}
349349

350+
const stack = cdk.Stack.find(this);
351+
const isChina = stack.env.region && stack.env.region.startsWith('cn-');
352+
if (isChina && props.environment && Object.keys(props.environment).length > 0) {
353+
throw new Error(`Environment variables are not supported in this region (${stack.env.region}); consider using tags or SSM parameters instead`);
354+
}
355+
350356
const resource = new CfnFunction(this, 'Resource', {
351357
functionName: props.functionName,
352358
description: props.description,

packages/@aws-cdk/aws-lambda/test/test.lambda.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1178,6 +1178,52 @@ export = {
11781178
test.done();
11791179
},
11801180

1181+
'environment variables are prohibited in China'(test: Test) {
1182+
// GIVEN
1183+
const stack = new cdk.Stack(undefined, undefined, { env: { region: 'cn-north-1' }});
1184+
1185+
// WHEN
1186+
test.throws(() => {
1187+
new lambda.Function(stack, 'MyLambda', {
1188+
code: new lambda.InlineCode('foo'),
1189+
handler: 'index.handler',
1190+
runtime: lambda.Runtime.NodeJS,
1191+
environment: {
1192+
SOME: 'Variable'
1193+
}
1194+
});
1195+
}, /Environment variables are not supported/);
1196+
1197+
test.done();
1198+
},
1199+
1200+
'environment variables work in an unspecified region'(test: Test) {
1201+
// GIVEN
1202+
const stack = new cdk.Stack();
1203+
1204+
// WHEN
1205+
new lambda.Function(stack, 'MyLambda', {
1206+
code: new lambda.InlineCode('foo'),
1207+
handler: 'index.handler',
1208+
runtime: lambda.Runtime.NodeJS,
1209+
environment: {
1210+
SOME: 'Variable'
1211+
}
1212+
});
1213+
1214+
// THEN
1215+
expect(stack).to(haveResource('AWS::Lambda::Function', {
1216+
Environment: {
1217+
Variables: {
1218+
SOME: "Variable"
1219+
}
1220+
}
1221+
}));
1222+
1223+
test.done();
1224+
1225+
},
1226+
11811227
'support reserved concurrent executions'(test: Test) {
11821228
const stack = new cdk.Stack();
11831229

0 commit comments

Comments
 (0)