如何以优雅的方式从参数存储中取出值在Node.js中:

在使用SSM的getSSMParameters方法获取多个参数时,需要做一些调整,但最终的代码变得比我想象的更加优雅,所以我会附上一个lambda的示例代码。

const AWS = require("aws-sdk");
AWS.config.update({ region: "ap-northeast-1" });
const SSM = new AWS.SSM();

const getSSMParameters = async(names) => {
    const result = await SSM.getParameters({Names: names}).promise();
    return names.map(name => 
        result?.Parameters?.find(el => el.Name === name)?.Value
    );
};

exports.handler = async (event) => {
    const [url, stranger, user, nickname, avatar] = await getSSMParameters(['url','stranger','user','nickname','avatar']);
    const response = {
        statusCode: 200,
        body: {url, stranger, user, nickname, avatar},
    };
    return response;
};
广告
将在 10 秒后关闭
bannerAds