|
| 1 | +import { AwsIntegration } from './aws'; |
| 2 | +import * as iam from '../../../aws-iam'; |
| 3 | +import { IEndpoint } from '../../../aws-sagemaker'; |
| 4 | +import { IntegrationConfig, IntegrationOptions } from '../integration'; |
| 5 | +import { Method } from '../method'; |
| 6 | + |
| 7 | +/** |
| 8 | + * Options for SageMakerIntegration |
| 9 | + */ |
| 10 | +export interface SagemakerIntegrationOptions extends IntegrationOptions { |
| 11 | +} |
| 12 | + |
| 13 | +/** |
| 14 | + * Integrates an AWS Sagemaker Endpoint to an API Gateway method |
| 15 | + * |
| 16 | + * @example |
| 17 | + * |
| 18 | + * declare const resource: apigateway.Resource; |
| 19 | + * declare const endpoint: sagemaker.IEndpoint; |
| 20 | + * resource.addMethod('POST', new apigateway.SagemakerIntegration(endpoint)); |
| 21 | + * |
| 22 | + */ |
| 23 | +export class SagemakerIntegration extends AwsIntegration { |
| 24 | + private readonly endpoint: IEndpoint; |
| 25 | + |
| 26 | + constructor(endpoint: IEndpoint, options: SagemakerIntegrationOptions = {}) { |
| 27 | + super({ |
| 28 | + service: 'runtime.sagemaker', |
| 29 | + path: `endpoints/${endpoint.endpointName}/invocations`, |
| 30 | + options: { |
| 31 | + credentialsRole: options.credentialsRole, |
| 32 | + integrationResponses: [ |
| 33 | + { statusCode: '200' }, |
| 34 | + ], |
| 35 | + ...options, |
| 36 | + }, |
| 37 | + }); |
| 38 | + |
| 39 | + this.endpoint = endpoint; |
| 40 | + } |
| 41 | + |
| 42 | + public bind(method: Method): IntegrationConfig { |
| 43 | + const bindResult = super.bind(method); |
| 44 | + |
| 45 | + const credentialsRole = bindResult.options?.credentialsRole ?? new iam.Role(method, 'SagemakerRole', { |
| 46 | + assumedBy: new iam.ServicePrincipal('apigateway.amazonaws.com'), |
| 47 | + description: 'Generated by CDK::ApiGateway::SagemakerIntegration', |
| 48 | + }); |
| 49 | + |
| 50 | + this.endpoint.grantInvoke(credentialsRole); |
| 51 | + |
| 52 | + method.addMethodResponse({ |
| 53 | + statusCode: '200', |
| 54 | + }); |
| 55 | + |
| 56 | + return { |
| 57 | + ...bindResult, |
| 58 | + options: { |
| 59 | + ...bindResult.options, |
| 60 | + credentialsRole, |
| 61 | + }, |
| 62 | + }; |
| 63 | + } |
| 64 | +} |
0 commit comments