4

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.

Haych
  • 932
  • 13
  • 36
  • Can you explain why you need to make the API Gateway get_api_key() call from boto3, rather than from the AWS JavaScript SDK? – jarmod Dec 07 '18 at 15:50
  • 1
    Someone at Amazon pointed me towards boto3 so I just assumes I had to use that! – Haych Dec 07 '18 at 15:55

1 Answers1

9

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;
};
jarmod
  • 71,565
  • 16
  • 115
  • 122
  • But what if I need to use a custom script? I'm trying to use an external python library and I'm having troubles at running it. How do I install the dependencies? – javiergarval Jan 02 '19 at 11:34
  • @javiergarval building a Python deployment package is described at https://docs.aws.amazon.com/lambda/latest/dg/lambda-python-how-to-create-deployment-package.html – jarmod Jan 02 '19 at 11:45
  • How do I install python dependencies inside a Node.js lambda? I'm working with Node.js and want to use a custom python script; I use node's child_process to spawn the script but I'm not getting a desired response. Instead, it's requiring a library and I cannot install it via ssh. I tried deploying it with the documentation you gave me, created the .zip file with all the dependecies I need but I do not know how to deploy it using $ aws lambda update-function-code --function-name python37 --zip-file fileb://function.zip – javiergarval Jan 02 '19 at 11:59
  • 1
    @javiergarval It's unusual to launch one runtime (Python) from another runtime (Node.js) in Lambda but I would review the blog at https://aws.amazon.com/blogs/compute/running-executables-in-aws-lambda/. You're probably going to need to set some environment variables (including PYTHONPATH?) in advance of spawning the child process so that Python knows where your deployed packages are. – jarmod Jan 02 '19 at 14:00
  • What about the method "start_export_task" (which exports an RDS snapshot to S3)? I can't find it in the aws-sdk, while it exists in boto3. – user345602 Jul 04 '20 at 22:39
  • 1
    @user345602 see startExportTask at https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/RDS.html – jarmod Jul 05 '20 at 00:37