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

feat(aws-lambda): add grantInvoke() method #962

Merged
merged 1 commit into from
Oct 18, 2018
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
11 changes: 11 additions & 0 deletions packages/@aws-cdk/aws-lambda/lib/lambda-ref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,17 @@ export abstract class FunctionRef extends cdk.Construct
};
}

/**
* Grant the given identity permissions to invoke this Lambda
*/
public grantInvoke(identity?: iam.IPrincipal) {
if (identity) {
identity.addToPolicy(new iam.PolicyStatement()
.addAction('lambda:InvokeFunction')
.addResource(this.functionArn));
}
}

/**
* Return the given named metric for this Lambda
*/
Expand Down
29 changes: 29 additions & 0 deletions packages/@aws-cdk/aws-lambda/test/test.lambda.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1081,6 +1081,35 @@ export = {
test.done();
},

'grantInvoke adds iam:InvokeFunction'(test: Test) {
// GIVEN
const stack = new cdk.Stack();
const role = new iam.Role(stack, 'Role', {
assumedBy: new iam.AccountPrincipal('1234'),
});
const fn = new lambda.Function(stack, 'Function', {
code: lambda.Code.inline('xxx'),
handler: 'index.handler',
runtime: lambda.Runtime.NodeJS810,
});

// WHEN
fn.grantInvoke(role);

// THEN
expect(stack).to(haveResource('AWS::IAM::Policy', {
PolicyDocument: {
Statement: [
{
Action: 'lambda:InvokeFunction',
Resource: { "Fn::GetAtt": [ "Function76856677", "Arn" ] }
}
]
}
}));

test.done();
},
};

function newTestLambda(parent: cdk.Construct) {
Expand Down