Skip to content

Commit 01bbe4c

Browse files
authored
fix(lambda): imported Function still has region and account from its Stack, instead of its ARN (#18255)
Fixes #18228 ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent e3359e0 commit 01bbe4c

File tree

4 files changed

+36
-3
lines changed

4 files changed

+36
-3
lines changed

packages/@aws-cdk/aws-cloudfront/test/web-distribution.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1377,7 +1377,7 @@ added the ellipsis so a user would know there was more to ...`,
13771377
// GIVEN
13781378
const stack = new cdk.Stack();
13791379
const sourceBucket = new s3.Bucket(stack, 'Bucket');
1380-
const lambdaVersion = lambda.Version.fromVersionArn(stack, 'Version', 'arn:my-version');
1380+
const lambdaVersion = lambda.Version.fromVersionArn(stack, 'Version', 'arn:aws:lambda:function-region:111111111111:function:function-name');
13811381

13821382
// WHEN
13831383
new CloudFrontWebDistribution(stack, 'MyDistribution', {

packages/@aws-cdk/aws-codepipeline-actions/test/lambda/lambda-invoke-action.test.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,9 @@ describe('', () => {
267267
});
268268

269269
test('exposes variables for other actions to consume', () => {
270-
const stack = new Stack();
270+
const stack = new Stack(undefined, undefined, {
271+
env: { account: '123456789012', region: 'us-east-1' },
272+
});
271273

272274
const sourceOutput = new codepipeline.Artifact();
273275
const lambdaInvokeAction = new cpactions.LambdaInvokeAction({

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

+3-1
Original file line numberDiff line numberDiff line change
@@ -455,7 +455,9 @@ export class Function extends FunctionBase {
455455
protected readonly canCreatePermissions = attrs.sameEnvironment ?? this._isStackAccount();
456456

457457
constructor(s: Construct, i: string) {
458-
super(s, i);
458+
super(s, i, {
459+
environmentFromArn: functionArn,
460+
});
459461

460462
this.grantPrincipal = role || new iam.UnknownPrincipal({ resource: this });
461463

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

+29
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,35 @@ describe('function', () => {
297297
expect(imported.functionName).toEqual('ProcessKinesisRecords');
298298
});
299299

300+
describe('Function.fromFunctionAttributes()', () => {
301+
let stack: cdk.Stack;
302+
303+
beforeEach(() => {
304+
const app = new cdk.App();
305+
stack = new cdk.Stack(app, 'Base', {
306+
env: { account: '111111111111', region: 'stack-region' },
307+
});
308+
});
309+
310+
describe('for a function in a different account and region', () => {
311+
let func: lambda.IFunction;
312+
313+
beforeEach(() => {
314+
func = lambda.Function.fromFunctionAttributes(stack, 'iFunc', {
315+
functionArn: 'arn:aws:lambda:function-region:222222222222:function:function-name',
316+
});
317+
});
318+
319+
test("the function's region is taken from the ARN", () => {
320+
expect(func.env.region).toBe('function-region');
321+
});
322+
323+
test("the function's account is taken from the ARN", () => {
324+
expect(func.env.account).toBe('222222222222');
325+
});
326+
});
327+
});
328+
300329
describe('addPermissions', () => {
301330
test('imported Function w/ resolved account and function arn', () => {
302331
// GIVEN

0 commit comments

Comments
 (0)