Skip to content

Commit

Permalink
fix(lambda): imported Function still has region and account from its …
Browse files Browse the repository at this point in the history
…Stack, instead of its ARN

Fixes #18228
  • Loading branch information
skinny85 committed Jan 4, 2022
1 parent 82fa742 commit b862ef3
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
4 changes: 3 additions & 1 deletion packages/@aws-cdk/aws-lambda/lib/function.ts
Expand Up @@ -455,7 +455,9 @@ export class Function extends FunctionBase {
protected readonly canCreatePermissions = attrs.sameEnvironment ?? this._isStackAccount();

constructor(s: Construct, i: string) {
super(s, i);
super(s, i, {
environmentFromArn: functionArn,
});

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

Expand Down
29 changes: 29 additions & 0 deletions packages/@aws-cdk/aws-lambda/test/function.test.ts
Expand Up @@ -297,6 +297,35 @@ describe('function', () => {
expect(imported.functionName).toEqual('ProcessKinesisRecords');
});

describe('Function.fromFunctionAttributes()', () => {
let stack: cdk.Stack;

beforeEach(() => {
const app = new cdk.App();
stack = new cdk.Stack(app, 'Base', {
env: { account: '111111111111', region: 'stack-region' },
});
});

describe('for a function in a different account and region', () => {
let func: lambda.IFunction;

beforeEach(() => {
func = lambda.Function.fromFunctionAttributes(stack, 'iFunc', {
functionArn: 'arn:aws:lambda:function-region:222222222222:function:function-name',
});
});

test("the function's region is taken from the ARN", () => {
expect(func.env.region).toBe('function-region');
});

test("the function's account is taken from the ARN", () => {
expect(func.env.account).toBe('222222222222');
});
});
});

describe('addPermissions', () => {
test('imported Function w/ resolved account and function arn', () => {
// GIVEN
Expand Down

0 comments on commit b862ef3

Please sign in to comment.