-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(apigateway): L2 construct for Sagemaker Integration (#25459)
Sagemaker integration for ApiGateway. Pulled the IEndpoint from the alpha module. Stabilizes `sagemaker.IEndpoint` from `aws-sagemaker-alpha` so that it can be used in the integration. ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
- Loading branch information
Showing
10 changed files
with
169 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
packages/aws-cdk-lib/aws-apigateway/lib/integrations/sagemaker.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { AwsIntegration } from './aws'; | ||
import * as iam from '../../../aws-iam'; | ||
import { IEndpoint } from '../../../aws-sagemaker'; | ||
import { IntegrationConfig, IntegrationOptions } from '../integration'; | ||
import { Method } from '../method'; | ||
|
||
/** | ||
* Options for SageMakerIntegration | ||
*/ | ||
export interface SagemakerIntegrationOptions extends IntegrationOptions { | ||
} | ||
|
||
/** | ||
* Integrates an AWS Sagemaker Endpoint to an API Gateway method | ||
* | ||
* @example | ||
* | ||
* declare const resource: apigateway.Resource; | ||
* declare const endpoint: sagemaker.IEndpoint; | ||
* resource.addMethod('POST', new apigateway.SagemakerIntegration(endpoint)); | ||
* | ||
*/ | ||
export class SagemakerIntegration extends AwsIntegration { | ||
private readonly endpoint: IEndpoint; | ||
|
||
constructor(endpoint: IEndpoint, options: SagemakerIntegrationOptions = {}) { | ||
super({ | ||
service: 'runtime.sagemaker', | ||
path: `endpoints/${endpoint.endpointName}/invocations`, | ||
options: { | ||
credentialsRole: options.credentialsRole, | ||
integrationResponses: [ | ||
{ statusCode: '200' }, | ||
], | ||
...options, | ||
}, | ||
}); | ||
|
||
this.endpoint = endpoint; | ||
} | ||
|
||
public bind(method: Method): IntegrationConfig { | ||
const bindResult = super.bind(method); | ||
|
||
const credentialsRole = bindResult.options?.credentialsRole ?? new iam.Role(method, 'SagemakerRole', { | ||
assumedBy: new iam.ServicePrincipal('apigateway.amazonaws.com'), | ||
description: 'Generated by CDK::ApiGateway::SagemakerIntegration', | ||
}); | ||
|
||
this.endpoint.grantInvoke(credentialsRole); | ||
|
||
method.addMethodResponse({ | ||
statusCode: '200', | ||
}); | ||
|
||
return { | ||
...bindResult, | ||
options: { | ||
...bindResult.options, | ||
credentialsRole, | ||
}, | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
packages/aws-cdk-lib/aws-apigateway/test/integrations/sagemaker.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { Template } from '../../../assertions'; | ||
import * as iam from '../../../aws-iam'; | ||
import * as sagemaker from '../../../aws-sagemaker'; | ||
import * as cdk from '../../../core'; | ||
import * as apigateway from '../../lib'; | ||
|
||
describe('SageMaker Integration', () => { | ||
test('minimal setup', () => { | ||
// GIVEN | ||
const stack = new cdk.Stack(); | ||
const api = new apigateway.RestApi(stack, 'my-api'); | ||
const endpoint = new FakeEndpoint(stack, 'FakeEndpoint'); | ||
|
||
// WHEN | ||
const integration = new apigateway.SagemakerIntegration(endpoint); | ||
api.root.addMethod('POST', integration); | ||
|
||
// THEN | ||
Template.fromStack(stack).hasResourceProperties('AWS::ApiGateway::Method', { | ||
Integration: { | ||
IntegrationHttpMethod: 'POST', | ||
Type: 'AWS', | ||
Uri: { | ||
'Fn::Join': [ | ||
'', | ||
[ | ||
'arn:', | ||
{ | ||
Ref: 'AWS::Partition', | ||
}, | ||
':apigateway:', | ||
{ | ||
Ref: 'AWS::Region', | ||
}, | ||
':runtime.sagemaker:path/endpoints/endpointName/invocations', | ||
], | ||
], | ||
}, | ||
}, | ||
}); | ||
}); | ||
}); | ||
|
||
class FakeEndpoint extends cdk.Resource implements sagemaker.IEndpoint { | ||
public readonly endpointArn = 'endpointArn'; | ||
|
||
public readonly endpointName = 'endpointName'; | ||
|
||
public grantInvoke(grantee: iam.IGrantable) { | ||
return iam.Grant.addToPrincipal({ | ||
grantee, | ||
actions: ['sagemaker:InvokeEndpoint'], | ||
resourceArns: [this.endpointArn], | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import * as iam from '../../aws-iam'; | ||
import { IResource } from '../../core'; | ||
|
||
/** | ||
* The interface for a SageMaker Endpoint resource. | ||
*/ | ||
export interface IEndpoint extends IResource { | ||
/** | ||
* The ARN of the endpoint. | ||
* | ||
* @attribute | ||
*/ | ||
readonly endpointArn: string; | ||
|
||
/** | ||
* The name of the endpoint. | ||
* | ||
* @attribute | ||
*/ | ||
readonly endpointName: string; | ||
|
||
/** | ||
* Permits an IAM principal to invoke this endpoint | ||
* @param grantee The principal to grant access to | ||
*/ | ||
grantInvoke(grantee: iam.IGrantable): iam.Grant; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export * from './sagemaker.generated'; | ||
export * from './sagemaker.generated'; | ||
export * from './endpoint'; |
17 changes: 9 additions & 8 deletions
17
packages/aws-cdk-lib/rosetta/aws_apigateway/default.ts-fixture
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
packages/aws-cdk-lib/rosetta/aws_apigateway/stepfunctions.ts-fixture
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters