Skip to content

Commit 2c53cf9

Browse files
authored
chore(lambda): hide warning if skipPermissions is set (#30060)
### Issue #29887 Closes #29887 ### Reason for this change If an user imports a lambda and wants to add permissions a warning is show. This warning should be skippable with the skipPermissions flag. ### Description of how you validated changes Unit tests for checking if the warning is shown/not shown depending on the value of `skipPermissions` are added. ### Checklist - [x] My code adheres to the [CONTRIBUTING GUIDE](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md) and [DESIGN GUIDELINES](https://github.com/aws/aws-cdk/blob/main/docs/DESIGN_GUIDELINES.md) ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent 50331a1 commit 2c53cf9

File tree

2 files changed

+54
-1
lines changed

2 files changed

+54
-1
lines changed

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

+3-1
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,9 @@ export abstract class FunctionBase extends Resource implements IFunction, ec2.IC
344344
*/
345345
public addPermission(id: string, permission: Permission) {
346346
if (!this.canCreatePermissions) {
347-
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.`);
347+
if (!this._skipPermissions) {
348+
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.`);
349+
}
348350
return;
349351
}
350352

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

+51
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { ProfilingGroup } from '../../aws-codeguruprofiler';
77
import * as ec2 from '../../aws-ec2';
88
import * as efs from '../../aws-efs';
99
import * as iam from '../../aws-iam';
10+
import { AccountPrincipal } from '../../aws-iam';
1011
import * as kms from '../../aws-kms';
1112
import * as logs from '../../aws-logs';
1213
import * as s3 from '../../aws-s3';
@@ -15,6 +16,7 @@ import * as sns from '../../aws-sns';
1516
import * as sqs from '../../aws-sqs';
1617
import * as cdk from '../../core';
1718
import { Aspects, Lazy, Size } from '../../core';
19+
import { getWarnings } from '../../core/test/util';
1820
import * as cxapi from '../../cx-api';
1921
import * as lambda from '../lib';
2022
import { AdotLambdaLayerJavaSdkVersion } from '../lib/adot-layers';
@@ -223,6 +225,55 @@ describe('function', () => {
223225
fn.addPermission('S4', { principal: new iam.OrganizationPrincipal('my:org') });
224226
});
225227

228+
test('does not show warning if skipPermissions is set', () => {
229+
const app = new cdk.App();
230+
const stack = new cdk.Stack(app);
231+
const imported = lambda.Function.fromFunctionAttributes(stack, 'Imported', {
232+
functionArn: 'arn:aws:lambda:us-west-2:123456789012:function:my-function',
233+
skipPermissions: true,
234+
});
235+
imported.addPermission('Permission', {
236+
action: 'lambda:InvokeFunction',
237+
principal: new AccountPrincipal('123456789010'),
238+
});
239+
240+
expect(getWarnings(app.synth()).length).toBe(0);
241+
});
242+
243+
test('shows warning if skipPermissions is not set', () => {
244+
const app = new cdk.App();
245+
const stack = new cdk.Stack(app);
246+
const imported = lambda.Function.fromFunctionAttributes(stack, 'Imported', {
247+
functionArn: 'arn:aws:lambda:us-west-2:123456789012:function:my-function',
248+
});
249+
imported.addPermission('Permission', {
250+
action: 'lambda:InvokeFunction',
251+
principal: new AccountPrincipal('123456789010'),
252+
});
253+
254+
expect(getWarnings(app.synth())).toEqual([
255+
{
256+
message: {
257+
'Fn::Join': [
258+
'',
259+
[
260+
'addPermission() has no effect on a Lambda Function with region=us-west-2, account=123456789012, in a Stack with region=',
261+
{
262+
Ref: 'AWS::Region',
263+
},
264+
', account=',
265+
{
266+
Ref: 'AWS::AccountId',
267+
},
268+
'. Suppress this warning if this is is intentional, or pass sameEnvironment=true to fromFunctionAttributes() if you would like to add the permissions. [ack: UnclearLambdaEnvironment]',
269+
],
270+
],
271+
},
272+
path: '/Default/Imported',
273+
},
274+
]);
275+
});
276+
226277
test('applies source account/ARN conditions if the principal has conditions', () => {
227278
const stack = new cdk.Stack();
228279
const fn = newTestLambda(stack);

0 commit comments

Comments
 (0)