Aws CDK:Api网关,sagemaker集成,不支持类型为SageMakerRuntime的AWS服务错误。
创始人
2024-11-14 14:00:15
0

出现这个错误的原因是AWS CDK不支持直接创建类型为SageMakerRuntime的AWS服务。解决方法是使用AWS CDK的高级功能来创建这个服务。

以下是一个使用AWS CDK创建Api网关并集成SageMaker的示例代码:

import * as cdk from 'aws-cdk-lib';
import * as apigateway from 'aws-cdk-lib/aws-apigateway';
import * as sagemaker from 'aws-cdk-lib/aws-sagemaker';

export class MyStack extends cdk.Stack {
  constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {
    super(scope, id, props);

    // 创建SageMaker模型
    const model = new sagemaker.CfnModel(this, 'MyModel', {
      executionRoleArn: 'YOUR_EXECUTION_ROLE_ARN',
      primaryContainer: {
        image: 'YOUR_MODEL_IMAGE',
        modelDataUrl: 'YOUR_MODEL_DATA_URL',
      },
    });

    // 创建SageMaker端点配置
    const endpointConfig = new sagemaker.CfnEndpointConfig(this, 'MyEndpointConfig', {
      productionVariants: [{
        variantName: 'MyVariant',
        modelName: model.ref,
        initialInstanceCount: 1,
        instanceType: 'ml.t2.medium',
        initialVariantWeight: 1,
      }],
    });

    // 创建SageMaker端点
    const endpoint = new sagemaker.CfnEndpoint(this, 'MyEndpoint', {
      endpointConfigName: endpointConfig.ref,
    });

    // 创建Api网关
    const api = new apigateway.RestApi(this, 'MyApi');

    // 创建Lambda集成
    const integration = new apigateway.AwsIntegration({
      service: 'sagemaker',
      integrationHttpMethod: 'POST',
      path: `endpoints/${endpoint.ref}/invocations`,
      options: {
        credentialsRole: 'YOUR_LAMBDA_ROLE_ARN',
        passthroughBehavior: apigateway.PassthroughBehavior.NEVER,
        requestTemplates: {
          'application/json': `{
            "body": $util.escapeJavaScript($input.json('$')),
            "contentType": "application/json"
          }`,
        },
        integrationResponses: [{
          statusCode: '200',
          responseTemplates: {
            'application/json': `#set($inputRoot = $input.path('$'))
            $inputRoot`,
          },
        }],
      },
    });

    // 创建资源和方法
    const resource = api.root.addResource('my-resource');
    const method = resource.addMethod('POST', integration);
  }
}

const app = new cdk.App();
new MyStack(app, 'MyStack');

在上面的示例代码中,我们首先创建了SageMaker模型、端点配置和端点。然后,我们创建了Api网关,并使用AwsIntegration类来集成SageMaker。最后,我们将资源和方法添加到Api网关。

请注意,示例代码中的一些值需要替换为您自己的值,比如YOUR_EXECUTION_ROLE_ARNYOUR_MODEL_IMAGEYOUR_MODEL_DATA_URLYOUR_LAMBDA_ROLE_ARN

希望这个示例能够帮助您解决问题!

相关内容

热门资讯

重大通报!Wepoke德州版本... 重大通报!Wepoke德州版本(wePokE)外挂透明挂辅助软件(透视)安装教程(讲解有挂)-哔哩哔...
透视代打(微扑克ai代打)外挂... 透视代打(微扑克ai代打)外挂透明挂辅助器安装(辅助挂)软件透明挂(2021已更新)(哔哩哔哩);科...
透明新版(云扑克辅助挂)外挂透... 云扑克辅助挂赢率提升策略‌;透明新版(云扑克辅助挂)外挂透明挂辅助APP(透视)发牌规律(有挂方式)...
透视了解!德州最新(WEPOk... WePoke高级策略深度解析‌;透视了解!德州最新(WEPOke)外挂透明挂辅助APP(辅助挂)教你...
透视好牌(aapOKER)外挂... 透视好牌(aapOKER)外挂透明挂辅助APP(透视)透视辅助(2025已更新)(哔哩哔哩)是一款可...
分享一款(微扑克游戏)外挂透明... 分享一款(微扑克游戏)外挂透明挂辅助器(辅助挂)发牌规律(了解有挂)-哔哩哔哩是一款可以让一直输的玩...
程序员教你!pokerrrr开... WePoker透视辅助版本解析‌,程序员教你!pokerrrr开挂(wepoke)外挂透明挂辅助机制...
透视透明(德州之星ai)外挂透... 1、透视透明(德州之星ai)外挂透明挂辅助工具(辅助挂)透视辅助(2021已更新)(哔哩哔哩);该软...
热点推荐(Wepoke代打)外... 热点推荐(Wepoke代打)外挂透明挂辅助软件(透视)透视辅助(有挂分享)-哔哩哔哩;值得一提的是,...
必知教程!微扑克总结(Wepo... 必知教程!微扑克总结(Wepoke)外挂透明挂辅助app(辅助挂)可靠技巧(真实有挂)-哔哩哔哩;微...