Good morning all,
I'm trying to connect with NodeJs to my documentdb cluster with mongoose
without ssl:
I get {"message":"Internal server error"}
with ssl i get pem file not found
{"message":"ENOENT: no such file or directory, open '/var/task/rds-combined-ca-bundle.pem'"}
Here is my code with ssl
import { APIGatewayProxyEvent, APIGatewayProxyResult } from 'aws-lambda'
import mongoose = require('mongoose')
import fs = require("fs")
import path = require("path")
export const lambdaHandler = async (event: APIGatewayProxyEvent): Promise<APIGatewayProxyResult> => {
let response: APIGatewayProxyResult;
try {
const filePath = path.join(__dirname, 'rds-combined-ca-bundle.pem')
const databaseUri = 'mongodb://myuser:mypassword@mycluster.docdb.amazonaws.com:27017/?ssl=true&replicaSet=rs0&readPreference=secondaryPreferred&retryWrites=false'
const client = await mongoose.connect(databaseUri, {
ssl: true,
sslValidate: false,
sslCA: filePath,
useNewUrlParser: true,
useUnifiedTopology: true
})
// Return result
response = { statusCode: 200, body: JSON.stringify({ test: 'test mongoose', client: client }) }
} catch (err: unknown) {
console.log('4',err)
response = { statusCode: 500, body: JSON.stringify({ message: err.message }) }
}
return response;
}
Here is my code without ssl
import { APIGatewayProxyEvent, APIGatewayProxyResult } from 'aws-lambda'
import mongoose = require('mongoose')
export const lambdaHandler = async (event: APIGatewayProxyEvent): Promise<APIGatewayProxyResult> => {
let response: APIGatewayProxyResult;
try {
const client = await mongoose.connect(
'mongodb://myuser:mypassword@mycluster.docdb.amazonaws.com:27017/sample-database?replicaSet=rs0&readPreference=secondaryPreferred&retryWrites=false',
{
useNewUrlParser: true
})
//Return result
response = { statusCode: 200, body: JSON.stringify({ test: 'test mongoose', client: client }) }
} catch (err: unknown) {
console.log('4',err)
response = { statusCode: 500, body: JSON.stringify({ message: err.message }) }
}
return response;
}
Could you help me ? thank you sincerely
thank you for your reply ! I will try the solution with the layer. have a nice day