rahul sharma
rahul sharma

Reputation: 334

Error: Cannot find module '@aws-sdk/client-sns'

I am getting this error when calling my lambda.

"errorType": "Runtime.ImportModuleError", "errorMessage": "Error: Cannot find module '@aws-sdk/client-sns'\nRequire stack:\n- /var/task/handler.js\n- /var/runtime/UserFunction.js\n- /var/runtime/index.js", "trace": [ "Runtime.ImportModuleError: Error: Cannot find module '@aws-sdk/client-sns'",

import * as AWS from '@aws-sdk/client-sns';
import { JamaSnsMessage } from './models/jama';
import { region, snsTopicArn } from './utils/constants';
import { log } from './utils/logger';

const client = new AWS.SNS({ region });

/**
 * Publishes given SNS formatted Jama items to SNS topic
 *
 * @param {JamaSnsMessage[]} items
 */
export const publishItems = async (items: JamaSnsMessage[]): Promise<void> => {
  if (!items || items.length <= 0) {
    return;
  }

  for (const item of items) {
    const params = {
      /* eslint-disable */
      MessageStructure: 'json',
      Message: JSON.stringify(item),
      TopicArn: snsTopicArn,
      /* eslint-enable */
    };
    log.info(`Sending jama item: ${JSON.stringify(item)} to sns`);
    await send(params);
  }
};

export const send = async (params: AWS.PublishInput): Promise<void> => {
  try {
    const data = await client.send(new AWS.PublishCommand(params));
    log.info(`Item: ${JSON.stringify(params)} was published with id: ${data.MessageId}`);
  } catch (error) {
    log.error(`Error while publishing message ${JSON.stringify(params)}. Cause: ${error}`);
  }
};

Upvotes: 14

Views: 49699

Answers (5)

Dmitry Grinko
Dmitry Grinko

Reputation: 15204

Install sdk v3

npm i @aws-sdk/client-sns

Upvotes: 4

Brian Guilder
Brian Guilder

Reputation: 17

I ran into a similar problem using AWS Layers and ES modules. Using the absolute specifier for the import fixed the issue for me.

import {S3Client, GetObjectCommand} from "file:///opt/nodejs/node_modules/@aws-sdk/client-s3/dist-cjs/index.js";

Upvotes: -1

AmitA
AmitA

Reputation: 3245

I had a similar problem with my Node 14 Lambda function when trying to import the DynamoDB V3 client as documented in AWS SDK for JavaScript v3.

The function logged:

Error: Cannot find module '@aws-sdk/client-dynamodb" DynamoDB

I followed this guide to create a Lambda layer and attach it to the Lambda function.

However, I think the process can be simplified - instead of launching an EC2 instance, you can do this over CloudShell. The region that hosts your Lambda does not need to support CloudShell. Simply launch a CloudShell from any region that supports it, then do:

# Create the directory
$ mkdir -p aws-sdk-layer/nodejs
$ cd aws-sdk-layer/nodejs

# Add the clients you want to use from the new SDK
$ yarn add @aws-sdk/client-dynamodb @aws-sdk/client-apigatewaymanagementapi

# Create the zip file
$ zip -r ../package.zip ../

# Publish the layer
# In the below, change:
#   The layer name (currently node_sdk)
#   The description (currently "My layer")
#   The region
#
# Copy from the response the value of the "LayerVersionArn" key
#
$ aws lambda publish-layer-version --layer-name node_sdk --description "My layer" --license-info "MIT" --compatible-runtimes nodejs14.x --zip-file fileb://../package.zip --region <specify a region>

# Add the layer to your function
# In the below, change:
#   The function name (currently my-function)
#   Set the layer's ARN to the value of the "LayerVersionArn" key in the response from the previous statement (currently arn:aws:lambda:us-east-2:123456789012:layer:node_sdk:1)
#   The region
#
$ aws lambda update-function-configuration --function-name my-function --layers arn:aws:lambda:us-east-2:123456789012:layer:node_sdk:1 --region <specify a region>

And you should be good to go!

Upvotes: 0

bmur
bmur

Reputation: 446

If you're using V2 of the SDK, use:

var AWS = require('aws-sdk');
AWS.config.update({region: 'REGION'});

// Create promise and SNS service object
const SNS = new AWS.SNS({apiVersion: '2010-03-31'})

And if for size reasons you want to use only the SNS module in your Lambda function, I recommend you use V3 of the AWS SKD for JavaScript. Lambda is still compatible by default only with V2 of the SDK, but there's a workaround. Here's an example of creating a function using only modules of the AWS SDK for JavaScrpt - version 3.

Upvotes: 7

rahul sharma
rahul sharma

Reputation: 334

The issue was with Node version 10.X with V3 sdk. when I updated the node version to 12.X then it started working. I am using AWS CDK so I also needed to update the CDK dependencies to 1.88.0 to get this 12.X support.

Change runtime for lambda in CDK project -

runtime: lambda.Runtime.NODEJS_12_X

Upvotes: -1

Related Questions