Explore Courses Blog Tutorials Interview Questions
0 votes
in AWS by (19.1k points)

I am working with the AWS SDK using the KMS library. I would like to use async and await instead of callbacks.

import AWS, { KMS } from "aws-sdk";

this.kms = new AWS.KMS();

const key = await this.kms.generateDataKey();

However, this does not work, when wrapped in an async function.

How can I use async and await here?

1 Answer

0 votes
by (44.4k points)

If you are using aws-sdk with version > 2.x, you can transform an aws.Request to a promise with chain .promise() function. For your case:

  try {

    let key = await kms.generateDataKey().promise();

  } catch (e) {



the key is a KMS.Types.GenerateDataKeyResponse - the second param of callback(in callback style).

The e is an AWSError - The first param of callback func

note: await expression only allowed within an async function

Related questions

Want to get 50% Hike on your Salary?

Learn how we helped 50,000+ professionals like you !

0 votes
1 answer
0 votes
1 answer

Browse Categories