简体   繁体   English

从用 Node.js 编写的 AWS Lambda 函数调用 Python boto3 库

[英]Call Python boto3 library from AWS Lambda function written in Node.js

I have a lambda, written in Node.我有一个用 Node 编写的 lambda。 I need to make a call to the get_api_key function from the boto3 library .我需要从boto3 库调用get_api_key函数。 A stripped down version of my Node.js Lambda function is here:我的 Node.js Lambda 函数的精简版在这里:

exports.handler = function(input, context) {
   const spawn = require("child_process").spawn;
   const pythonProcess = spawn('python',["pythonScript.py", "API_KEY_123"]);
   pythonProcess.stdout.on('data', (data) => {
      console.log("DATA FROM PYTHON: ", data);
   });
};

I used the functionality for this from this question .我从这个问题中使用了这个功能。 My Python script looks like this:我的 Python 脚本如下所示:

import sys
import boto3

#this is the client
client = boto3.client('apigateway')

apiKey = client.get_api_key(apiKey=sys.argv[1], includeValue=True)
print(apiKey)

I expected to see the console.log result appear in my CloudWatch logs for this Lambda function but it seems we aren't getting any data from the Python script as no logging is done.我希望看到此 Lambda 函数的console.log结果出现在我的 CloudWatch 日志中,但似乎我们没有从 Python 脚本中获取任何数据,因为没有完成日志记录。

Am I doing what I am trying to do correctly?我在做我想做的正确的事吗? There is a setting on the Lambda function which says that it is written in Node.js so I don't know if the fact that I have randomly made a Python script in the same directory as the Lambda function will be causing a problem? Lambda 函数上有一个设置说它是用 Node.js 编写的,所以我不知道我在与 Lambda 函数相同的目录中随机制作 Python 脚本是否会导致问题?

I am happy for an alternative to this if it might be easier.如果它可能更容易,我很高兴有替代方案。

AWS Lambda natively supports a number of languages, including JavaScript and Python. AWS Lambda 原生支持多种语言,包括 JavaScript 和 Python。 You don't need to use the boto3 library (which would require you to write in Python).您不需要使用 boto3 库(这需要您用 Python 编写)。 You can use the AWS JavaScript SDK.您可以使用 AWS JavaScript 开发工具包。

Here's an example of getting an API key from API Gateway:以下是从 API 网关获取 API 密钥的示例:

const AWS = require("aws-sdk");
const apigateway = new AWS.APIGateway();

exports.handler = async (event) => {

    var params = {
        apiKey: "ab92ke1p70",
        includeValue: true
    };

    const apikey = await apigateway.getApiKey(params).promise();
    console.log("apikey:", apikey);
    return apikey;
};

暂无
暂无

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

相关问题 如何从 boto3 为 python 中的 node.js 应用程序创建和部署 AWS lambda - How to create and deploy AWS lambda from boto3 for node.js app in python aws lambda 函数禁用和删除 python boto3 中的 CloudFront 分布 - aws lambda function to disable and delete CloudFront distribution in python boto3 如何使用无服务器框架通过 AWS API 网关在 Node.js 中编写的 AWS Lambda function 上返回错误? - How do I return errors on an AWS Lambda function written in Node.js through the AWS API Gateway using the Serverless framework? AWS Lambda Python boto3 从 KeyConditionExpression 中具有多个属性的 dynamodb 表中读取 - AWS Lambda Python boto3 reading from dynamodb table with mulitple attibutes in KeyConditionExpression 尝试从 AWS Lambda 连接到 Boto3 客户端并接收超时 - Trying to connect to Boto3 Client from AWS Lambda and Receiving Timeout 使用来自 lambda boto3 的参数调用 Redshift 存储过程 - Call a Redshift stored procedure with a parameter from lambda boto3 如何使用 boto3 从 Lambda 调用 Redshift 存储过程? - How to call a Redshift stored procedure from Lambda using boto3? 使用 AWS CDK python 部署 node.js lambda - Deploying node.js lambda with AWS CDK python Node.js AWS Lambda 不等待任何事情 - Node.js AWS Lambda not waiting for anything 如何在 Python 中使用 Boto3 AWS Lambda 获取 aws cloudwatch 指标统计信息? - How to get aws cloudwatch metrics statistics using Boto3 AWS Lambda in Python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM