[英]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.