Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(lambda): imported Function still has region and account from its Stack, instead of its ARN #18255

Merged
merged 2 commits into from Jan 4, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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