AWS Lambda의 ESModule
5859 단어 lambdaesmoduletoplevelawaitaws
NodeJS 기본 모듈 시스템
CommonJS입니다(AWS SDK를 추가하는 CommonJS 방식
const AWS = require('aws-sdk');
).npm install aws-sdk
. CommonJS를 사용하는 Lambda 코드의 예를 들어 보겠습니다.
CommonJS 예제
const AWS = require('aws-sdk');
//Brings in entire AWS SDK that contains all services clients.
const functionName = process.env.AWS_LAMBDA_FUNCTION_NAME;
const encrypted = process.env['secret'];
//Environment variable 'secret' is encrypted using Customer Managed Key.
let decrypted;
//Cold Start: 'decrypted' value is null.
//Warm Start: 'decrypted' value is not null - contains decrypted value.
exports.handler = async (event) => {
if (!decrypted) {
//matches only during Cold Start
await fetchDecryptedValue();
}
processEvent(event, decrypted);
};
let fetchDecryptedValue = async function() {
const kms = new AWS.KMS();
try {
const req = {
CiphertextBlob: Buffer.from(encrypted, 'base64'),
EncryptionContext: { LambdaFunctionName: functionName},
};
const data = await kms.decrypt(req).promise();
decrypted = data.Plaintext.toString('ascii');
} catch (err) {
console.log('Decrypt error:', err);
throw err;
}
}
let processEvent = function(event, decrypted) {
// use decrypted credential here
}
ESModule 예제
ESModule이 Lambda에서 작동하도록 하기 위한 전제 조건
{
"name": "blog-esmodule",
"type": "module",
"description": "Example for esmodule usage in AWS Lambda.",
"version": "1.0",
"main": "index.js",
"dependencies": {
"@aws-sdk/client-kms": "^3.76.0"
}
}
npm install @aws-sdk/client-kms
//ESModule example
import { KMSClient, DecryptCommand } from "@aws-sdk/client-kms";
// Imports only the kms service client. Reduced code size. Improved cold start time
const functionName = process.env.AWS_LAMBDA_FUNCTION_NAME;
const encrypted = process.env['secret'];
let decrypted;
//during Cold/Warm start behaves same as CommonJS lambda code
export const handler = async (event) => {
if (!decrypted) {
await fetchDecryptedValue();
}
processEvent(event, decrypted);
};
let fetchDecryptedValue = async function() {
const client = new KMSClient();
try {
const req = {
CiphertextBlob: Buffer.from(encrypted, 'base64'),
EncryptionContext: { LambdaFunctionName: functionName },
};
const command = new DecryptCommand(req);
const data = await client.send(command);
decrypted = Buffer.from(data.Plaintext).toString();
} catch (err) {
console.log('Decrypt error:', err);
throw err;
}
}
let processEvent = function(event, decrypted) {
// use decrypted credential here
}
ESModule의 이점
AWS Lambda의 최상위 대기
아래 예는 최상위 대기 기능을 사용하는 ESModule Lambda를 보여줍니다.
//Top-level await ESModuleJS
import { KMSClient, DecryptCommand } from "@aws-sdk/client-kms"; // ES Modules import
const functionName = process.env.AWS_LAMBDA_FUNCTION_NAME;
const encrypted = process.env['secret'];
let decrypted = await fetchDecryptedValue();
//This is not possible in CommonJS
export const handler = async (event) => {
//Clean code - No need of condition checking on 'decrypted' global variable.
processEvent(event, decrypted);
};
async function fetchDecryptedValue() {
let decryptedValue = '';
const client = new KMSClient();
try {
const req = {
CiphertextBlob: Buffer.from(encrypted, 'base64'),
EncryptionContext: { LambdaFunctionName: functionName },
};
const command = new DecryptCommand(req);
const data = await client.send(command);
decryptedValue = Buffer.from(data.Plaintext).toString();
} catch (err) {
console.log('Decrypt error:', err);
throw err;
}
return decryptedValue;
}
let processEvent = function(event, decrypted) {
// use decrypted credential here
}
giovanni gargiulo에서 Pixabay의 이미지
Reference
이 문제에 관하여(AWS Lambda의 ESModule), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://dev.to/prabusah_53/esmodule-in-aws-lambda-7ml텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)