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(appsync): Create Lambda permission when using Lambda Authorizer(#… #20641

Merged
merged 3 commits into from
Jun 13, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
16 changes: 11 additions & 5 deletions packages/@aws-cdk/aws-appsync/lib/graphqlapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,11 +167,6 @@ export interface OpenIdConnectConfig {
export interface LambdaAuthorizerConfig {
/**
* The authorizer lambda function.
* Note: This Lambda function must have the following resource-based policy assigned to it.
* When configuring Lambda authorizers in the console, this is done for you.
* To do so with the AWS CLI, run the following:
*
* `aws lambda add-permission --function-name "arn:aws:lambda:us-east-2:111122223333:function:my-function" --statement-id "appsync" --principal appsync.amazonaws.com --action lambda:InvokeFunction`
*
* @see https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-appsync-graphqlapi-lambdaauthorizerconfig.html
*/
Expand Down Expand Up @@ -519,6 +514,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