Skip to content

Commit

Permalink
fix(appsync): Create Lambda permission when using Lambda Authorizer(a…
Browse files Browse the repository at this point in the history
  • Loading branch information
cm-iwata committed Jun 7, 2022
1 parent 15bcc3c commit 30a81a7
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
11 changes: 11 additions & 0 deletions packages/@aws-cdk/aws-appsync/lib/graphqlapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,17 @@ export class GraphqlApi extends GraphqlApiBase {
this.apiKeyResource.addDependsOn(this.schemaResource);
this.apiKey = this.apiKeyResource.attrApiKey;
}

if (modes.some((mode) => mode.authorizationType === AuthorizationType.LAMBDA)) {
const config = modes.find((mode: AuthorizationMode) => {
return mode.authorizationType === AuthorizationType.LAMBDA && mode.lambdaAuthorizerConfig;
})?.lambdaAuthorizerConfig;
config?.handler.addPermission('appsync', {
principal: new ServicePrincipal('appsync.amazonaws.com'),
action: 'lambda:InvokeFunction',
});
}

}

/**
Expand Down
39 changes: 39 additions & 0 deletions packages/@aws-cdk/aws-appsync/test/appsync-auth.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -669,6 +669,18 @@ describe('AppSync Lambda Authorization', () => {
},
},
});

Template.fromStack(stack).hasResourceProperties('AWS::Lambda::Permission', {
Action: 'lambda:InvokeFunction',
FunctionName: {
'Fn::GetAtt': [
'authfunction96361832',
'Arn',
],
},
});


});

test('Lambda authorization configurable in default authorization', () => {
Expand Down Expand Up @@ -702,6 +714,15 @@ describe('AppSync Lambda Authorization', () => {
IdentityValidationExpression: 'custom-.*',
},
});
Template.fromStack(stack).hasResourceProperties('AWS::Lambda::Permission', {
Action: 'lambda:InvokeFunction',
FunctionName: {
'Fn::GetAtt': [
'authfunction96361832',
'Arn',
],
},
});
});

test('Lambda authorization configurable in additional authorization has default configuration', () => {
Expand Down Expand Up @@ -733,6 +754,15 @@ describe('AppSync Lambda Authorization', () => {
},
}],
});
Template.fromStack(stack).hasResourceProperties('AWS::Lambda::Permission', {
Action: 'lambda:InvokeFunction',
FunctionName: {
'Fn::GetAtt': [
'authfunction96361832',
'Arn',
],
},
});
});

test('Lambda authorization configurable in additional authorization', () => {
Expand Down Expand Up @@ -768,6 +798,15 @@ describe('AppSync Lambda Authorization', () => {
},
}],
});
Template.fromStack(stack).hasResourceProperties('AWS::Lambda::Permission', {
Action: 'lambda:InvokeFunction',
FunctionName: {
'Fn::GetAtt': [
'authfunction96361832',
'Arn',
],
},
});
});

test('Lambda authorization throws with multiple lambda authorization', () => {
Expand Down

0 comments on commit 30a81a7

Please sign in to comment.