如何使用 AWS lambda 中的帐户 ID 获取帐户名称

问题描述

我正在使用 Node.js 12.x 处理 AWS Lambda 函数我有event.requestContext提取的 accountId。有没有办法使用 lambda 函数中的 accountId 获取帐户名称

解决方法

您实际上可以使用 api call ListAccountAliases 列出帐户别名。 documentaiton

中的相应示例
        // Load the AWS SDK for Node.js
    var AWS = require('aws-sdk');
    // Set the region 
    AWS.config.update({region: 'REGION'});

    // Create the IAM service object
    var iam = new AWS.IAM({apiVersion: '2010-05-08'});

    iam.listAccountAliases({MaxItems: 10},function(err,data) {
    if (err) {
        console.log("Error",err);
    } else {
        console.log("Success",data);
    }
    });