【问题标题】:Is there a way to deploy a Lex bot using CDK?有没有办法使用 CDK 部署 Lex 机器人?
【发布时间】:2020-09-05 19:29:57
【问题描述】:

我想使用 CDK 将 Lex 机器人部署到我的 AWS 账户。

查看 API 参考 documentation 我找不到 Lex 的构造。另外,我在 CDK GitHub 存储库中找到了这个 issue,它确认 Lex 没有 CDK 构造。

是否有任何解决方法可以部署 Lex 机器人或其他工具来执行此操作?

【问题讨论】:

    标签: aws-cdk aws-lex


    【解决方案1】:

    有!虽然可能有点麻烦,但完全可以使用custom resources

    自定义资源通过定义处理自定义资源的创建和删除事件的 lambda 来工作。由于可以使用 AWS API 创建和删除 AWS Lex 机器人,因此我们可以让 lambda 在创建或销毁资源时执行此操作。

    这是我在 TS/JS 中编写的一个简单示例:

    CDK 代码(TypeScript):

    import * as path from 'path';
    import * as cdk from '@aws-cdk/core';
    import * as iam from '@aws-cdk/aws-iam';
    import * as logs from '@aws-cdk/aws-logs';
    import * as lambda from '@aws-cdk/aws-lambda';
    import * as cr from '@aws-cdk/custom-resources';
    
    export class CustomResourceExample extends cdk.Stack {
      constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {
        super(scope, id, props);
    
        // Lambda that will handle the different cloudformation resource events
        const lexBotResourceHandler = new lambda.Function(this, 'LexBotResourceHandler', {
          code: lambda.Code.fromAsset(path.join(__dirname, 'lambdas')),
          handler: 'lexBotResourceHandler.handler',
          runtime: lambda.Runtime.NODEJS_14_X,
        });
        lexBotResourceHandler.addToRolePolicy(new iam.PolicyStatement({
          resources: ['*'],
          actions: ['lex:PutBot', 'lex:DeleteBot']
        }))
    
        // Custom resource provider, specifies how the custom resources should be created
        const lexBotResourceProvider = new cr.Provider(this, 'LexBotResourceProvider', {
          onEventHandler: lexBotResourceHandler,
          logRetention: logs.RetentionDays.ONE_DAY // Default is to keep forever
        });
    
        // The custom resource, creating one of these will invoke the handler and create the bot
        new cdk.CustomResource(this, 'ExampleLexBot', {
          serviceToken: lexBotResourceProvider.serviceToken,
    
          // These options will be passed down to the lambda
          properties: {
            locale: 'en-US',
            childDirected: false
          }
        })
      }
    }
    

    Lambda 代码 (JavaScript):

    const AWS = require('aws-sdk');
    const Lex = new AWS.LexModelBuildingService();
    
    const onCreate = async (event) => {
      await Lex.putBot({
        name: event.LogicalResourceId,
        locale: event.ResourceProperties.locale,
        childDirected: Boolean(event.ResourceProperties.childDirected)
      }).promise();
    };
    
    const onUpdate = async (event) => {
      // TODO: Not implemented
    };
    
    const onDelete = async (event) => {
      await Lex.deleteBot({
        name: event.LogicalResourceId
      }).promise();
    };
    
    exports.handler = async (event) => {
      switch (event.RequestType) {
        case 'Create':
          await onCreate(event);
          break;
    
        case 'Update':
          await onUpdate(event);
          break;
    
        case 'Delete':
          await onDelete(event);
          break;
      }
    };
    

    我承认这是一个非常简单的示例,但希望它足以让您或任何人开始阅读并了解如何通过添加更多选项和更多自定义资源(例如意图)来构建它。

    【讨论】:

      猜你喜欢
      • 2013-02-17
      • 2020-10-06
      • 1970-01-01
      • 2021-01-08
      • 1970-01-01
      • 2015-08-31
      • 2012-05-08
      • 2018-12-26
      • 1970-01-01
      相关资源
      最近更新 更多