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 (#18255)

Fixes #18228

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
skinny85 committed Jan 4, 2022
1 parent e3359e0 commit 01bbe4c
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 3 deletions.
Expand Up @@ -1377,7 +1377,7 @@ added the ellipsis so a user would know there was more to ...`,
// GIVEN
const stack = new cdk.Stack();
const sourceBucket = new s3.Bucket(stack, 'Bucket');
const lambdaVersion = lambda.Version.fromVersionArn(stack, 'Version', 'arn:my-version');
const lambdaVersion = lambda.Version.fromVersionArn(stack, 'Version', 'arn:aws:lambda:function-region:111111111111:function:function-name');

// WHEN
new CloudFrontWebDistribution(stack, 'MyDistribution', {
Expand Down
Expand Up @@ -267,7 +267,9 @@ describe('', () => {
});

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

const sourceOutput = new codepipeline.Artifact();
const lambdaInvokeAction = new cpactions.LambdaInvokeAction({
Expand Down
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 01bbe4c

Please sign in to comment.