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

chore(lambda): hide warning if skipPermissions is set #30060

Merged
merged 2 commits into from
May 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion packages/aws-cdk-lib/aws-lambda/lib/function-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,9 @@ export abstract class FunctionBase extends Resource implements IFunction, ec2.IC
*/
public addPermission(id: string, permission: Permission) {
if (!this.canCreatePermissions) {
Annotations.of(this).addWarningV2('UnclearLambdaEnvironment', `addPermission() has no effect on a Lambda Function with region=${this.env.region}, account=${this.env.account}, in a Stack with region=${Stack.of(this).region}, account=${Stack.of(this).account}. Suppress this warning if this is is intentional, or pass sameEnvironment=true to fromFunctionAttributes() if you would like to add the permissions.`);
if (!this._skipPermissions) {
Annotations.of(this).addWarningV2('UnclearLambdaEnvironment', `addPermission() has no effect on a Lambda Function with region=${this.env.region}, account=${this.env.account}, in a Stack with region=${Stack.of(this).region}, account=${Stack.of(this).account}. Suppress this warning if this is is intentional, or pass sameEnvironment=true to fromFunctionAttributes() if you would like to add the permissions.`);
}
return;
}

Expand Down
51 changes: 51 additions & 0 deletions packages/aws-cdk-lib/aws-lambda/test/function.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { ProfilingGroup } from '../../aws-codeguruprofiler';
import * as ec2 from '../../aws-ec2';
import * as efs from '../../aws-efs';
import * as iam from '../../aws-iam';
import { AccountPrincipal } from '../../aws-iam';
import * as kms from '../../aws-kms';
import * as logs from '../../aws-logs';
import * as s3 from '../../aws-s3';
Expand All @@ -15,6 +16,7 @@ import * as sns from '../../aws-sns';
import * as sqs from '../../aws-sqs';
import * as cdk from '../../core';
import { Aspects, Lazy, Size } from '../../core';
import { getWarnings } from '../../core/test/util';
import * as cxapi from '../../cx-api';
import * as lambda from '../lib';
import { AdotLambdaLayerJavaSdkVersion } from '../lib/adot-layers';
Expand Down Expand Up @@ -223,6 +225,55 @@ describe('function', () => {
fn.addPermission('S4', { principal: new iam.OrganizationPrincipal('my:org') });
});

test('does not show warning if skipPermissions is set', () => {
const app = new cdk.App();
const stack = new cdk.Stack(app);
const imported = lambda.Function.fromFunctionAttributes(stack, 'Imported', {
functionArn: 'arn:aws:lambda:us-west-2:123456789012:function:my-function',
skipPermissions: true,
});
imported.addPermission('Permission', {
action: 'lambda:InvokeFunction',
principal: new AccountPrincipal('123456789010'),
});

expect(getWarnings(app.synth()).length).toBe(0);
});

test('shows warning if skipPermissions is not set', () => {
const app = new cdk.App();
const stack = new cdk.Stack(app);
const imported = lambda.Function.fromFunctionAttributes(stack, 'Imported', {
functionArn: 'arn:aws:lambda:us-west-2:123456789012:function:my-function',
});
imported.addPermission('Permission', {
action: 'lambda:InvokeFunction',
principal: new AccountPrincipal('123456789010'),
});

expect(getWarnings(app.synth())).toEqual([
{
message: {
'Fn::Join': [
'',
[
'addPermission() has no effect on a Lambda Function with region=us-west-2, account=123456789012, in a Stack with region=',
{
Ref: 'AWS::Region',
},
', account=',
{
Ref: 'AWS::AccountId',
},
'. Suppress this warning if this is is intentional, or pass sameEnvironment=true to fromFunctionAttributes() if you would like to add the permissions. [ack: UnclearLambdaEnvironment]',
],
],
},
path: '/Default/Imported',
},
]);
});

test('applies source account/ARN conditions if the principal has conditions', () => {
const stack = new cdk.Stack();
const fn = newTestLambda(stack);
Expand Down