TiernO
TiernO

Reputation: 447

Cannot find module aws-amplify, lambda function

I'm try to make an API post request in my lambda function but in the aws website, using nodejs I cannot import API ? Here is what I am trying

console.log('Loading function');
const AWS = require('aws-sdk');
const translate = new AWS.Translate({ apiVersion: '2017-07-01' });
var API = require('aws-amplify');

exports.handler = async (event, context) => {
  try {
    const params = {
      SourceLanguageCode: 'en', /* required */
      TargetLanguageCode: 'es', /* required */
      Text: 'Hello World', /* required */
    };

    const data = await translate.translateText(params).promise();

    createSite(data.TranslatedText);

  } catch (err) {
    console.log(err, err.stack);
  }

  function createSite(site) {
  return API.post("sites", "/sites", {
    body: site
  });
}
};

I have also tried import...

Upvotes: 0

Views: 1241

Answers (1)

Mark B
Mark B

Reputation: 200476

I think you may be looking at front-end browser based JavaScript examples, which aren't always going to work in a back-end AWS Lambda NodeJS runtime environment. It appears you are trying to use this library, which states it is "a JavaScript library for frontend and mobile developers", which probably isn't what you want to use on AWS Lambda. It appears you also did not include that library in your AWS Lambda function's deployment.

I suggest using the AWS Amplify client in the AWS SDK for NodeJS which is automatically included in your Lambda function's runtime environment. You would create an Amplify client like so:

var amplify = new AWS.Amplify();

Upvotes: 2

Related Questions