Haych
Haych

Reputation: 942

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

I have a lambda, written in Node. I need to make a call to the get_api_key function from the boto3 library. A stripped down version of my Node.js Lambda function is here:

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:

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.

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?

I am happy for an alternative to this if it might be easier.

Upvotes: 4

Views: 10536

Answers (1)

jarmod
jarmod

Reputation: 78850

AWS Lambda natively supports a number of languages, including JavaScript and Python. You don't need to use the boto3 library (which would require you to write in Python). You can use the AWS JavaScript SDK.

Here's an example of getting an API key from API Gateway:

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;
};

Upvotes: 9

Related Questions