繁体   English   中英

错误:找不到模块“@aws-sdk/client-sns”

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

拨打我的 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", "trace": [ "Runtime.ImportModuleError: 错误: 找不到模块 '@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}`); } };

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

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 function 中使用 SNS 模块,我建议您使用 AWS SKD 的 V3 用于 JavaScript。 Lambda 默认情况下仍仅与 SDK 的 V2 兼容,但有一种解决方法。 以下是仅使用 AWS SDK for JavaScrpt - 版本 3 的模块创建 function 的示例

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 .

function 记录:

错误:找不到模块“@aws-sdk/client-dynamodb”DynamoDB

我按照本指南创建了 Lambda 层并将其附加到 Lambda function。

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

# 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>

你应该很高兴去!

问题在于带有 V3 sdk 的 Node 版本 10.X。 当我将节点版本更新为 12.X 时,它开始工作。 我正在使用 AWS CDK,因此我还需要将 CDK 依赖项更新为 1.88.0 以获得此 12.X 支持。

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

运行时: lambda.Runtime.NODEJS_12_X

我在使用 AWS Layers 和 ES 模块时遇到了类似的问题。 使用绝对说明符进行导入为我解决了这个问题。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM