1.在调用 AWS Lambda 函数时,为 IAM 角色添加解密密钥的权限。可以通过以下代码来为角色添加 "KMSDecrypt" 权限:
{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": [ "kms:Decrypt" ], "Resource": [ "arn:aws:kms:us-east-1:012345678910:key/abcd1234-a123-456b-99c7-efg89hijk432" ] } ] }
const AWS = require('aws-sdk'); const kms = new AWS.KMS(); // Initializing AWS KMS
exports.handler = async (event) => { const KeyId = 'alias/MyKeyAlias'; // KMS Key Alias const encryptedPayload = event.Records[0].body; // Accessing Encrypted Payload from Queue const decryptedPayload = await kms.decrypt({ CiphertextBlob: Buffer.from(encryptedPayload, 'base64'), KeyId }).promise(); // Decrypting the Payload
console.log(decryptedPayload.Plaintext.toString('utf-8')); // Accessing Payload data
};
通过这两种方法之一,您的 AWS Lambda 函数就能够读取加密队列中的消息了。