首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >错误:找不到模块'@aws-sdk/client-sns‘

错误:找不到模块'@aws-sdk/client-sns‘
EN

Stack Overflow用户
提问于 2021-02-08 19:04:32
回答 3查看 4K关注 0票数 4

当我调用我的lambda时,我得到了这个错误。

"errorType":"Runtime.ImportModuleError","errorMessage":“错误:找不到模块‘@aws-sdk/client-sns’\n请求堆栈:\n- /var/task/handler.js\n- /var/runtime/UserFunction.js\n- /var/runtime/index.js",”errorMessage“:[ "Runtime.ImportModuleError:错误:找不到模块'@aws-sdk/client-sns'",

代码语言:javascript
代码运行次数:0
运行
复制
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}`);
  }
};

EN

回答 3

Stack Overflow用户

发布于 2021-02-08 22:49:43

如果您使用的是SDK的V2,请使用:

代码语言:javascript
代码运行次数:0
运行
复制
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'})

如果出于大小原因,您只想在Lambda函数中使用SNS模块,我建议您使用AWS SKD for JavaScript的V3。默认情况下,Lambda仍然只与SDK的V2兼容,但有一个变通方法。这是一个example of creating a function using only modules of the AWS SDK for JavaScrpt - version 3

票数 3
EN

Stack Overflow用户

发布于 2021-10-13 21:13:22

当我尝试导入AWS SDK for JavaScript v3中记录的DynamoDB V3客户端时,我的Node14 Lambda函数也遇到了类似的问题。

记录的函数:

客户端错误:找不到模块‘@aws-sdk/

- DynamoDB“DynamoDB

我跟随this guide创建了一个Lambda层,并将其附加到Lambda函数。

然而,我认为这个过程是可以简化的--你可以通过CloudShell来完成,而不是启动EC2实例。托管Lambda的区域不需要支持CloudShell。只需从任何支持它的区域启动CloudShell,然后执行以下操作:

代码语言:javascript
代码运行次数:0
运行
复制
# 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>

你应该可以走了!

票数 1
EN

Stack Overflow用户

发布于 2021-02-09 18:29:35

该问题与V3 sdk的节点版本10.X有关。当我将节点版本更新到12.X时,它开始工作了。我使用的是AWS CDK,因此我还需要将CDK依赖项更新到1.88.0以获得此12.X支持。

在CDK项目中更改lambda的运行时-

运行时: lambda.Runtime.NODEJS_12_X

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66100387

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档