【问题标题】:Express/AwsServerlessExpress/lambda/Secrets Manager - Exporting a Variable from a function -Express/AwsServerlessExpress/lambda/Secrets Manager - 从函数中导出变量 -
【发布时间】:2020-10-05 09:26:02
【问题描述】:

大纲:我正在尝试将 express 应用程序移植到 lambda,使用 AWS Serverless Express,我需要访问 Secrets Manager 存储 MongoDB Atlas 的凭据。

问题:我已经成功找回了我的秘密,但是我不能 为 AWS Serverless Express 的其余部分“导出”密钥 要使用的应用程序。

我尝试过的:解析和更新环境变量 modules.export.variableName(尝试仅导出变量 秘密)modules.export.functionName(试图导出整个 函数)返回一个数组(将函数的结果返回到 一个数组并从数组中调用)

应用程序结构:lambda.js server.js /db/db.js 控制器: 总共有6个控制器

lambda.js

'use strict'
const awsServerlessExpress = require('aws-serverless-express');
const app = require('./server');
const AWS = require('aws-sdk');
require('dotenv').config();
const secretsManager = new AWS.SecretsManager({
region: 'us-east-1' });
const AWS_SECRET = process.env.AWS_SECRET;

let server;
let uri
var password
module.exports.uri3;
let uri2;

const createServer = async (callback) => {
const secret = await secretsManager.getSecretValue({SecretId: AWS_SECRET}).promise();

const secretBackup = JSON.parse(secret.SecretString)
   
Object.assign(process.env, JSON.parse(secret.SecretString));

module.exports.exporteduserName = secretBackup.username;
console.log('undernasdnfkasdf ' + this.exporteduserName) //This returns the correct value
password = secretBackup.password;
//console.log('this is what is stored in userName ' + userName)
console.log('this is what is stored in password ' + password) //This returns the correct value

uri2 = `mongodb+srv://${process.env.username}:${process.env.password}@cluster0.3jrug.mongodb.net/lamb?retryWrites=true&w=majority`; //This returns the correct value

module.exports.uri3 = uri2

console.log(uri2) 

server = awsServerlessExpress.createServer(app, null, binaryMimeTypes)
return server 

}

console.log('this is uri3 ' + this.uri3) //undefined

console.log('outside function ' + this.exporteduserName) //undefined
const binaryMimeTypes = [
  'application/javascript',
  'application/json',
  'application/octet-stream',
  'application/xml',
  'font/eot',
  'font/opentype',
  'font/otf',
  'image/jpeg',
  'image/png',
  'image/svg+xml',
  'text/comma-separated-values',
  'text/css',
  'text/html',
  'text/javascript',
  'text/plain',
  'text/text',
  'text/xml'
]

我怀疑我的问题在这里(下)- 但是我不完全知道这是什么 代码可以。似乎解决了promise函数然后设置 server 到带有 SecretId 的 createServer 函数。为什么会这样 使用这个变量我不确定。

exports.handler = (event, context) => {
    Promise.resolve(server || createServer({SecretId: AWS_SECRET}, )).then((server) => {
    awsServerlessExpress.proxy(server, event, context);
    //console.log('this is what i have stored ' + process.env.AWS_SECRET);
    });
};

【问题讨论】:

    标签: amazon-web-services express aws-lambda aws-secrets-manager


    【解决方案1】:

    好的,所以我设法解决了这个问题,并想在其他人可能有这个问题的情况下更新它。

    以下记录了 Lambda 的“最佳实践”。在链接中-它们主要提供了应与使用exports.handler 一起使用的格式 https://docs.aws.amazon.com/lambda/latest/dg/best-practices.html

    但是有一个警告 - 因为我使用 AWS Serverless Express - 它需要创建 Promise.resolve 创建服务器事件。因此 - 您不能在 AWS Serverless Express exports.handler 函数中使用 promise 函数。这是因为它已经包含一个 Promise.resolve - 所以如果有两个 Promise,它只会引起混乱。

    exports.handler = (event, context, callback) => {
        Promise.resolve(server || createServer({SecretId: AWS_SECRET})).then((server) => {
            awsServerlessExpress.proxy(server, event, context);
            context.callbackWaitsForEmptyEventLoop = false;
            var uri2 = `mongodb+srv://${process.env.username}:${process.env.password}@clusterX.XYYYYY.mongodb.net/lamb?retryWrites=true&w=majority`;
            var serverjstest = app.abetatestfunc (uri2);
            var dbfiletest = dbfile.dbfiletest (uri2);
          
        });
    
    
    };
    

    就我而言 - 因为我使用 Express,所以我有许多不同的 *.js 文件。为了执行这些功能 - 我必须 module.exports JS 文件,如您在上面看到的(dbfile.debfiletest(uri2))。之后 - 我需要将我的新伪函数放在现有代码之上,同时使用 uri2 输入/变量。即:

    function abetatestfunc(uri2) {
        console.log('this is a test call from within server.js to see if we can get uri2 here ' + uri2)
        uri = uri2
        console.log('server.js value of uri ' + uri)
    
    //your code goes here
    
    }
    

    以这种方式 - 函数将被调用 exports.handler 被 api 网关调用。

    希望对您有所帮助。我确信有 100 种不同的方法可以让它更好/更好/运行得更好——但我还没有。我只是在“让它发挥作用”阶段。

    【讨论】:

      猜你喜欢
      • 2023-03-18
      • 2020-11-27
      • 2021-06-25
      • 2021-02-13
      • 2020-09-28
      • 2021-07-24
      • 2023-01-01
      • 2020-10-26
      • 2023-01-26
      相关资源
      最近更新 更多