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): allow grantInvoke with principals #2391

Merged
merged 1 commit into from
May 6, 2019
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
2 changes: 1 addition & 1 deletion packages/@aws-cdk/aws-lambda/lib/function-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ export abstract class FunctionBase extends Resource implements IFunction {
resource: {
addToResourcePolicy: (_statement) => {
// Couldn't add permissions to the principal, so add them locally.
const identifier = 'Invoke' + JSON.stringify(grantee!.grantPrincipal.policyFragment.principalJson);
const identifier = `Invoke${grantee.grantPrincipal}`; // calls the .toString() of the princpal
this.addPermission(identifier, {
principal: grantee.grantPrincipal!,
action: 'lambda:InvokeFunction',
Expand Down
56 changes: 56 additions & 0 deletions packages/@aws-cdk/aws-lambda/test/test.lambda.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1020,6 +1020,62 @@ export = {
test.done();
},

'grantInvoke with a service 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.NodeJS810,
});
const service = new iam.ServicePrincipal('apigateway.amazonaws.com');

// WHEN
fn.grantInvoke(service);

// THEN
expect(stack).to(haveResource('AWS::Lambda::Permission', {
Action: 'lambda:InvokeFunction',
FunctionName: {
'Fn::GetAtt': [
'Function76856677',
'Arn'
]
},
Principal: 'apigateway.amazonaws.com'
}));

test.done();
},

'grantInvoke with an account 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.NodeJS810,
});
const account = new iam.AccountPrincipal('123456789012');

// WHEN
fn.grantInvoke(account);

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

test.done();
},

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