且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

创建AWS Lambda函数以从IAM获取用户

更新时间:2023-12-02 23:44:52

由于您的函数已经是 async ,因此您无需使用旧的过时的 callback 方法

Since your function already is async you don't need to use the old, outdated callback approach.

AWS开发工具包方法提供了一个 .promise()方法,您可以将其附加到每个AWS异步调用中,并且通过 async ,您可以简单地 await 承诺.

The AWS SDK methods provide a .promise() method which you can append to every AWS asynchronous call and, with async, you can simply await on a Promise.

    var AWS = require('aws-sdk');
    var iam = new AWS.IAM();
    AWS.config.loadFromPath('./config.json');

    let getUsers = async event => {
        var params = {
            UserName: "5dc6f49d50498e2907f8ee69"
        };

        const user = await iam.getUser(params).promise()
        console.log(user)
    };

希望您有一个调用此代码的处理程序,否则它将无法正常工作.如果要导出 getUsers 作为处理函数,请确保首先通过 module.exports 导出它.即: module.exports.getUsers =异步事件... .仔细检查您的Lambda处理程序是否在函数本身上正确配置,其中 index.getUsers 代表 index 是文件名( index.js )和 getUsers 您导出的函数.

Hopefully you have a handler that invokes this code, otherwise this is not going to work. If you want to export getUsers as your handler function, make sure export it through module.exports first. I.e: module.exports.getUsers = async event.... Double check that your Lambda handler is properly configured on the function itself, where index.getUsers would stand for index being the filename (index.js) and getUsers your exported function.