Skip to content

Commit b3792aa

Browse files
jogoldrix0rrr
authored andcommitted
fix(lambda): allow grantInvoke with principals (#2391)
Fixes 'Cannot use tokens in construct ID' when calling grantInvoke with a service or account principal. Error with a service: Cannot use tokens in construct ID: Invoke{"Service":["${Token[TOKEN.139]}"]} Error with an account: Cannot use tokens in construct ID: Invoke{"AWS":["${Token[TOKEN.813]}"]}
1 parent 52af870 commit b3792aa

File tree

2 files changed

+57
-1
lines changed

2 files changed

+57
-1
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ export abstract class FunctionBase extends Resource implements IFunction {
228228
resource: {
229229
addToResourcePolicy: (_statement) => {
230230
// Couldn't add permissions to the principal, so add them locally.
231-
const identifier = 'Invoke' + JSON.stringify(grantee!.grantPrincipal.policyFragment.principalJson);
231+
const identifier = `Invoke${grantee.grantPrincipal}`; // calls the .toString() of the princpal
232232
this.addPermission(identifier, {
233233
principal: grantee.grantPrincipal!,
234234
action: 'lambda:InvokeFunction',

packages/@aws-cdk/aws-lambda/test/test.lambda.ts

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1020,6 +1020,62 @@ export = {
10201020
test.done();
10211021
},
10221022

1023+
'grantInvoke with a service principal'(test: Test) {
1024+
// GIVEN
1025+
const stack = new cdk.Stack();
1026+
const fn = new lambda.Function(stack, 'Function', {
1027+
code: lambda.Code.inline('xxx'),
1028+
handler: 'index.handler',
1029+
runtime: lambda.Runtime.NodeJS810,
1030+
});
1031+
const service = new iam.ServicePrincipal('apigateway.amazonaws.com');
1032+
1033+
// WHEN
1034+
fn.grantInvoke(service);
1035+
1036+
// THEN
1037+
expect(stack).to(haveResource('AWS::Lambda::Permission', {
1038+
Action: 'lambda:InvokeFunction',
1039+
FunctionName: {
1040+
'Fn::GetAtt': [
1041+
'Function76856677',
1042+
'Arn'
1043+
]
1044+
},
1045+
Principal: 'apigateway.amazonaws.com'
1046+
}));
1047+
1048+
test.done();
1049+
},
1050+
1051+
'grantInvoke with an account principal'(test: Test) {
1052+
// GIVEN
1053+
const stack = new cdk.Stack();
1054+
const fn = new lambda.Function(stack, 'Function', {
1055+
code: lambda.Code.inline('xxx'),
1056+
handler: 'index.handler',
1057+
runtime: lambda.Runtime.NodeJS810,
1058+
});
1059+
const account = new iam.AccountPrincipal('123456789012');
1060+
1061+
// WHEN
1062+
fn.grantInvoke(account);
1063+
1064+
// THEN
1065+
expect(stack).to(haveResource('AWS::Lambda::Permission', {
1066+
Action: 'lambda:InvokeFunction',
1067+
FunctionName: {
1068+
'Fn::GetAtt': [
1069+
'Function76856677',
1070+
'Arn'
1071+
]
1072+
},
1073+
Principal: '123456789012'
1074+
}));
1075+
1076+
test.done();
1077+
},
1078+
10231079
'Can use metricErrors on a lambda Function'(test: Test) {
10241080
// GIVEN
10251081
const stack = new cdk.Stack();

0 commit comments

Comments
 (0)