簡體   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