创建AWS Lambda函数以从IAM获取用户 [英] Create AWS Lambda function to get Users from IAM

查看:54
本文介绍了创建AWS Lambda函数以从IAM获取用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从AWS IAM获取用户详细信息,所以我创建了一个lambda函数,但是响应代码502错误.我的代码如下.

I want to get user details from AWS IAM so I have created a lambda function but there is an error with response code 502. My code is as below.

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

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

    iam.getUser(params, (err, data) => {
        if (err) {
            callback(err)
        } else {
            console.log(data)
            callback(data)
        }
    })
};

推荐答案

由于您的函数已经是 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.

这篇关于创建AWS Lambda函数以从IAM获取用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆