0

I am trying fetch IAM role in aws lambda function, like

const iamClient = new IAMClient({
        region: "us-west-2"
    });
const role = await iamClient.getRole({
            RoleName: roleName
        });

But I am getting below error,

"TypeError: iamClient.getRole is not a function"

Is there any better way to fetch IAM policy or role in aws lambda?

Mangesh Tak
  • 346
  • 1
  • 6
  • 22

1 Answers1

0

You're importing the v3 SDK but writing v2 code. v2 IAM clients expose named methods such as getRole, CreateUser etc. but v3 requires you to build a 'command' object and then to send it via the client object. Recommend you read some of the SDK v3 samples.

Here's one such example:

import { GetRoleCommand, IAMClient } from "@aws-sdk/client-iam";

const client = new IAMClient({ region: "us-west-2" });

const getRole = async (RoleName) => {
  const command = new GetRoleCommand({ RoleName });
  return client.send(command);
};

If you prefer to use the v2 SDK then:

const AWS = require('aws-sdk');

const iam = new AWS.IAM({ region: "us-west-2" });

const getRole = async (RoleName) => {
  return iam.getRole({ RoleName }).promise();
};
jarmod
  • 71,565
  • 16
  • 115
  • 122