Skip to content
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
6 changes: 5 additions & 1 deletion packages/@aws-cdk/aws-lambda/lib/function-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -285,8 +285,12 @@ export abstract class FunctionBase extends Resource implements IFunction {
return (principal as iam.ServicePrincipal).service;
}

if (`arn` in principal) {
return (principal as iam.ArnPrincipal).arn;
}

throw new Error(`Invalid principal type for Lambda permission statement: ${principal.constructor.name}. ` +
'Supported: AccountPrincipal, ServicePrincipal');
'Supported: AccountPrincipal, ArnPrincipal, ServicePrincipal');
}
}

Expand Down
33 changes: 31 additions & 2 deletions packages/@aws-cdk/aws-lambda/test/test.lambda.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,15 +186,16 @@ export = {
test.done();
},

'fails if the principal is not a service or account principals'(test: Test) {
'fails if the principal is not a service, account or arn principal'(test: Test) {
const stack = new cdk.Stack();
const fn = newTestLambda(stack);

test.throws(() => fn.addPermission('F1', { principal: new iam.ArnPrincipal('just:arn') }),
test.throws(() => fn.addPermission('F1', { principal: new iam.OrganizationPrincipal('org') }),
/Invalid principal type for Lambda permission statement/);

fn.addPermission('S1', { principal: new iam.ServicePrincipal('my-service') });
fn.addPermission('S2', { principal: new iam.AccountPrincipal('account') });
fn.addPermission('S3', { principal: new iam.ArnPrincipal('my:arn') });

test.done();
},
Expand Down Expand Up @@ -1074,6 +1075,34 @@ export = {
test.done();
},

'grantInvoke with an arn principal'(test: Test) {
// GIVEN
const stack = new cdk.Stack();
const fn = new lambda.Function(stack, 'Function', {
code: lambda.Code.inline('xxx'),
handler: 'index.handler',
runtime: lambda.Runtime.NODEJS_8_10,
});
const account = new iam.ArnPrincipal('arn:aws:iam::123456789012:role/someRole');

// WHEN
fn.grantInvoke(account);

// THEN
expect(stack).to(haveResource('AWS::Lambda::Permission', {
Action: 'lambda:InvokeFunction',
FunctionName: {
'Fn::GetAtt': [
'Function76856677',
'Arn'
]
},
Principal: 'arn:aws:iam::123456789012:role/someRole'
}));

test.done();
},

'Can use metricErrors on a lambda Function'(test: Test) {
// GIVEN
const stack = new cdk.Stack();
Expand Down