简体   繁体   English

使用 AWS Lambda function 连接到 kubernetes

[英]Connect to kubernetes with AWS Lambda function

I am trying to create a lambda function for an Alexa skill that will connect to an EKS cluster.我正在尝试为将连接到 EKS 集群的 Alexa 技能创建 lambda function。 I am creating the cluster using with Terraform and hosting it on AWS.我正在使用 Terraform 创建集群并将其托管在 AWS 上。 I am not sure if I am correctly exporting my function correctly and would appreciate some feedback.我不确定我是否正确导出了 function 并希望得到一些反馈。

const Alexa = require('ask-sdk')
const Client = require('kubernetes-client').Client

const connect = require("./connect");

const LaunchRequestHandler = {
  canHandle(handlerInput) {
    return handlerInput.requestEnvelope.request.type === "LaunchRequest";
  },
  handle(handlerInput) {
    console.log("Launch Request Handler Called");

    let speechText =
      "KubeVoice Test";
    let repromptText =
      "I did not receive any input. You can say, Connect to Cluster.";

    return handlerInput.responseBuilder
      .speak(speechText)
      .reprompt(repromptText)
      .getResponse();
  }
};

//Handler for connecting
const KubernetesConnect = {
    canHandle(handlerInput) {
        
        return (
            handlerInput.requestEnvelope.request.type === "IntentRequest" && 
            handlerInput.request.intent.name === "Connect"
        );
    },
    handle(handlerInput) {
        let connectToCluster = connect.connect();
        let speechText = "connected to cluster";
        return handlerInput.responseBuilder.speak(speechText).getResponse();
    }
};

const UnhandledHandler = {
    canHandle() {
        return true;
    },
    handle(handlerInput, error) {
        console.log(`Error Handler : ${error.message}`);
        return handlerInput.responseBuilder.speak(
            "Sorry, I am unable to process your request, ask me to connect"
            ).getResponse();
    }
};



exports.handler = Alexa.SkillBuilders.custom().
    addRequestHandlers(
        LaunchRequestHandler,
        KubernetesConnect,
    )
    .addErrorHandlers(UnhandledHandler)
    .lambda();

And the code for connecting to the eks cluster is taken from the Godaddy Kubernetes client example并且连接eks集群的代码取自Godaddy Kubernetes客户端示例

const Client = require('kubernetes-client').Client

async function connect() {
    try {
        const client = new Client({
            config: {
                url: process.env.K8S_CLUSTER_HOST,
                auth: {
                    provider: {
                        type: 'cmd',
                        config: {
                            'cmd-path': 'aws-iam-authenticator',
                            'cmd-args': 'token -i' + process.env.K8S_AUTH_TOKEN,
                            'cmd-env': {
                                AWS_PROFILE: process.env.AWS_PROFILE
                            },
                            'token-key': 'status.token'
                        }
                    }
                },
                insecureSkipTlsVerify: true
            },
            version: process.env.K8S_CLUSTER_VERSION
        });
    }
    catch (err) {
        console.error('Error: ', err);
    }
}

module.exports.connect = connect;

This is the error I am getting这是我得到的错误

2021-04-10T13:45:14.982Z    c1c8c985-5eb0-43dd-a041-5a077ca12b6c    INFO    Error Handler : Cannot read property 'type' of undefined

was missing .requestEnvelope on the line for the connect intent.连接意图的线路上缺少.requestEnvelope The code functions now, just need to sort IAM roles for the cluster!代码现在运行,只需要对集群的 IAM 角色进行排序!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM