#mongodb #amazon-web-services #aws-lambda #mongodb-atlas
Вопрос:
Я написал лямбда-функцию для подключения к Atlas, и когда я запускаю тест, чтобы проверить, установится ли соединение, я получаю Calling the invoke API action failed with this message: Request failed with status code 403
Мой план состоит в том, чтобы сначала установить соединение с atlas, а затем перейти к API REST с помощью шлюза API.
Моя структура файлов в лямбде выглядит следующим образом:
В App.js моя основная причина заключается в проверке того, устанавливается ли соединение с помощью зашифрованной переменной среды:
'use strict'
var MongoClient = require('mongodb').MongoClient;
const AWS = require('aws-sdk');
let atlas_connection_uri;
let cachedDb = null;
exports.handler = (event, context, callback) => {
var uri = process.env['MONGODB_ATLAS_CLUSTER_URI'];
if (atlas_connection_uri != null) {
processEvent(event, context, callback);
}
else {
const kms = new AWS.KMS();
kms.decrypt({ CiphertextBlob: new Buffer(uri, 'base64') }, (err, data) => {
if (err) {
console.log('Decrypt error:', err);
return callback(err);
}
atlas_connection_uri = data.Plaintext.toString('ascii');
processEvent(event, context, callback);
});
}
};
function processEvent(event, context, callback) {
console.log('Calling MongoDB Atlas from AWS Lambda with event: ' JSON.stringify(event));
var jsonContents = JSON.parse(JSON.stringify(event));
context.callbackWaitsForEmptyEventLoop = false;
try {
if (cachedDb == null) {
console.log('=> connecting to database');
MongoClient.connect(atlas_connection_uri, function (err, client) {
cachedDb = client.db('travel');
return createDoc(cachedDb, jsonContents, callback);
});
}
else {
createDoc(cachedDb, jsonContents, callback);
}
}
catch (err) {
console.error('an error occurred', err);
}
};
и мой пакет.json:
{
"name": "lambda-api",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo "Error: no test specified" amp;amp; exit 1"
},
"repository": {
"type": "git",
"url": "git https:
},
"keywords": [],
"author": "",
"license": "ISC",
"homepage": "https:
"dependencies": {
"aws-sdk": "^2.922.0",
"mongodb": "^3.6.9"
}
}
Так что я не уверен, почему я получаю API action failed
ошибку. Кажется, все выглядит правильно.